Search in sources :

Example 51 with DOMResult

use of javax.xml.transform.dom.DOMResult in project uPortal by Jasig.

the class XmlUtilitiesImpl method convertToDom.

@Override
public Node convertToDom(XMLEventReader xmlEventReader) throws XMLStreamException {
    //Convert the XmlEventReader into a DOM
    final XMLOutputFactory xmlOutputFactory = this.getXmlOutputFactory();
    final DOMResult sourceDom = new DOMResult(DocumentFactory.getThreadDocument());
    final XMLEventWriter sourceWriter = xmlOutputFactory.createXMLEventWriter(sourceDom);
    sourceWriter.add(xmlEventReader);
    sourceWriter.flush();
    sourceWriter.close();
    return sourceDom.getNode();
}
Also used : XMLOutputFactory(javax.xml.stream.XMLOutputFactory) DOMResult(javax.xml.transform.dom.DOMResult) XMLEventWriter(javax.xml.stream.XMLEventWriter) IndentingXMLEventWriter(org.apereo.portal.xml.stream.IndentingXMLEventWriter)

Example 52 with DOMResult

use of javax.xml.transform.dom.DOMResult in project midpoint by Evolveum.

the class Validator method validateSchema.

// this was made public to allow validation of pre-parsed non-prism documents
public Node validateSchema(Element objectDoc, OperationResult objectResult) {
    OperationResult result = objectResult.createSubresult(Validator.class.getName() + ".validateSchema");
    DOMResult validationResult = new DOMResult();
    try {
        xsdValidator.validate(new DOMSource(objectDoc), validationResult);
    } catch (SAXException e) {
        result.recordFatalError("Validation error: " + e.getMessage(), e);
        objectResult.computeStatus("Validation error: " + e.getMessage());
        return null;
    } catch (IOException e) {
        result.recordFatalError("IO error during validation: " + e.getMessage(), e);
        objectResult.computeStatus("IO error during validation: " + e.getMessage());
        return null;
    }
    result.recordSuccess();
    return validationResult.getNode();
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) DOMResult(javax.xml.transform.dom.DOMResult) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException)

Example 53 with DOMResult

use of javax.xml.transform.dom.DOMResult in project midpoint by Evolveum.

the class TestExtraSchema method testExtraSchema.

/**
	 * Test is extra schema can be loaded to the schema registry and whether the file compliant to that
	 * schema can be validated.
	 */
@Test
public void testExtraSchema() throws SAXException, IOException, SchemaException {
    System.out.println("===[ testExtraSchema ]===");
    Document dataDoc = DOMUtil.parseFile(new File(COMMON_DIR_PATH, "root-foo.xml"));
    PrismContext context = constructPrismContext();
    SchemaRegistryImpl reg = (SchemaRegistryImpl) context.getSchemaRegistry();
    Document extraSchemaDoc = DOMUtil.parseFile(new File(EXTRA_SCHEMA_DIR, "root.xsd"));
    reg.registerSchema(extraSchemaDoc, "file root.xsd");
    reg.initialize();
    Schema javaxSchema = reg.getJavaxSchema();
    assertNotNull(javaxSchema);
    Validator validator = javaxSchema.newValidator();
    DOMResult validationResult = new DOMResult();
    validator.validate(new DOMSource(dataDoc), validationResult);
//		System.out.println("Validation result:");
//		System.out.println(DOMUtil.serializeDOMToString(validationResult.getNode()));
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) DOMResult(javax.xml.transform.dom.DOMResult) Schema(javax.xml.validation.Schema) PrismSchema(com.evolveum.midpoint.prism.schema.PrismSchema) Document(org.w3c.dom.Document) SchemaRegistryImpl(com.evolveum.midpoint.prism.schema.SchemaRegistryImpl) File(java.io.File) Validator(javax.xml.validation.Validator) Test(org.testng.annotations.Test)

Example 54 with DOMResult

use of javax.xml.transform.dom.DOMResult in project lucene-solr by apache.

the class XMLLoader method load.

