Class OoxmlFacade
java.lang.Object
com.github.oowekyala.ooxml.messages.OoxmlFacade
Main entry point of the API. Example usage:
public AppConfig parseConfigFile(Path path) throws IOException, XmlException {
DocumentBuilder builder;
try {
builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
} catch (ParserConfigurationException e) {
throw new IllegalStateException("Platform does not support XML", e);
}
PositionedXmlDoc doc;
try (Reader reader = Files.newBufferedReader(path)) {
// configure the input source's system ID to have
// a file path in the messages
InputSource iSource = new InputSource();
iSource.setSystemId(path.toString());
iSource.setCharacterStream(reader);
// Here we go
doc = new OoxmlFacade().parse(builder, iSource);
}
// This is the object that maps DOM nodes to file
// positions
XmlPositioner positioner = doc.getPositioner();
// Create the reporter, which you can use during parsing
// to report messages on specific nodes
XmlErrorReporter reporter = new DefaultXmlErrorReporter(XmlMessageHandler.SYSTEM_ERR, positioner));
return appSpecificParsing(doc.getDocument(), reporter);
}
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate NiceXmlMessageFormatterprivate intprivate XmlMessageHandlerprivate boolean -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionintbooleanparse(DocumentBuilder domBuilder, InputSource inputSource) Parses an XML document and creates an associatedXmlPositioner.private PositionedXmlDocparseImpl(DocumentBuilder builder, SpyInputSource isource) private static SpyInputSourcespyOn(InputSource inputSource) withAnsiColors(boolean useAnsiColors) withContextLines(int numContextLines) withFormatter(NiceXmlMessageFormatter formatter) withPrinter(XmlMessageHandler printer)
-
Field Details
-
printer
-
formatter
-
useAnsiColors
private boolean useAnsiColors -
numContextLines
private int numContextLines
-
-
Constructor Details
-
OoxmlFacade
public OoxmlFacade()
-
-
Method Details
-
withFormatter
-
withPrinter
-
withAnsiColors
-
withContextLines
-
getFormatter
-
getPrinter
-
isUseAnsiColors
public boolean isUseAnsiColors() -
getNumContextLines
public int getNumContextLines() -
parse
public PositionedXmlDoc parse(DocumentBuilder domBuilder, InputSource inputSource) throws XmlException, IOException Parses an XML document and creates an associatedXmlPositioner. Exceptions thrown by the parser (eg because of invalid XML syntax) are reported using the given message handler. Their position is recovered on a best-effort basis. Only fatal parsing exceptions are thrown, but even them are passed to the givenXmlMessageHandler.To validate the document against a schema, you must use either
setValidatingorsetSchemaon the document builder factory. Text positions cannot be recovered by aValidatorafter the fact (this is a limitation ofjava.xml). Note that setting both may result in duplicate messages.For best messages back your
InputSourcewith anInputStream, or better, aReader.- Parameters:
domBuilder- Preconfigured DOM builder, the error handler is set by this method.inputSource- Source for the XML document. The system ID should be set for better error messages.- Throws:
IOException- If reading from the input source throws an IOExceptionXmlException- If the parser throws a fatal exception
-
parseImpl
private PositionedXmlDoc parseImpl(DocumentBuilder builder, SpyInputSource isource) throws XmlException, IOException - Throws:
XmlExceptionIOException
-
spyOn
- Throws:
IOException
-