use of javax.xml.transform.dom.DOMResult in project lucene-solr by apache.
the class QueryTemplateManager method getQueryAsDOM.
/**
* Slow means of constructing query - parses stylesheet from input stream
*/
public static Document getQueryAsDOM(Properties formProperties, InputStream xslIs) throws SAXException, IOException, ParserConfigurationException, TransformerException {
DOMResult result = new DOMResult();
transformCriteria(formProperties, xslIs, result);
return (Document) result.getNode();
}
use of javax.xml.transform.dom.DOMResult in project uPortal by Jasig.
the class BaseXsltDataUpgraderTest method testXsltUpgrade.
protected void testXsltUpgrade(final Resource xslResource, final PortalDataKey dataKey, final Resource inputResource, final Resource expectedResultResource, final Resource xsdResource) throws Exception {
final XmlUtilities xmlUtilities = new XmlUtilitiesImpl() {
@Override
public Templates getTemplates(Resource stylesheet) throws TransformerConfigurationException, IOException {
final TransformerFactory transformerFactory = TransformerFactory.newInstance();
return transformerFactory.newTemplates(new StreamSource(stylesheet.getInputStream()));
}
};
final XsltDataUpgrader xsltDataUpgrader = new XsltDataUpgrader();
xsltDataUpgrader.setPortalDataKey(dataKey);
xsltDataUpgrader.setXslResource(xslResource);
xsltDataUpgrader.setXmlUtilities(xmlUtilities);
xsltDataUpgrader.afterPropertiesSet();
//Create XmlEventReader (what the JaxbPortalDataHandlerService has)
final XMLInputFactory xmlInputFactory = xmlUtilities.getXmlInputFactory();
final XMLEventReader xmlEventReader = xmlInputFactory.createXMLEventReader(inputResource.getInputStream());
final Node sourceNode = xmlUtilities.convertToDom(xmlEventReader);
final DOMSource source = new DOMSource(sourceNode);
final DOMResult result = new DOMResult();
xsltDataUpgrader.upgradeData(source, result);
//XSD Validation
final String resultString = XmlUtilitiesImpl.toString(result.getNode());
if (xsdResource != null) {
final Schema schema = this.loadSchema(new Resource[] { xsdResource }, XMLConstants.W3C_XML_SCHEMA_NS_URI);
final Validator validator = schema.newValidator();
try {
validator.validate(new StreamSource(new StringReader(resultString)));
} catch (Exception e) {
throw new XmlTestException("Failed to validate XSLT output against provided XSD", resultString, e);
}
}
XMLUnit.setIgnoreWhitespace(true);
XMLUnit.setNormalizeWhitespace(true);
try {
Diff d = new Diff(new InputStreamReader(expectedResultResource.getInputStream()), new StringReader(resultString));
assertTrue("Upgraded data doesn't match expected data: " + d, d.similar());
} catch (Exception e) {
throw new XmlTestException("Failed to assert similar between XSLT output and expected XML", resultString, e);
} catch (Error e) {
throw new XmlTestException("Failed to assert similar between XSLT output and expected XML", resultString, e);
}
}
use of javax.xml.transform.dom.DOMResult in project galley by Commonjava.
the class XMLInfrastructure method fallbackParseDocument.
private Document fallbackParseDocument(String xml, final Object docSource, final Exception e) throws GalleyMavenXMLException {
logger.debug("Failed to parse: {}. DOM error: {}. Trying STaX parse with IS_REPLACING_ENTITY_REFERENCES == false...", e, docSource, e.getMessage());
try {
Source source;
if (safeInputFactory != null) {
xml = repairXmlDeclaration(xml);
final XMLEventReader eventReader = safeInputFactory.createXMLEventReader(new StringReader(xml));
source = new StAXSource(eventReader);
} else {
// Deal with ø and other undeclared entities...
xml = escapeNonXMLEntityRefs(xml);
final XMLReader reader = XMLReaderFactory.createXMLReader();
reader.setFeature("http://xml.org/sax/features/validation", false);
source = new SAXSource(reader, new InputSource(new StringReader(xml)));
}
final DOMResult result = new DOMResult();
final Transformer transformer = newTransformer();
transformer.transform(source, result);
return (Document) result.getNode();
} catch (final TransformerException e1) {
throw new GalleyMavenXMLException("Failed to parse: %s. Transformer error: %s.\nOriginal DOM error: %s", e1, docSource, e1.getMessage(), e.getMessage());
} catch (final SAXException e1) {
throw new GalleyMavenXMLException("Failed to parse: %s. SAX error: %s.\nOriginal DOM error: %s", e1, docSource, e1.getMessage(), e.getMessage());
} catch (final XMLStreamException e1) {
throw new GalleyMavenXMLException("Failed to parse: %s. STaX error: %s.\nOriginal DOM error: %s", e1, docSource, e1.getMessage(), e.getMessage());
}
}
use of javax.xml.transform.dom.DOMResult in project uPortal by Jasig.
the class JaxbPortalDataHandlerService method importOrUpgradeData.
/** Run the import/update process on the data */
protected final void importOrUpgradeData(String systemId, PortalDataKey portalDataKey, XMLEventReader xmlEventReader) {
//See if there is a registered importer for the data, if so import
final IDataImporter<Object> dataImporterExporter = this.portalDataImporters.get(portalDataKey);
if (dataImporterExporter != null) {
this.logger.debug("Importing: {}", getPartialSystemId(systemId));
final Object data = unmarshallData(xmlEventReader, dataImporterExporter);
dataImporterExporter.importData(data);
this.logger.info("Imported : {}", getPartialSystemId(systemId));
return;
}
//No importer, see if there is an upgrader, if so upgrade
final IDataUpgrader dataUpgrader = this.portalDataUpgraders.get(portalDataKey);
if (dataUpgrader != null) {
this.logger.debug("Upgrading: {}", getPartialSystemId(systemId));
//Convert the StAX stream to a DOM node, due to poor JDK support for StAX with XSLT
final Node sourceNode;
try {
sourceNode = xmlUtilities.convertToDom(xmlEventReader);
} catch (XMLStreamException e) {
throw new RuntimeException("Failed to create StAXSource from original XML reader", e);
}
final DOMSource source = new DOMSource(sourceNode);
final DOMResult result = new DOMResult();
final boolean doImport = dataUpgrader.upgradeData(source, result);
if (doImport) {
//If the upgrader didn't handle the import as well wrap the result DOM in a new Source and start the import process over again
final org.w3c.dom.Node node = result.getNode();
final PortalDataKey upgradedPortalDataKey = new PortalDataKey(node);
if (this.logger.isTraceEnabled()) {
this.logger.trace("Upgraded: " + getPartialSystemId(systemId) + " to " + upgradedPortalDataKey + "\n\nSource XML: \n" + XmlUtilitiesImpl.toString(source.getNode()) + "\n\nResult XML: \n" + XmlUtilitiesImpl.toString(node));
} else {
this.logger.info("Upgraded: {} to {}", getPartialSystemId(systemId), upgradedPortalDataKey);
}
final DOMSource upgradedSource = new DOMSource(node, systemId);
this.importData(upgradedSource, upgradedPortalDataKey);
} else {
this.logger.info("Upgraded and Imported: {}", getPartialSystemId(systemId));
}
return;
}
//No importer or upgrader found, fail
throw new IllegalArgumentException("Provided data " + portalDataKey + " has no registered importer or upgrader support: " + systemId);
}
use of javax.xml.transform.dom.DOMResult in project midpoint by Evolveum.
the class Main method createUserQuery2.
private static QueryType createUserQuery2(String username) throws JAXBException {
QueryType query = new QueryType();
SearchFilterType filter = new SearchFilterType();
PropertyComplexValueFilterClauseType fc = new PropertyComplexValueFilterClauseType();
ItemPathType path = new ItemPathType();
path.setValue("declare namespace c=\"http://midpoint.evolveum.com/xml/ns/public/common/common-3\"; c:name");
fc.setPath(path);
fc.getValue().add(username);
ObjectFactory factory = new ObjectFactory();
JAXBElement<PropertyComplexValueFilterClauseType> equal = factory.createEqual(fc);
JAXBContext jaxbContext = JAXBContext.newInstance("com.evolveum.midpoint.xml.ns._public.common.api_types_3:" + "com.evolveum.midpoint.xml.ns._public.common.common_3:" + "com.evolveum.prism.xml.ns._public.annotation_3:" + "com.evolveum.prism.xml.ns._public.query_3:" + "com.evolveum.prism.xml.ns._public.types_3:");
Marshaller marshaller = jaxbContext.createMarshaller();
DOMResult result = new DOMResult();
marshaller.marshal(equal, result);
filter.setFilterClause(((Document) result.getNode()).getDocumentElement());
query.setFilter(filter);
return query;
}
Aggregations