Commit 8ddbd1cb authored by Vincent Calame's avatar Vincent Calame

Création de fichiers à part pour des classes internes

parent eeb7ce52
/* ScrutariLib_Xml - Copyright (c) 2018 Vincent Calame - Exemole
* Logiciel libre donné sous triple licence :
* 1) selon les termes de la CeCILL V2
* 2) selon les termes de l’EUPL V.1.1
* 3) selon les termes de la GNU GPLv3
* Voir le fichier licences.txt
*/
package net.scrutari.xml;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.text.ParseException;
import java.util.Map;
import net.mapeadores.util.attr.AttributeKey;
import net.mapeadores.util.localisation.Lang;
import net.mapeadores.util.logging.ErrorMessageException;
import net.mapeadores.util.logging.MessageHandler;
import net.mapeadores.util.xml.handlers.DefaultElementHandler;
import net.mapeadores.util.xml.handlers.ElementHandler;
import net.mapeadores.util.xml.handlers.LabelElementHandler;
import net.mapeadores.util.xml.handlers.SimpleElementHandler;
import net.scrutari.data.BaseData;
import net.scrutari.data.DataConstants;
import net.scrutari.data.DataValidator;
/**
*
* @author Vincent Calame
*/
class BaseMetadataHandler extends DefaultElementHandler {
private static final int BASE_ICON = 1;
private static final int LANG = 2;
private final BaseData.Builder baseDataBuilder;
private final MessageHandler messageHandler;
private final DataValidator dataValidator;
private final String xpath;
BaseMetadataHandler(BaseData.Builder baseDataBuilder, MessageHandler messageHandler, DataValidator dataValidator, String xpath) {
this.baseDataBuilder = baseDataBuilder;
this.messageHandler = messageHandler;
this.dataValidator = dataValidator;
this.xpath = xpath;
}
@Override
public ElementHandler startSubElement(String tagname, Map<String, String> attributeMap) {
String tagXpath = xpath + "/" + tagname;
switch (tagname) {
case "base-icon":
return new SimpleElementHandler(BASE_ICON);
case "intitule-short":
return new PhraseHandler(baseDataBuilder, messageHandler, DataConstants.BASE_TITLE, tagXpath);
case "intitule-long":
return new PhraseHandler(baseDataBuilder, messageHandler, DataConstants.BASE_LONGTITLE, tagXpath);
case "langs-ui":
return new AvailableLangHandler(baseDataBuilder, messageHandler, tagXpath);
case "attr":
AttributeKey attributeKey = SctXmlUtils.parseAttributeKey(attributeMap, messageHandler, tagXpath);
if (attributeKey != null) {
return new AttrHandler(attributeKey, messageHandler, SctXmlUtils.toXpath(tagXpath, attributeKey), dataValidator.getFormatChecker(attributeKey));
} else {
return null;
}
default:
return null;
}
}
@Override
public void endSimpleSubElement(int type, String text) {
switch (type) {
case BASE_ICON:
boolean done = false;
try {
done = baseDataBuilder.setBaseIcon(SctXmlUtils.testURI(text));
} catch (MalformedURLException | URISyntaxException e) {
}
if (!done) {
SctXmlUtils.format(messageHandler, "_ error.wrong.xml.tagvalue", xpath + "/base-icon", text);
}
break;
}
}
@Override
public void endSubElement(ElementHandler elementHandler) {
if (elementHandler instanceof AttrHandler) {
AttrHandler attrHandler = (AttrHandler) elementHandler;
baseDataBuilder.addAttribute(attrHandler.getAttributeKey(), attrHandler.getValueList());
}
}
private static class PhraseHandler extends DefaultElementHandler {
private final BaseData.Builder baseDataBuilder;
private final MessageHandler messageHandler;
private final String name;
private final String xpath;
private PhraseHandler(BaseData.Builder baseDataBuilder, MessageHandler messageHandler, String name, String xpath) {
this.baseDataBuilder = baseDataBuilder;
this.messageHandler = messageHandler;
this.name = name;
this.xpath = xpath;
}
@Override
public ElementHandler startSubElement(String tagname, Map<String, String> attributeMap) {
if (tagname.equals("lib")) {
try {
return new LabelElementHandler(attributeMap, xpath + "/" + tagname);
} catch (ErrorMessageException eme) {
SctXmlUtils.lang(messageHandler, eme.getErrorMessage());
return null;
}
}
return null;
}
@Override
public void endSubElement(ElementHandler elementHandler) {
baseDataBuilder.addLabel(name, SctXmlUtils.testLabel(elementHandler));
}
}
private static class AvailableLangHandler extends DefaultElementHandler {
private final BaseData.Builder baseDataBuilder;
private final MessageHandler messageHandler;
private final String xpath;
private AvailableLangHandler(BaseData.Builder baseDataBuilder, MessageHandler messageHandler, String xpath) {
this.baseDataBuilder = baseDataBuilder;
this.messageHandler = messageHandler;
this.xpath = xpath;
}
@Override
public ElementHandler startSubElement(String tagname, Map attributeMap) {
if (tagname.equals("lang")) {
return new SimpleElementHandler(LANG);
}
return null;
}
@Override
public void endSimpleSubElement(int type, String text) {
try {
Lang lang = Lang.parse(text);
baseDataBuilder.addAvailableLang(lang);
} catch (ParseException ile) {
SctXmlUtils.lang(messageHandler, "_ error.wrong.xml.tagvalue", xpath + "/lang", text);
}
}
}
}
/* ScrutariLib_Xml - Copyright (c) 2018 Vincent Calame - Exemole
* Logiciel libre donné sous triple licence :
* 1) selon les termes de la CeCILL V2
* 2) selon les termes de l’EUPL V.1.1
* 3) selon les termes de la GNU GPLv3
* Voir le fichier licences.txt
*/
package net.scrutari.xml;
import java.util.Map;
import net.mapeadores.util.logging.MessageHandler;
import net.mapeadores.util.xml.DomMessages;
import net.mapeadores.util.xml.handlers.DefaultElementHandler;
import net.mapeadores.util.xml.handlers.ElementHandler;
import net.mapeadores.util.xml.handlers.SimpleElementHandler;
import net.scrutari.datauri.BaseURI;
/**
*
* @author Vincent Calame
*/
class BaseURIHandler extends DefaultElementHandler {
private static final int AUTHORITY = 1;
private static final int BASE_NAME = 2;
private final MessageHandler messageHandler;
private String authority = null;
private String baseName = null;
BaseURIHandler(MessageHandler messageHandler) {
this.messageHandler = messageHandler;
}
@Override
public ElementHandler startSubElement(String tagname, Map attributeMap) {
if ((tagname.equals("authority")) || (tagname.equals("authority-uuid"))) {
return new SimpleElementHandler(AUTHORITY);
}
if (tagname.equals("base-name")) {
return new SimpleElementHandler(BASE_NAME);
}
return null;
}
@Override
public void endSimpleSubElement(int type, String text) {
if (text.length() == 0) {
text = null;
}
switch (type) {
case AUTHORITY:
authority = text;
break;
case BASE_NAME:
baseName = text;
break;
}
}
BaseURI getBaseURI() {
if (authority == null) {
xml("_ error.unsupported.xml.missingtag", "/base/base-metadata", "authority");
}
if (baseName == null) {
xml("_ error.unsupported.xml.missingtag", "/base/base-metadata", "base-name");
}
if ((authority != null) && (baseName != null)) {
BaseURI baseURI = BaseURI.check(authority, baseName);
if (baseURI == null) {
xml("_ error.wrong.xml.tagvalue", "/base/base-metadata", "authority|base-name");
}
return baseURI;
} else {
return null;
}
}
private void xml(String key, Object... values) {
DomMessages.invalid(messageHandler, key, values);
}
}
......@@ -11,13 +11,9 @@ package net.scrutari.xml;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import net.mapeadores.util.exceptions.NestedLibraryException;
import net.mapeadores.util.logging.MessageHandler;
import net.mapeadores.util.xml.DomMessages;
import net.mapeadores.util.xml.handlers.DefaultElementHandler;
import net.mapeadores.util.xml.handlers.ElementHandler;
import net.mapeadores.util.xml.handlers.SimpleElementHandler;
import net.scrutari.datauri.BaseURI;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
......@@ -30,8 +26,6 @@ import org.xmlpull.v1.XmlPullParserFactory;
*/
public class BaseURIXmlEngine {
private static final int AUTHORITY = 1;
private static final int BASE_NAME = 2;
private static final short START = 0;
private static final short ON_BASE = 1;
private static final short ON_METADATA = 2;
......@@ -50,7 +44,7 @@ public class BaseURIXmlEngine {
public BaseURI read(InputStream inputStream) throws XmlPullParserException, IOException {
XmlPullParser xpp = factory.newPullParser();
xpp.setInput(inputStream, null);
MetadataHandler metadataHandler = null;
BaseURIHandler metadataHandler = null;
short state = START;
int eventType = xpp.getEventType();
do {
......@@ -68,7 +62,7 @@ public class BaseURIXmlEngine {
case ON_BASE:
if (tagName.equals("base-metadata")) {
state = ON_METADATA;
metadataHandler = new MetadataHandler();
metadataHandler = new BaseURIHandler(messageHandler);
break;
} else {
xml("_ error.unsupported.xml.missingtag", "/base", "base-metadata");
......@@ -100,59 +94,4 @@ public class BaseURIXmlEngine {
DomMessages.invalid(messageHandler, key, values);
}
private class MetadataHandler extends DefaultElementHandler {
private String authority = null;
private String baseName = null;
private MetadataHandler() {
}
@Override
public ElementHandler startSubElement(String tagname, Map attributeMap) {
if ((tagname.equals("authority")) || (tagname.equals("authority-uuid"))) {
return new SimpleElementHandler(AUTHORITY);
}
if (tagname.equals("base-name")) {
return new SimpleElementHandler(BASE_NAME);
}
return null;
}
@Override
public void endSimpleSubElement(int type, String text) {
if (text.length() == 0) {
text = null;
}
switch (type) {
case AUTHORITY:
authority = text;
break;
case BASE_NAME:
baseName = text;
break;
}
}
private BaseURI getBaseURI() {
if (authority == null) {
xml("_ error.unsupported.xml.missingtag", "/base/base-metadata", "authority");
}
if (baseName == null) {
xml("_ error.unsupported.xml.missingtag", "/base/base-metadata", "base-name");
}
if ((authority != null) && (baseName != null)) {
BaseURI baseURI = BaseURI.check(authority, baseName);
if (baseURI == null) {
xml("_ error.wrong.xml.tagvalue", "/base/base-metadata", "authority|base-name");
}
return baseURI;
} else {
return null;
}
}
}
}
/* ScrutariLib_Xml - Copyright (c) 2018 Vincent Calame - Exemole
* Logiciel libre donné sous triple licence :
* 1) selon les termes de la CeCILL V2
* 2) selon les termes de l’EUPL V.1.1
* 3) selon les termes de la GNU GPLv3
* Voir le fichier licences.txt
*/
package net.scrutari.xml;
import java.util.Map;
import net.mapeadores.util.logging.MessageHandler;
import net.mapeadores.util.xml.handlers.DefaultElementHandler;
import net.mapeadores.util.xml.handlers.ElementHandler;
import net.scrutari.data.BaseData;
import net.scrutari.data.CorpusData;
import net.scrutari.data.DataValidator;
import net.scrutari.data.ThesaurusData;
import net.scrutari.datauri.CorpusURI;
import net.scrutari.datauri.ThesaurusURI;
/**
*
* @author Vincent Calame
*/
class RootHandler extends DefaultElementHandler {
private final BaseData.Builder baseDataBuilder;
private final Parameters parameters;
private final MessageHandler messageHandler;
private final DataValidator dataValidator;
private final boolean buildBaseMetadata;
RootHandler(BaseData.Builder baseDataBuilder, Parameters parameters, boolean buildBaseMetadata) {
this.baseDataBuilder = baseDataBuilder;
this.parameters = parameters;
this.messageHandler = parameters.getMessageHandler();
this.dataValidator = parameters.getDataValidator();
this.buildBaseMetadata = buildBaseMetadata;
}
@Override
public ElementHandler startSubElement(String tagname, Map<String, String> attributeMap) {
switch (tagname) {
case "base-metadata":
if (buildBaseMetadata) {
return new BaseMetadataHandler(baseDataBuilder, parameters.getMessageHandler(), dataValidator, "/base/base-metadata");
} else {
return null;
}
case "corpus":
String corpusName = getAttributeString(attributeMap, "corpus-name");
if (corpusName == null) {
SctXmlUtils.xml(messageHandler, "_ error.empty.xml.attribute", "/base/corpus", "corpus-name");
return null;
}
CorpusURI corpusURI = CorpusURI.check(parameters.getBaseURI(), corpusName);
if (corpusURI == null) {
SctXmlUtils.format(messageHandler, "_ error.wrong.xml.attributevalue", "/base/corpus", "corpus-name", corpusName);
return null;
}
try {
return new CorpusHandler(corpusURI, parameters);
} catch (IllegalArgumentException iae) {
return null;
}
case "thesaurus":
String thesaurusName = getAttributeString(attributeMap, "thesaurus-name");
if (thesaurusName == null) {
SctXmlUtils.xml(messageHandler, "_ error.empty.xml.attribute", "/base/thesaurus", "thesaurus-name");
return null;
}
ThesaurusURI thesaurusURI = ThesaurusURI.check(parameters.getBaseURI(), thesaurusName);
if (thesaurusURI == null) {
SctXmlUtils.format(messageHandler, "_ error.wrong.xml.attributevalue", "/base/thesaurus", "thesaurus-name", thesaurusName);
return null;
}
try {
return new ThesaurusHandler(thesaurusURI, parameters);
} catch (IllegalArgumentException iae) {
return null;
}
default:
return null;
}
}
@Override
public void endSubElement(ElementHandler elementHandler) {
if (elementHandler instanceof CorpusHandler) {
CorpusData corpusData = ((CorpusHandler) elementHandler).flushCorpusData();
if (corpusData != null) {
baseDataBuilder.addCodeCorpus(corpusData.getCodeCorpus());
}
} else if (elementHandler instanceof ThesaurusHandler) {
ThesaurusData thesaurusData = ((ThesaurusHandler) elementHandler).flushThesaurusData();
if (thesaurusData != null) {
baseDataBuilder.addCodeThesaurus(thesaurusData.getCodeThesaurus());
}
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment