use of com.helger.xml.serialize.read.ISAXReaderSettings in project ph-commons by phax.
the class MicroReader method readMicroXML.
/**
* Read the passed input source as MicroXML.
*
* @param aInputSource
* The input source to use. May be <code>null</code> in which case
* <code>null</code> is directly returned.
* @param aSettings
* The settings to use. If <code>null</code> the default settings will
* be used.
* @return <code>null</code> if either the input source is <code>null</code>
* or if the input was invalid XML.
*/
@Nullable
public static IMicroDocument readMicroXML(@WillClose @Nullable final InputSource aInputSource, @Nullable final ISAXReaderSettings aSettings) {
if (aInputSource == null)
return null;
final EntityResolver aEntityResolver = aSettings == null ? null : aSettings.getEntityResolver();
final MicroSAXHandler aMicroHandler = new MicroSAXHandler(false, aEntityResolver, true);
// Copy and modify settings
final SAXReaderSettings aRealSettings = SAXReaderSettings.createCloneOnDemand(aSettings);
aRealSettings.setEntityResolver(aMicroHandler).setDTDHandler(aMicroHandler).setContentHandler(aMicroHandler).setLexicalHandler(aMicroHandler);
if (aRealSettings.getErrorHandler() == null) {
// Use MicroHandler as default error handler if none is specified
aRealSettings.setErrorHandler(aMicroHandler);
}
if (aEntityResolver instanceof EntityResolver2) {
// Ensure to use the new aEntityResolver2 APIs if available
aRealSettings.setFeatureValue(EXMLParserFeature.USE_ENTITY_RESOLVER2, true);
}
if (SAXReader.readXMLSAX(aInputSource, aRealSettings).isFailure())
return null;
return aMicroHandler.getDocument();
}
use of com.helger.xml.serialize.read.ISAXReaderSettings in project ph-commons by phax.
the class MicroReaderTest method testReadEntity.
@Test
public void testReadEntity() {
// Read file with notation
final IMicroDocument doc = MicroReader.readMicroXML(new ClassPathResource("xml/xml-entity-public.xml"), new SAXReaderSettings().setEntityResolver(new EmptyEntityResolver()));
assertNotNull(doc);
final MicroSAXHandler aHdl = new MicroSAXHandler(true, new EmptyEntityResolver(), true);
final ISAXReaderSettings aSettings = new SAXReaderSettings().setEntityResolver(aHdl).setDTDHandler(aHdl).setContentHandler(aHdl).setErrorHandler(aHdl).setLexicalHandler(aHdl);
assertTrue(SAXReader.readXMLSAX(InputSourceFactory.create(ClassPathResource.getInputStream("xml/xml-entity-public.xml")), aSettings).isSuccess());
assertNotNull(aHdl.getDocument());
// Write again
assertNotNull(MicroWriter.getNodeAsString(doc));
}
use of com.helger.xml.serialize.read.ISAXReaderSettings in project ph-schematron by phax.
the class SchematronHelper method _recursiveResolveAllSchematronIncludes.
@SuppressFBWarnings("RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE")
@Nonnull
private static ESuccess _recursiveResolveAllSchematronIncludes(@Nonnull final IMicroElement eRoot, @Nonnull final IReadableResource aResource, @Nullable final ISAXReaderSettings aSettings, @Nonnull final ISchematronErrorHandler aErrorHandler, final boolean bLenient) {
if (eRoot != null) {
final DefaultSchematronIncludeResolver aIncludeResolver = new DefaultSchematronIncludeResolver(aResource);
for (final IMicroElement aElement : eRoot.getAllChildElementsRecursive()) if (isValidSchematronNS(aElement.getNamespaceURI(), bLenient) && aElement.getLocalName().equals(CSchematronXML.ELEMENT_INCLUDE)) {
String sHref = aElement.getAttributeValue(CSchematronXML.ATTR_HREF);
try {
final int nHashIndex = sHref.indexOf('#');
String sAnchor = null;
if (nHashIndex >= 0) {
sAnchor = sHref.substring(nHashIndex + 1);
sHref = sHref.substring(0, nHashIndex);
}
final IReadableResource aIncludeRes = aIncludeResolver.getResolvedSchematronResource(sHref);
if (aIncludeRes == null) {
aErrorHandler.handleError(SingleError.builderError().errorLocation(new SimpleLocation(aResource.getPath())).errorText("Failed to resolve include '" + sHref + "'").build());
return ESuccess.FAILURE;
}
if (LOGGER.isDebugEnabled())
LOGGER.debug("Resolved '" + sHref + "' relative to '" + aIncludeResolver.getBaseHref() + "' as '" + aIncludeRes.getPath() + "'");
// Read XML to be included
final IMicroDocument aIncludedDoc = MicroReader.readMicroXML(aIncludeRes, aSettings);
if (aIncludedDoc == null) {
aErrorHandler.handleError(SingleError.builderError().errorLocation(new SimpleLocation(aResource.getPath())).errorText("Failed to parse include " + aIncludeRes).build());
return ESuccess.FAILURE;
}
IMicroElement aIncludedContent;
if (sAnchor == null) {
// no anchor present - include the whole document
// Return the document element
aIncludedContent = aIncludedDoc.getDocumentElement();
} else {
final String sFinalAnchor = sAnchor;
final Wrapper<IMicroElement> aMatch = new Wrapper<>();
// Also include the root element in the search
ChildrenProviderHierarchyVisitor.visitFrom(aIncludedDoc.getDocumentElement(), new DefaultHierarchyVisitorCallback<IMicroNode>() {
@Override
public EHierarchyVisitorReturn onItemBeforeChildren(final IMicroNode aItem) {
if (aItem.isElement()) {
final IMicroElement aCurElement = (IMicroElement) aItem;
final String sID = aCurElement.getAttributeValue("id");
if (sFinalAnchor.equals(sID))
aMatch.set(aCurElement);
}
return EHierarchyVisitorReturn.CONTINUE;
}
}, true);
aIncludedContent = aMatch.get();
if (aIncludedContent == null) {
aErrorHandler.handleError(SingleError.builderWarn().errorLocation(new SimpleLocation(aResource.getPath())).errorText("Failed to resolve an element with the ID '" + sAnchor + "' in " + aIncludeRes + "! Therefore including the whole document!").build());
aIncludedContent = aIncludedDoc.getDocumentElement();
}
}
// Important to detach from parent!
aIncludedContent.detachFromParent();
// It is okay to include sthg else
if (false) {
// Check for correct namespace URI of included content
if (!isValidSchematronNS(aIncludedContent.getNamespaceURI(), bLenient)) {
aErrorHandler.handleError(SingleError.builderError().errorLocation(new SimpleLocation(aResource.getPath())).errorText("The included resource " + aIncludeRes + " contains the wrong XML namespace URI '" + aIncludedContent.getNamespaceURI() + "' but was expected to have: " + StringHelper.getImplodedMapped(", ", getAllValidSchematronNS(bLenient), x -> "'" + x + "'")).build());
return ESuccess.FAILURE;
}
}
// Check that not a whole Schema but only a part is included
if (isValidSchematronNS(aIncludedContent.getNamespaceURI(), bLenient) && CSchematronXML.ELEMENT_SCHEMA.equals(aIncludedContent.getLocalName())) {
aErrorHandler.handleError(SingleError.builderWarn().errorLocation(new SimpleLocation(aResource.getPath())).errorText("The included resource " + aIncludeRes + " seems to be a complete schema. To includes parts of a schema the respective element must be the root element of the included resource.").build());
}
// Recursive resolve includes
if (_recursiveResolveAllSchematronIncludes(aIncludedContent, aIncludeRes, aSettings, aErrorHandler, bLenient).isFailure())
return ESuccess.FAILURE;
// Now replace "include" element with content in MicroDOM
aElement.getParent().replaceChild(aElement, aIncludedContent);
} catch (final IOException ex) {
aErrorHandler.handleError(SingleError.builderError().errorLocation(new SimpleLocation(aResource.getPath())).errorText("Failed to read include '" + sHref + "'").linkedException(ex).build());
return ESuccess.FAILURE;
}
}
}
return ESuccess.SUCCESS;
}
Aggregations