@Override
public void load(SolrQueryRequest req, SolrQueryResponse rsp, ContentStream stream, UpdateRequestProcessor processor) throws Exception {
    final String charset = ContentStreamBase.getCharsetFromContentType(stream.getContentType());
    InputStream is = null;
    XMLStreamReader parser = null;
    String tr = req.getParams().get(CommonParams.TR, null);
    if (tr != null) {
        if (req.getCore().getCoreDescriptor().isConfigSetTrusted() == false) {
            throw new SolrException(ErrorCode.UNAUTHORIZED, "The configset for this collection was uploaded without any authentication in place," + " and this operation is not available for collections with untrusted configsets. To use this feature, re-upload the configset" + " after enabling authentication and authorization.");
        }
        final Transformer t = getTransformer(tr, req);
        final DOMResult result = new DOMResult();
        // an internal result DOM tree, we just access it directly as input for StAX):
        try {
            is = stream.getStream();
            final InputSource isrc = new InputSource(is);
            isrc.setEncoding(charset);
            final XMLReader xmlr = saxFactory.newSAXParser().getXMLReader();
            xmlr.setErrorHandler(xmllog);
            xmlr.setEntityResolver(EmptyEntityResolver.SAX_INSTANCE);
            final SAXSource source = new SAXSource(xmlr, isrc);
            t.transform(source, result);
        } catch (TransformerException te) {
            throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, te.getMessage(), te);
        } finally {
            IOUtils.closeQuietly(is);
        }
        // second step: feed the intermediate DOM tree into StAX parser:
        try {
            parser = inputFactory.createXMLStreamReader(new DOMSource(result.getNode()));
            this.processUpdate(req, processor, parser);
        } catch (XMLStreamException e) {
            throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, e.getMessage(), e);
        } finally {
            if (parser != null)
                parser.close();
        }
    } else // Normal XML Loader
    {
        try {
            is = stream.getStream();
            if (log.isTraceEnabled()) {
                final byte[] body = IOUtils.toByteArray(is);
                // TODO: The charset may be wrong, as the real charset is later
                // determined by the XML parser, the content-type is only used as a hint!
                log.trace("body", new String(body, (charset == null) ? ContentStreamBase.DEFAULT_CHARSET : charset));
                IOUtils.closeQuietly(is);
                is = new ByteArrayInputStream(body);
            }
            parser = (charset == null) ? inputFactory.createXMLStreamReader(is) : inputFactory.createXMLStreamReader(is, charset);
            this.processUpdate(req, processor, parser);
        } catch (XMLStreamException e) {
            throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, e.getMessage(), e);
        } finally {
            if (parser != null)
                parser.close();
            IOUtils.closeQuietly(is);
        }
    }
}
Also used : InputSource(org.xml.sax.InputSource) DOMSource(javax.xml.transform.dom.DOMSource) XMLStreamReader(javax.xml.stream.XMLStreamReader) Transformer(javax.xml.transform.Transformer) DOMResult(javax.xml.transform.dom.DOMResult) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) SAXSource(javax.xml.transform.sax.SAXSource) XMLStreamException(javax.xml.stream.XMLStreamException) ByteArrayInputStream(java.io.ByteArrayInputStream) SolrException(org.apache.solr.common.SolrException) XMLReader(org.xml.sax.XMLReader) TransformerException(javax.xml.transform.TransformerException)

Example 55 with DOMResult

use of javax.xml.transform.dom.DOMResult in project lucene-solr by apache.

the class QueryTemplateManager method getQueryAsDOM.

/**
   * Fast means of constructing query using a cached,precompiled stylesheet
   */
public static Document getQueryAsDOM(Properties formProperties, Templates template) throws ParserConfigurationException, TransformerException {
    DOMResult result = new DOMResult();
    transformCriteria(formProperties, template, result);
    return (Document) result.getNode();
}
Also used : DOMResult(javax.xml.transform.dom.DOMResult) Document(org.w3c.dom.Document)

Aggregations

DOMResult (javax.xml.transform.dom.DOMResult)61 Document (org.w3c.dom.Document)33 DOMSource (javax.xml.transform.dom.DOMSource)24 Transformer (javax.xml.transform.Transformer)20 DocumentBuilder (javax.xml.parsers.DocumentBuilder)17 TransformerException (javax.xml.transform.TransformerException)14 IOException (java.io.IOException)13 InputSource (org.xml.sax.InputSource)13 StreamSource (javax.xml.transform.stream.StreamSource)12 StringReader (java.io.StringReader)11 SAXResult (javax.xml.transform.sax.SAXResult)10 Element (org.w3c.dom.Element)10 Node (org.w3c.dom.Node)10 SAXSource (javax.xml.transform.sax.SAXSource)9 StreamResult (javax.xml.transform.stream.StreamResult)9 Test (org.junit.Test)9 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)8 Source (javax.xml.transform.Source)8 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)7 InputStream (java.io.InputStream)6