use of org.xml.sax.EntityResolver in project robovm by robovm.
the class XMLFilterImplTest method testGetSetEntityResolver.
public void testGetSetEntityResolver() {
EntityResolver resolver = new MockResolver();
parent.setEntityResolver(resolver);
assertEquals(resolver, parent.getEntityResolver());
parent.setEntityResolver(null);
assertEquals(null, parent.getEntityResolver());
}
use of org.xml.sax.EntityResolver in project hale by halestudio.
the class XMLPathUpdater method update.
/**
* Actual implementation of the update method.
*
* @param xmlResource the XML resource file that gets updated
* @param oldPath its original location
* @param locationXPath a XPath expression to find nodes that should be
* processed
* @param includeWebResources whether web resources should be copied and
* updates, too
* @param reporter the reporter of the current IO process where errors
* should be reported to
* @param updates a map of already copied files which is used and gets
* filled by this method. Needed for multiple updates on the same
* file.
* @throws IOException if an IO exception occurs
*/
private static void update(File xmlResource, URI oldPath, String locationXPath, boolean includeWebResources, IOReporter reporter, Map<URI, File> updates) throws IOException {
// every XML resource should be updated (and copied) only once
// so we save the currently adapted resource in a map
updates.put(oldPath, xmlResource);
// counter for the directory because every resource should have its own
// directory
int count = 0;
DocumentBuilder builder = null;
try {
builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
} catch (ParserConfigurationException e) {
throw new IOException("Can not create a DocumentBuilder", e);
}
builder.setEntityResolver(new EntityResolver() {
@Override
public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
// FIXME some documentation would be nice why this is OK here?!
return new InputSource(new StringReader(""));
}
});
Document doc = null;
try {
doc = builder.parse(xmlResource);
} catch (SAXException e1) {
// if the file is no XML file simply stop the recursion
return;
}
// find schemaLocation of imports/includes via XPath
XPath xpath = XPathFactory.newInstance().newXPath();
NodeList nodelist = null;
try {
nodelist = ((NodeList) xpath.evaluate(locationXPath, doc, XPathConstants.NODESET));
} catch (XPathExpressionException e) {
throw new IOException("The XPath expression is wrong", e);
}
// iterate over all imports or includes and get the schemaLocations
for (int i = 0; i < nodelist.getLength(); i++) {
Node locationNode = nodelist.item(i);
String location = locationNode.getNodeValue();
URI locationUri = null;
try {
locationUri = new URI(location);
} catch (Exception e1) {
reporter.error(new IOMessageImpl("The location is no valid URI.", e1));
continue;
}
if (!locationUri.isAbsolute()) {
locationUri = oldPath.resolve(locationUri);
}
String scheme = locationUri.getScheme();
InputStream input = null;
if (scheme != null) {
// should the resource be included?
if (includeWebResources || !(scheme.equals("http") || scheme.equals("https"))) {
DefaultInputSupplier supplier = new DefaultInputSupplier(locationUri);
input = supplier.getInput();
} else
continue;
} else {
// file is invalid - at least report that
reporter.error(new IOMessageImpl("Skipped resource because it cannot be loaded from " + locationUri.toString(), null));
continue;
}
// every file needs its own directory because of name conflicts
String filename = location;
if (location.contains("/"))
filename = location.substring(location.lastIndexOf("/") + 1);
filename = count + "/" + filename;
File includednewFile = null;
if (updates.containsKey(locationUri)) {
// if the current XML schema is already updated we have to
// find the relative path to this resource
URI relative = IOUtils.getRelativePath(updates.get(locationUri).toURI(), xmlResource.toURI());
locationNode.setNodeValue(relative.toString());
} else if (input != null) {
// we need the directory of the file
File xmlResourceDir = xmlResource.getParentFile();
// path where the file should be copied to
includednewFile = new File(xmlResourceDir, filename);
try {
includednewFile.getParentFile().mkdirs();
} catch (SecurityException e) {
throw new IOException("Can not create directories " + includednewFile.getParent(), e);
}
// copy to new directory
OutputStream output = new FileOutputStream(includednewFile);
ByteStreams.copy(input, output);
output.close();
input.close();
// set new location in the XML resource
locationNode.setNodeValue(filename);
update(includednewFile, locationUri, locationXPath, includeWebResources, reporter, updates);
count++;
}
// write new XML-File
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = null;
try {
transformer = transformerFactory.newTransformer();
} catch (TransformerConfigurationException e) {
log.debug("Can not create transformer for creating XMl file", e);
return;
}
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(xmlResource);
try {
transformer.transform(source, result);
} catch (TransformerException e) {
log.debug("Cannot create new XML file", e);
return;
}
}
}
use of org.xml.sax.EntityResolver in project nifi by apache.
the class EvaluateXPath method onTrigger.
@Override
@SuppressWarnings("unchecked")
public void onTrigger(final ProcessContext context, final ProcessSession session) {
final List<FlowFile> flowFiles = session.get(50);
if (flowFiles.isEmpty()) {
return;
}
final ComponentLog logger = getLogger();
final XMLReader xmlReader;
try {
xmlReader = XMLReaderFactory.createXMLReader();
} catch (SAXException e) {
logger.error("Error while constructing XMLReader {}", new Object[] { e });
throw new ProcessException(e.getMessage());
}
if (!context.getProperty(VALIDATE_DTD).asBoolean()) {
xmlReader.setEntityResolver(new EntityResolver() {
@Override
public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
return new InputSource(new StringReader(""));
}
});
}
final XPathFactory factory = factoryRef.get();
final XPathEvaluator xpathEvaluator = (XPathEvaluator) factory.newXPath();
final Map<String, XPathExpression> attributeToXPathMap = new HashMap<>();
for (final Map.Entry<PropertyDescriptor, String> entry : context.getProperties().entrySet()) {
if (!entry.getKey().isDynamic()) {
continue;
}
final XPathExpression xpathExpression;
try {
xpathExpression = xpathEvaluator.compile(entry.getValue());
attributeToXPathMap.put(entry.getKey().getName(), xpathExpression);
} catch (XPathExpressionException e) {
// should not happen because we've already validated the XPath (in XPathValidator)
throw new ProcessException(e);
}
}
final XPathExpression slashExpression;
try {
slashExpression = xpathEvaluator.compile("/");
} catch (XPathExpressionException e) {
logger.error("unable to compile XPath expression due to {}", new Object[] { e });
session.transfer(flowFiles, REL_FAILURE);
return;
}
final String destination = context.getProperty(DESTINATION).getValue();
final QName returnType;
switch(context.getProperty(RETURN_TYPE).getValue()) {
case RETURN_TYPE_AUTO:
if (DESTINATION_ATTRIBUTE.equals(destination)) {
returnType = STRING;
} else if (DESTINATION_CONTENT.equals(destination)) {
returnType = NODESET;
} else {
throw new IllegalStateException("The only possible destinations should be CONTENT or ATTRIBUTE...");
}
break;
case RETURN_TYPE_NODESET:
returnType = NODESET;
break;
case RETURN_TYPE_STRING:
returnType = STRING;
break;
default:
throw new IllegalStateException("There are no other return types...");
}
flowFileLoop: for (FlowFile flowFile : flowFiles) {
final AtomicReference<Throwable> error = new AtomicReference<>(null);
final AtomicReference<Source> sourceRef = new AtomicReference<>(null);
session.read(flowFile, new InputStreamCallback() {
@Override
public void process(final InputStream rawIn) throws IOException {
try (final InputStream in = new BufferedInputStream(rawIn)) {
final List<Source> rootList = (List<Source>) slashExpression.evaluate(new SAXSource(xmlReader, new InputSource(in)), NODESET);
sourceRef.set(rootList.get(0));
} catch (final Exception e) {
error.set(e);
}
}
});
if (error.get() != null) {
logger.error("unable to evaluate XPath against {} due to {}; routing to 'failure'", new Object[] { flowFile, error.get() });
session.transfer(flowFile, REL_FAILURE);
continue;
}
final Map<String, String> xpathResults = new HashMap<>();
for (final Map.Entry<String, XPathExpression> entry : attributeToXPathMap.entrySet()) {
Object result = null;
try {
result = entry.getValue().evaluate(sourceRef.get(), returnType);
if (result == null) {
continue;
}
} catch (final XPathExpressionException e) {
logger.error("failed to evaluate XPath for {} for Property {} due to {}; routing to failure", new Object[] { flowFile, entry.getKey(), e });
session.transfer(flowFile, REL_FAILURE);
continue flowFileLoop;
}
if (returnType == NODESET) {
List<Source> nodeList = (List<Source>) result;
if (nodeList.isEmpty()) {
logger.info("Routing {} to 'unmatched'", new Object[] { flowFile });
session.transfer(flowFile, REL_NO_MATCH);
continue flowFileLoop;
} else if (nodeList.size() > 1) {
logger.error("Routing {} to 'failure' because the XPath evaluated to {} XML nodes", new Object[] { flowFile, nodeList.size() });
session.transfer(flowFile, REL_FAILURE);
continue flowFileLoop;
}
final Source sourceNode = nodeList.get(0);
if (DESTINATION_ATTRIBUTE.equals(destination)) {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
doTransform(sourceNode, baos);
xpathResults.put(entry.getKey(), baos.toString("UTF-8"));
} catch (UnsupportedEncodingException e) {
throw new ProcessException(e);
} catch (TransformerException e) {
error.set(e);
}
} else if (DESTINATION_CONTENT.equals(destination)) {
flowFile = session.write(flowFile, new OutputStreamCallback() {
@Override
public void process(final OutputStream rawOut) throws IOException {
try (final OutputStream out = new BufferedOutputStream(rawOut)) {
doTransform(sourceNode, out);
} catch (TransformerException e) {
error.set(e);
}
}
});
}
} else if (returnType == STRING) {
final String resultString = (String) result;
if (DESTINATION_ATTRIBUTE.equals(destination)) {
xpathResults.put(entry.getKey(), resultString);
} else if (DESTINATION_CONTENT.equals(destination)) {
flowFile = session.write(flowFile, new OutputStreamCallback() {
@Override
public void process(final OutputStream rawOut) throws IOException {
try (final OutputStream out = new BufferedOutputStream(rawOut)) {
out.write(resultString.getBytes("UTF-8"));
}
}
});
}
}
}
if (error.get() == null) {
if (DESTINATION_ATTRIBUTE.equals(destination)) {
flowFile = session.putAllAttributes(flowFile, xpathResults);
final Relationship destRel = xpathResults.isEmpty() ? REL_NO_MATCH : REL_MATCH;
logger.info("Successfully evaluated XPaths against {} and found {} matches; routing to {}", new Object[] { flowFile, xpathResults.size(), destRel.getName() });
session.transfer(flowFile, destRel);
session.getProvenanceReporter().modifyAttributes(flowFile);
} else if (DESTINATION_CONTENT.equals(destination)) {
logger.info("Successfully updated content for {}; routing to 'matched'", new Object[] { flowFile });
session.transfer(flowFile, REL_MATCH);
session.getProvenanceReporter().modifyContent(flowFile);
}
} else {
logger.error("Failed to write XPath result for {} due to {}; routing original to 'failure'", new Object[] { flowFile, error.get() });
session.transfer(flowFile, REL_FAILURE);
}
}
}
use of org.xml.sax.EntityResolver in project nifi by apache.
the class EvaluateXQuery method onTrigger.
@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) {
final List<FlowFile> flowFileBatch = session.get(50);
if (flowFileBatch.isEmpty()) {
return;
}
final ComponentLog logger = getLogger();
final Map<String, XQueryExecutable> attributeToXQueryMap = new HashMap<>();
final Processor proc = new Processor(false);
final XMLReader xmlReader;
try {
xmlReader = XMLReaderFactory.createXMLReader();
} catch (SAXException e) {
logger.error("Error while constructing XMLReader {}", new Object[] { e });
throw new ProcessException(e.getMessage());
}
if (!context.getProperty(VALIDATE_DTD).asBoolean()) {
xmlReader.setEntityResolver(new EntityResolver() {
@Override
public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
return new InputSource(new StringReader(""));
}
});
}
final XQueryCompiler comp = proc.newXQueryCompiler();
for (final Map.Entry<PropertyDescriptor, String> entry : context.getProperties().entrySet()) {
if (!entry.getKey().isDynamic()) {
continue;
}
final XQueryExecutable exp;
try {
exp = comp.compile(entry.getValue());
attributeToXQueryMap.put(entry.getKey().getName(), exp);
} catch (SaxonApiException e) {
// should not happen because we've already validated the XQuery (in XQueryValidator)
throw new ProcessException(e);
}
}
final XQueryExecutable slashExpression;
try {
slashExpression = comp.compile("/");
} catch (SaxonApiException e) {
logger.error("unable to compile XQuery expression due to {}", new Object[] { e });
session.transfer(flowFileBatch, REL_FAILURE);
return;
}
final String destination = context.getProperty(DESTINATION).getValue();
flowFileLoop: for (FlowFile flowFile : flowFileBatch) {
if (!isScheduled()) {
session.rollback();
return;
}
final AtomicReference<Throwable> error = new AtomicReference<>(null);
final AtomicReference<XdmNode> sourceRef = new AtomicReference<>(null);
session.read(flowFile, new InputStreamCallback() {
@Override
public void process(final InputStream rawIn) throws IOException {
try (final InputStream in = new BufferedInputStream(rawIn)) {
XQueryEvaluator qe = slashExpression.load();
qe.setSource(new SAXSource(xmlReader, new InputSource(in)));
DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();
dfactory.setNamespaceAware(true);
Document dom = dfactory.newDocumentBuilder().newDocument();
qe.run(new DOMDestination(dom));
XdmNode rootNode = proc.newDocumentBuilder().wrap(dom);
sourceRef.set(rootNode);
} catch (final Exception e) {
error.set(e);
}
}
});
if (error.get() != null) {
logger.error("unable to evaluate XQuery against {} due to {}; routing to 'failure'", new Object[] { flowFile, error.get() });
session.transfer(flowFile, REL_FAILURE);
continue;
}
final Map<String, String> xQueryResults = new HashMap<>();
List<FlowFile> childrenFlowFiles = new ArrayList<>();
for (final Map.Entry<String, XQueryExecutable> entry : attributeToXQueryMap.entrySet()) {
try {
XQueryEvaluator qe = entry.getValue().load();
qe.setContextItem(sourceRef.get());
XdmValue result = qe.evaluate();
if (DESTINATION_ATTRIBUTE.equals(destination)) {
int index = 1;
for (XdmItem item : result) {
String value = formatItem(item, context);
String attributeName = entry.getKey();
if (result.size() > 1) {
attributeName += "." + index++;
}
xQueryResults.put(attributeName, value);
}
} else {
// if (DESTINATION_CONTENT.equals(destination)){
if (result.size() == 0) {
logger.info("Routing {} to 'unmatched'", new Object[] { flowFile });
session.transfer(flowFile, REL_NO_MATCH);
continue flowFileLoop;
} else if (result.size() == 1) {
final XdmItem item = result.itemAt(0);
flowFile = session.write(flowFile, new OutputStreamCallback() {
@Override
public void process(final OutputStream rawOut) throws IOException {
try (final OutputStream out = new BufferedOutputStream(rawOut)) {
writeformattedItem(item, context, out);
} catch (TransformerFactoryConfigurationError | TransformerException e) {
throw new IOException(e);
}
}
});
} else {
for (final XdmItem item : result) {
FlowFile ff = session.clone(flowFile);
ff = session.write(ff, new OutputStreamCallback() {
@Override
public void process(final OutputStream rawOut) throws IOException {
try (final OutputStream out = new BufferedOutputStream(rawOut)) {
try {
writeformattedItem(item, context, out);
} catch (TransformerFactoryConfigurationError | TransformerException e) {
throw new IOException(e);
}
}
}
});
childrenFlowFiles.add(ff);
}
}
}
} catch (final SaxonApiException e) {
logger.error("failed to evaluate XQuery for {} for Property {} due to {}; routing to failure", new Object[] { flowFile, entry.getKey(), e });
session.transfer(flowFile, REL_FAILURE);
session.remove(childrenFlowFiles);
continue flowFileLoop;
} catch (TransformerFactoryConfigurationError | TransformerException | IOException e) {
logger.error("Failed to write XQuery result for {} due to {}; routing original to 'failure'", new Object[] { flowFile, error.get() });
session.transfer(flowFile, REL_FAILURE);
session.remove(childrenFlowFiles);
continue flowFileLoop;
}
}
if (DESTINATION_ATTRIBUTE.equals(destination)) {
flowFile = session.putAllAttributes(flowFile, xQueryResults);
final Relationship destRel = xQueryResults.isEmpty() ? REL_NO_MATCH : REL_MATCH;
logger.info("Successfully evaluated XQueries against {} and found {} matches; routing to {}", new Object[] { flowFile, xQueryResults.size(), destRel.getName() });
session.transfer(flowFile, destRel);
session.getProvenanceReporter().modifyAttributes(flowFile);
} else {
// if (DESTINATION_CONTENT.equals(destination)) {
if (!childrenFlowFiles.isEmpty()) {
logger.info("Successfully created {} new FlowFiles from {}; routing all to 'matched'", new Object[] { childrenFlowFiles.size(), flowFile });
session.transfer(childrenFlowFiles, REL_MATCH);
session.remove(flowFile);
} else {
logger.info("Successfully updated content for {}; routing to 'matched'", new Object[] { flowFile });
session.transfer(flowFile, REL_MATCH);
session.getProvenanceReporter().modifyContent(flowFile);
}
}
}
// end flowFileLoop
}
use of org.xml.sax.EntityResolver in project Payara by payara.
the class BaseVerifier method createDOMObject.
protected void createDOMObject(InputSource source, String dd) throws ParserConfigurationException, SAXException, IOException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(true);
factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
EntityResolver dh = new XMLValidationHandler(false);
builder.setEntityResolver(dh);
Document document = builder.parse(source);
if ((dd.indexOf("sun-")) < 0) {
// NOI18N
if ((dd.indexOf("webservices")) < 0) {
// NOI18N
context.setDocument(document);
} else {
context.setWebServiceDocument(document);
}
} else
context.setRuntimeDocument(document);
}
Aggregations