Commit e68883a8 authored by Vincent Calame's avatar Vincent Calame

Utilisation de SAX pour XmlChecker

parent 0843657a
/* UtilLib - Copyright (c) 2015 Vincent Calame - Exemole
/* UtilLib - Copyright (c) 2015-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
......@@ -11,10 +11,12 @@ package net.scrutari.xml.xmlpull;
import java.io.IOException;
import java.io.InputStream;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import net.mapeadores.util.exceptions.NestedLibraryException;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
/**
......@@ -23,22 +25,18 @@ import org.xmlpull.v1.XmlPullParserFactory;
*/
public class XmlChecker {
private final XmlPullParserFactory factory;
private final SAXParserFactory factory;
public XmlChecker() {
try {
this.factory = XmlPullParserFactory.newInstance(System.getProperty(XmlPullParserFactory.PROPERTY_NAME), null);
} catch (XmlPullParserException xppe) {
throw new NestedLibraryException(xppe);
}
this.factory = SAXParserFactory.newInstance();
}
public boolean check(InputStream inputStream) throws XmlPullParserException, IOException {
XmlPullParser xpp = factory.newPullParser();
xpp.setInput(inputStream, null);
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
eventType = xpp.next();
public boolean check(InputStream inputStream) throws SAXException, IOException {
try {
SAXParser saxParser = factory.newSAXParser();
saxParser.parse(inputStream, new DefaultHandler());
} catch (ParserConfigurationException pce) {
throw new NestedLibraryException(pce);
}
return true;
}
......
......@@ -60,6 +60,7 @@ import net.scrutari.xml.IndexationXmlEngine;
import net.scrutari.xml.Parameters;
import net.scrutari.xml.xmlpull.XmlChecker;
import net.scrutari.xml.xmlpull.XmlPullUtils;
import org.xml.sax.SAXException;
import org.xmlpull.v1.XmlPullParserException;
......@@ -278,9 +279,9 @@ public final class SctEngineImpl implements SctEngine {
if (!stepDone) {
ok = false;
}
} catch (XmlPullParserException | IOException e) {
} catch (SAXException | IOException e) {
scrutariSourceManager.setState(scrutariSource, CollectResult.DATA_NOTXML_STATE);
updateLogHandler.xmlPullParserException(e);
updateLogHandler.exception(e);
ok = false;
}
}
......@@ -303,7 +304,7 @@ public final class SctEngineImpl implements SctEngine {
}
} catch (XmlPullParserException | IOException e) {
scrutariSourceManager.setState(scrutariSource, CollectResult.DATA_NOTXML_STATE);
updateLogHandler.xmlPullParserException(e);
updateLogHandler.exception(e);
}
}
}
......@@ -339,7 +340,7 @@ public final class SctEngineImpl implements SctEngine {
baseXmlEngine.run(baseDataBuilder, parameters, is, first);
} catch (XmlPullParserException | IOException e) {
scrutariSourceManager.setState(scrutariSource, CollectResult.DATA_NOTXML_STATE);
updateLogHandler.xmlPullParserException(e);
updateLogHandler.exception(e);
}
first = false;
}
......@@ -347,7 +348,7 @@ public final class SctEngineImpl implements SctEngine {
try (InputStream is = engineStorage.getConfBaseMetadataInputstream(scrutariSourceName)) {
baseXmlEngine.runMetadataRoot(baseDataBuilder, is, updateLogHandler, dataValidator);
} catch (XmlPullParserException | IOException e) {
updateLogHandler.xmlPullParserException(e);
updateLogHandler.exception(e);
}
}
BaseData baseData = baseDataBuilder.toBaseData(parameters.getCodeBase(), scrutariSourceName);
......@@ -371,7 +372,7 @@ public final class SctEngineImpl implements SctEngine {
indexationXmlEngine.run(is, baseURI);
} catch (XmlPullParserException | IOException e) {
scrutariSourceManager.setState(scrutariSource, CollectResult.DATA_NOTXML_STATE);
updateLogHandler.xmlPullParserException(e);
updateLogHandler.exception(e);
}
}
timeLog.log("indexationdata_parsed");
......
......@@ -36,14 +36,13 @@ public class UpdateLogHandler implements MultiMessageHandler {
builder.setCurrentURI(name);
}
public void saxException(SAXException saxe) {
public void exception(Exception e) {
flush();
DomMessages.saxException(builder, saxe);
}
public void xmlPullParserException(Exception xppe) {
flush();
DomMessages.malformed(builder, "_ error.exception.xml.sax", xppe.getMessage());
if (e instanceof SAXException) {
DomMessages.saxException(builder, (SAXException) e);
} else {
DomMessages.malformed(builder, "_ error.exception.io_withmessage", e.getClass().getName(), e.getMessage());
}
}
@Override
......
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