[DEV] better parser XML

This commit is contained in:
Edouard DUPIN 2021-05-03 16:57:33 +02:00
parent c1ef21754d
commit ad744e3d75
6 changed files with 111 additions and 35 deletions

View File

@ -21,11 +21,6 @@
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry combineaccessrules="false" kind="src" path="/scenarium-logger">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry combineaccessrules="false" kind="src" path="/atriasoft-etk">
<attributes>
<attribute name="module" value="true"/>

View File

@ -9,6 +9,7 @@ package org.atriasoft.exml;
import java.lang.reflect.Array;
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;
@ -98,6 +99,22 @@ public class Exml {
* 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);
}
/**
* Store the Xml in the file
*

View File

@ -10,61 +10,66 @@ import io.scenarium.logger.Logger;
public class Log {
private static final String LIB_NAME = "exml";
private static final String LIB_NAME_DRAW = Logger.getDrawableName(LIB_NAME);
private static final boolean PRINT_CRITICAL = Logger.getNeedPrint(LIB_NAME, LogLevel.CRITICAL);
private static final boolean PRINT_ERROR = Logger.getNeedPrint(LIB_NAME, LogLevel.ERROR);
private static final boolean PRINT_WARNING = Logger.getNeedPrint(LIB_NAME, LogLevel.WARNING);
private static final boolean PRINT_INFO = Logger.getNeedPrint(LIB_NAME, LogLevel.INFO);
private static final boolean PRINT_DEBUG = Logger.getNeedPrint(LIB_NAME, LogLevel.DEBUG);
private static final boolean PRINT_VERBOSE = Logger.getNeedPrint(LIB_NAME, LogLevel.VERBOSE);
private static final boolean PRINT_TODO = Logger.getNeedPrint(LIB_NAME, LogLevel.TODO);
private static final boolean PRINT_PRINT = Logger.getNeedPrint(LIB_NAME, LogLevel.PRINT);
private static final String LIB_NAME_DRAW = Logger.getDrawableName(Log.LIB_NAME);
private static final boolean PRINT_CRITICAL = Logger.getNeedPrint(Log.LIB_NAME, LogLevel.CRITICAL);
private static final boolean PRINT_DEBUG = Logger.getNeedPrint(Log.LIB_NAME, LogLevel.DEBUG);
private static final boolean PRINT_ERROR = Logger.getNeedPrint(Log.LIB_NAME, LogLevel.ERROR);
private static final boolean PRINT_INFO = Logger.getNeedPrint(Log.LIB_NAME, LogLevel.INFO);
private static final boolean PRINT_PRINT = Logger.getNeedPrint(Log.LIB_NAME, LogLevel.PRINT);
private static final boolean PRINT_TODO = Logger.getNeedPrint(Log.LIB_NAME, LogLevel.TODO);
private static final boolean PRINT_VERBOSE = Logger.getNeedPrint(Log.LIB_NAME, LogLevel.VERBOSE);
private static final boolean PRINT_WARNING = Logger.getNeedPrint(Log.LIB_NAME, LogLevel.WARNING);
public static void critical(final String data) {
if (PRINT_CRITICAL) {
Logger.critical(LIB_NAME_DRAW, data);
if (Log.PRINT_CRITICAL) {
Logger.critical(Log.LIB_NAME_DRAW, data);
}
}
public static void debug(final String data) {
if (PRINT_DEBUG) {
Logger.debug(LIB_NAME_DRAW, data);
if (Log.PRINT_DEBUG) {
Logger.debug(Log.LIB_NAME_DRAW, data);
}
}
public static void displayBackTrace() {
// TODO Auto-generated method stub
}
public static void error(final String data) {
if (PRINT_ERROR) {
Logger.error(LIB_NAME_DRAW, data);
if (Log.PRINT_ERROR) {
Logger.error(Log.LIB_NAME_DRAW, data);
}
}
public static void info(final String data) {
if (PRINT_INFO) {
Logger.info(LIB_NAME_DRAW, data);
if (Log.PRINT_INFO) {
Logger.info(Log.LIB_NAME_DRAW, data);
}
}
public static void print(final String data) {
if (PRINT_PRINT) {
Logger.print(LIB_NAME_DRAW, data);
if (Log.PRINT_PRINT) {
Logger.print(Log.LIB_NAME_DRAW, data);
}
}
public static void todo(final String data) {
if (PRINT_TODO) {
Logger.todo(LIB_NAME_DRAW, data);
if (Log.PRINT_TODO) {
Logger.todo(Log.LIB_NAME_DRAW, data);
}
}
public static void verbose(final String data) {
if (PRINT_VERBOSE) {
Logger.verbose(LIB_NAME_DRAW, data);
if (Log.PRINT_VERBOSE) {
Logger.verbose(Log.LIB_NAME_DRAW, data);
}
}
public static void warning(final String data) {
if (PRINT_WARNING) {
Logger.warning(LIB_NAME_DRAW, data);
if (Log.PRINT_WARNING) {
Logger.warning(Log.LIB_NAME_DRAW, data);
}
}

View File

@ -28,8 +28,7 @@ public class XmlElement extends XmlAttributeList {
/**
* Constructor
*/
public XmlElement() {
}
public XmlElement() {}
/**
* Constructor
@ -142,6 +141,18 @@ public class XmlElement extends XmlAttributeList {
throw new ExmlNodeDoesNotExist("Node does not exist: '" + name + "' in " + this.listAttribute.size());
}
public XmlNode getNodeNoExcept(final String name) {
if (name.isEmpty()) {
return null;
}
for (int iii = 0; iii < this.listSub.size(); iii++) {
if (this.listSub.get(iii) != null && this.listSub.get(iii).getValue().contentEquals(name)) {
return this.listSub.get(iii);
}
}
return null;
}
/**
* Get the list of the sub-nodes.
* @return List of current nodes.

View File

@ -200,6 +200,27 @@ public class ParseXml {
return false;
}
protected boolean iParseDOCTYPE(final Object parent, final String data, final PositionParsing pos, final FilePos filePos, final ParsingProperty parsingProperty) throws ExmlBuilderException {
Log.verbose("start parse : 'DOCTYPE'");
final FilePos tmpPos = new FilePos();
final int white = Tools.countWhiteChar(data, pos.value, tmpPos);
filePos.add(tmpPos);
// search end of the comment :
for (int iii = pos.value + white; iii < data.length(); iii++) {
Tools.drawElementParsed(data.charAt(iii), filePos);
if (filePos.check(data.charAt(iii))) {
continue;
}
if (data.charAt(iii) == '>') {
pos.value = iii;
return true;
}
}
pos.value = data.length();
parsingProperty.createError(new ExmlParserError(Tools.extractLine(data, pos.value), filePos, "DOTYPE got end of file without finding end node"));
return false;
}
protected boolean iParseElement(final Object parent, final String nameElement, final String data, final PositionParsing pos, final FilePos filePos, final ParsingProperty parsingProperty)
throws ExmlBuilderException {
// note : When start parsing the upper element must have set the value of the element and set the position after this one
@ -410,6 +431,23 @@ public class ParseXml {
return false;
}
iii = pos.value;
} else if (data.charAt(iii + white + 2) == 'D' && iii + white + 9 < data.length() && data.charAt(iii + white + 3) == 'O' && data.charAt(iii + white + 4) == 'C'
&& data.charAt(iii + white + 5) == 'T' && data.charAt(iii + white + 6) == 'Y' && data.charAt(iii + white + 7) == 'P' && data.charAt(iii + white + 8) == 'E') {
tmpPos.increment();
tmpPos.increment();
tmpPos.increment();
tmpPos.increment();
tmpPos.increment();
tmpPos.increment();
tmpPos.increment();
tmpPos.increment();
// find comment:
pos.value = iii + white + 4;
filePos.add(tmpPos);
if (!iParseDOCTYPE(parent, data, pos, filePos, parsingProperty)) {
return false;
}
iii = pos.value;
} else {
parsingProperty
.createError(new ExmlParserError(Tools.extractLine(data, pos.value), filePos, "End file with '<!" + data.charAt(iii + white + 2) + "' chars == > invalide XML"));

View File

@ -20,9 +20,9 @@ public class Tools {
* @return false The value can NOT be a part of attribute name
*/
public static boolean checkAvaillable(final Character val, final boolean firstChar) {
if (val == '!' || val == '"' || val == '#' || val == '$' || val == '%' || val == '&' || val == '\'' // '
|| val == '(' || val == ')' || val == '*' || val == '+' || val == ',' || val == '/' || val == ';' || val == '<' || val == '=' || val == '>' || val == '?' || val == '@' || val == '['
|| val == '\\' || val == ']' || val == '^' || val == '`' || val == '{' || val == '|' || val == '}' || val == '~' || val == ' ' || val == '\n' || val == '\t' || val == '\r') {
if (val == '!' || val == '"' || val == '#' || val == '$' || val == '%' || val == '&' || val == '\'' || val == '(' || val == ')' || val == '*' || val == '+' || val == ',' || val == '/'
|| val == ';' || val == '<' || val == '=' || val == '>' || val == '?' || val == '@' || val == '[' || val == '\\' || val == ']' || val == '^' || val == '`' || val == '{' || val == '|'
|| val == '}' || val == '~' || val == ' ' || val == '\n' || val == '\t' || val == '\r') {
return false;
}
if (firstChar) {
@ -33,6 +33,16 @@ public class Tools {
return true;
}
public static boolean checkNumber(final Character val, final boolean firstChar) {
if (val == '.' || (val >= '0' && val <= '9')) {
return true;
}
if (firstChar && val == '-') {
return true;
}
return false;
}
public static String cleanNumberList(final String data) {
return data.replaceAll("[ \t\n\r]", "").replaceAll(",", ";");
}