exml/src/org/atriasoft/exml/Exml.java

178 lines
6.1 KiB
Java

/** @file
* @author Edouard DUPIN
* @copyright 2021, Edouard DUPIN, all right reserved
* @license MPL v2.0 (see license file)
*/
package org.atriasoft.exml;
import java.io.IOException;
import java.lang.reflect.Array;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import org.atriasoft.etk.Uri;
import org.atriasoft.exml.builder.Builder;
import org.atriasoft.exml.builder.BuilderGeneric;
import org.atriasoft.exml.builder.BuilderIntrospection;
import org.atriasoft.exml.builder.IntrospectionObject;
import org.atriasoft.exml.exception.ExmlBuilderException;
import org.atriasoft.exml.exception.ExmlParserErrorMulti;
import org.atriasoft.exml.internal.Log;
import org.atriasoft.exml.model.XmlElement;
import org.atriasoft.exml.model.XmlNode;
import org.atriasoft.exml.parser.ParseXml;
import org.atriasoft.exml.parser.ParsingProperty;
import org.atriasoft.exml.serializer.SerializerXml;
public class Exml {
/**
* Display the Document on console
*/
public static void display(final XmlElement node) {
final StringBuilder tmpp = new StringBuilder();
Exml.generate(node, tmpp);
Log.info("Generated XML : \n" + tmpp.toString());
}
public static void generate(final Object root, final StringBuilder data) {
// TODO ...
}
/**
* Generate a string that contain the created XML
*
* @param data Data where the xml is stored
*/
public static void generate(final XmlNode root, final StringBuilder data) {
if (!root.isElement() || !((XmlElement) root).getValue().isEmpty()) {
SerializerXml.serialize(root, data, 0);
} else {
SerializerXml.serializeRoot((XmlElement) root, data);
}
// return iGenerate(_data, 0);
}
public static XmlElement parse(final String data) throws ExmlBuilderException, ExmlParserErrorMulti {
final Builder builder = new BuilderGeneric();
final ParseXml parser = new ParseXml(builder);
final ParsingProperty property = new ParsingProperty();
property.setDisplayError(true);
return (XmlElement) parser.parse(data, property);
}
public static <T> T[] parse(final String data, final Class<T> classType, final String rootNodeName) throws ExmlBuilderException, ExmlParserErrorMulti {
Builder builder;
try {
builder = new BuilderIntrospection(classType, rootNodeName);
} catch (final Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
final ParseXml parser = new ParseXml(builder);
final ParsingProperty property = new ParsingProperty();
property.setDisplayError(true);
final IntrospectionObject introspectionObject = (IntrospectionObject) parser.parse(data, property);
final Object listRet = introspectionObject.getData();
if (listRet != null && listRet instanceof List) {
final List<T> rootList = (List<T>) listRet;
final T[] strarr = (T[]) Array.newInstance(classType, 0);
return rootList.toArray(strarr);
}
return null;
}
private static String readFile(final Path path, final Charset encoding) throws IOException
{
byte[] encoded = Files.readAllBytes(path);
return new String(encoded, encoding);
}
public static <T> T[] parse(final Path path, final Class<T> classType, final String rootNodeName) throws ExmlBuilderException, ExmlParserErrorMulti {
String content = null;
try {
content = Exml.readFile(path, StandardCharsets.UTF_8);
} catch (IOException e) {
e.printStackTrace();
}
return Exml.parse(content, classType, rootNodeName);
}
/**
* Load the file that might contain the xml
*
* @param _uri URI of the xml
* @return false : An error occured
* @return true : Parsing is OK
*/
/*
* public boolean load( Uri _uri){ // Start loading the XML :
* EXML_VERBOSE("open file (xml) " + _uri); clear(); auto fileIo =
* uri::get(_uri); if (fileIo == null) { Log.error("File Does not exist : " +
* _uri); return false; } if (fileIo->open(io::OpenMode::Read) == false) {
* Log.error("Can not open (r) the file : " + _uri); return false; } // load
* data from the file: String tmpDataUnicode = fileIo->readAllString(); // close
* the file: fileIo->close(); // parse the data: boolean ret =
* parse(tmpDataUnicode); //Display(); return ret; }
*/
public static XmlElement parse(final Uri data) throws ExmlBuilderException, ExmlParserErrorMulti {
final Builder builder = new BuilderGeneric();
final ParseXml parser = new ParseXml(builder);
final ParsingProperty property = new ParsingProperty();
property.setDisplayError(true);
final byte[] elemData = Uri.getAllData(data);
if (elemData == null) {
Log.error("Can not read the Stream : " + data);
Log.displayBackTrace();
return null;
}
final String dataToParse = new String(elemData);
return (XmlElement) parser.parse(dataToParse, property);
}
public static XmlElement parse(final Path data) throws ExmlBuilderException, ExmlParserErrorMulti {
final Builder builder = new BuilderGeneric();
final ParseXml parser = new ParseXml(builder);
final ParsingProperty property = new ParsingProperty();
property.setDisplayError(true);
byte[] elemData = null;
try {
elemData = Files.readAllBytes(data);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (elemData == null) {
Log.error("Can not read the Stream : " + data);
Log.displayBackTrace();
return null;
}
final String dataToParse = new String(elemData);
return (XmlElement) parser.parse(dataToParse, property);
}
/**
* Store the Xml in the file
*
* @param _uri URI of the xml
* @return false : An error occured
* @return true : Parsing is OK
*/
/*
* public boolean store( Uri _uri){ String createData; if (generate(createData)
* == false) { Log.error("Error while creating the XML: " + _uri); return false;
* } auto fileIo = uri::get(_uri); if (fileIo == null) {
* Log.error("Can not create the uri: " + _uri); return false; } if
* (fileIo->open(io::OpenMode::Write) == false) {
* Log.error("Can not open (r) the file : " + _uri); return false; }
* fileIo->writeAll(createData); fileIo->close(); return true; }
*/
private Exml() {}
}