Search in sources :

Example 56 with Source

use of javax.xml.transform.Source in project j2objc by google.

the class FuncDocument method getDoc.

/**
   * Get the document from the given URI and base
   *
   * @param xctxt The XPath runtime state.
   * @param context The current context node
   * @param uri Relative(?) URI of the document
   * @param base Base to resolve relative URI from.
   *
   * @return The document Node pointing to the document at the given URI
   * or null
   *
   * @throws javax.xml.transform.TransformerException
   */
int getDoc(XPathContext xctxt, int context, String uri, String base) throws javax.xml.transform.TransformerException {
    // System.out.println("base: "+base+", uri: "+uri);
    SourceTreeManager treeMgr = xctxt.getSourceTreeManager();
    Source source;
    int newDoc;
    try {
        source = treeMgr.resolveURI(base, uri, xctxt.getSAXLocator());
        newDoc = treeMgr.getNode(source);
    } catch (IOException ioe) {
        throw new TransformerException(ioe.getMessage(), (SourceLocator) xctxt.getSAXLocator(), ioe);
    } catch (TransformerException te) {
        throw new TransformerException(te);
    }
    if (DTM.NULL != newDoc)
        return newDoc;
    // If the uri length is zero, get the uri of the stylesheet.
    if (uri.length() == 0) {
        // Hmmm... this seems pretty bogus to me... -sb
        uri = xctxt.getNamespaceContext().getBaseIdentifier();
        try {
            source = treeMgr.resolveURI(base, uri, xctxt.getSAXLocator());
        } catch (IOException ioe) {
            throw new TransformerException(ioe.getMessage(), (SourceLocator) xctxt.getSAXLocator(), ioe);
        }
    }
    String diagnosticsString = null;
    try {
        if ((null != uri) && (uri.length() > 0)) {
            newDoc = treeMgr.getSourceTree(source, xctxt.getSAXLocator(), xctxt);
        // System.out.println("newDoc: "+((Document)newDoc).getDocumentElement().getNodeName());
        } else
            warn(xctxt, XSLTErrorResources.WG_CANNOT_MAKE_URL_FROM, //"Can not make URL from: "+((base == null) ? "" : base )+uri);
            new Object[] { ((base == null) ? "" : base) + uri });
    } catch (Throwable throwable) {
        // throwable.printStackTrace();
        newDoc = DTM.NULL;
        // path.warn(XSLTErrorResources.WG_ENCODING_NOT_SUPPORTED_USING_JAVA, new Object[]{((base == null) ? "" : base )+uri}); //"Can not load requested doc: "+((base == null) ? "" : base )+uri);
        while (throwable instanceof org.apache.xml.utils.WrappedRuntimeException) {
            throwable = ((org.apache.xml.utils.WrappedRuntimeException) throwable).getException();
        }
        if ((throwable instanceof NullPointerException) || (throwable instanceof ClassCastException)) {
            throw new org.apache.xml.utils.WrappedRuntimeException((Exception) throwable);
        }
        StringWriter sw = new StringWriter();
        PrintWriter diagnosticsWriter = new PrintWriter(sw);
        if (throwable instanceof TransformerException) {
            TransformerException spe = (TransformerException) throwable;
            {
                Throwable e = spe;
                while (null != e) {
                    if (null != e.getMessage()) {
                        diagnosticsWriter.println(" (" + e.getClass().getName() + "): " + e.getMessage());
                    }
                    if (e instanceof TransformerException) {
                        TransformerException spe2 = (TransformerException) e;
                        SourceLocator locator = spe2.getLocator();
                        if ((null != locator) && (null != locator.getSystemId()))
                            diagnosticsWriter.println("   ID: " + locator.getSystemId() + " Line #" + locator.getLineNumber() + " Column #" + locator.getColumnNumber());
                        e = spe2.getException();
                        if (e instanceof org.apache.xml.utils.WrappedRuntimeException)
                            e = ((org.apache.xml.utils.WrappedRuntimeException) e).getException();
                    } else
                        e = null;
                }
            }
        } else {
            diagnosticsWriter.println(" (" + throwable.getClass().getName() + "): " + throwable.getMessage());
        }
        //sw.toString();
        diagnosticsString = throwable.getMessage();
    }
    if (DTM.NULL == newDoc) {
        // System.out.println("what?: "+base+", uri: "+uri);
        if (null != diagnosticsString) {
            warn(xctxt, XSLTErrorResources.WG_CANNOT_LOAD_REQUESTED_DOC, //"Can not load requested doc: "+((base == null) ? "" : base )+uri);
            new Object[] { diagnosticsString });
        } else
            warn(xctxt, XSLTErrorResources.WG_CANNOT_LOAD_REQUESTED_DOC, new Object[] { uri == null ? ((base == null) ? "" : base) + uri : //"Can not load requested doc: "+((base == null) ? "" : base )+uri);
            uri.toString() });
    } else {
    // %REVIEW%
    // TBD: What to do about XLocator?
    // xctxt.getSourceTreeManager().associateXLocatorToNode(newDoc, url, null);
    }
    return newDoc;
}
Also used : SourceTreeManager(org.apache.xpath.SourceTreeManager) IOException(java.io.IOException) XMLString(org.apache.xml.utils.XMLString) Source(javax.xml.transform.Source) TransformerException(javax.xml.transform.TransformerException) WrongNumberArgsException(org.apache.xpath.functions.WrongNumberArgsException) IOException(java.io.IOException) StringWriter(java.io.StringWriter) SourceLocator(javax.xml.transform.SourceLocator) XObject(org.apache.xpath.objects.XObject) TransformerException(javax.xml.transform.TransformerException) PrintWriter(java.io.PrintWriter)

Example 57 with Source

use of javax.xml.transform.Source in project graphhopper by graphhopper.

the class InstructionListTest method verifyGPX.

public void verifyGPX(String gpx) {
    SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema schema = null;
    try {
        Source schemaFile = new StreamSource(getClass().getResourceAsStream("gpx-schema.xsd"));
        schema = schemaFactory.newSchema(schemaFile);
    // using more schemas: http://stackoverflow.com/q/1094893/194609
    } catch (SAXException e1) {
        throw new IllegalStateException("There was a problem with the schema supplied for validation. Message:" + e1.getMessage());
    }
    Validator validator = schema.newValidator();
    try {
        validator.validate(new StreamSource(new StringReader(gpx)));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) Schema(javax.xml.validation.Schema) StreamSource(javax.xml.transform.stream.StreamSource) StringReader(java.io.StringReader) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) Validator(javax.xml.validation.Validator) SAXException(org.xml.sax.SAXException) SAXException(org.xml.sax.SAXException)

Example 58 with Source

use of javax.xml.transform.Source in project hazelcast by hazelcast.

the class DiscoverySpiTest method testSchema.

@Test
public void testSchema() throws Exception {
    String xmlFileName = "test-hazelcast-discovery-spi.xml";
    SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    URL schemaResource = DiscoverySpiTest.class.getClassLoader().getResource("hazelcast-config-3.9.xsd");
    assertNotNull(schemaResource);
    InputStream xmlResource = DiscoverySpiTest.class.getClassLoader().getResourceAsStream(xmlFileName);
    Source source = new StreamSource(xmlResource);
    Schema schema = factory.newSchema(schemaResource);
    Validator validator = schema.newValidator();
    validator.validate(source);
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) Schema(javax.xml.validation.Schema) URL(java.net.URL) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) Validator(javax.xml.validation.Validator) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 59 with Source

use of javax.xml.transform.Source in project midpoint by Evolveum.

the class SchemaRegistryImpl method parseJavaxSchema.

private void parseJavaxSchema() throws SAXException, IOException {
    schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Source[] sources = new Source[schemaDescriptions.size()];
    int i = 0;
    for (SchemaDescription schemaDescription : schemaDescriptions) {
        Source source = schemaDescription.getSource();
        sources[i] = source;
        i++;
    }
    schemaFactory.setResourceResolver(entityResolver);
    javaxSchema = schemaFactory.newSchema(sources);
}
Also used : Source(javax.xml.transform.Source)

Example 60 with Source

use of javax.xml.transform.Source in project jdk8u_jdk by JetBrains.

the class Test method main.

public static void main(String[] args) throws IOException, TransformerException {
    try {
        String address = deployWebservice();
        Service service = Service.create(new URL(address), ServiceImpl.SERVICE_NAME);
        Dispatch<Source> d = service.createDispatch(ServiceImpl.PORT_NAME, Source.class, Service.Mode.MESSAGE);
        Source response = d.invoke(new StreamSource(new StringReader(XML_REQUEST)));
        String resultXml = toString(response);
        log("= request ======== \n");
        log(XML_REQUEST);
        log("= result ========= \n");
        log(resultXml);
        log("\n==================");
        boolean xsAnyMixedPartSame = resultXml.contains(XS_ANY_MIXED_PART);
        log("resultXml.contains(XS_ANY_PART) = " + xsAnyMixedPartSame);
        if (!xsAnyMixedPartSame) {
            fail("The xs:any content=mixed part is supposed to be same in request and response.");
            throw new RuntimeException();
        }
        log("TEST PASSED");
    } finally {
        stopWebservice();
        // if you need to debug or explore wsdl generation result
        // comment this line out:
        deleteGeneratedFiles();
    }
}
Also used : StreamSource(javax.xml.transform.stream.StreamSource) StringReader(java.io.StringReader) Service(javax.xml.ws.Service) URL(java.net.URL) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source)

Aggregations

Source (javax.xml.transform.Source)238 StreamSource (javax.xml.transform.stream.StreamSource)161 DOMSource (javax.xml.transform.dom.DOMSource)108 Transformer (javax.xml.transform.Transformer)76 StreamResult (javax.xml.transform.stream.StreamResult)74 InputSource (org.xml.sax.InputSource)67 SAXSource (javax.xml.transform.sax.SAXSource)56 StringReader (java.io.StringReader)52 IOException (java.io.IOException)46 TransformerException (javax.xml.transform.TransformerException)45 Result (javax.xml.transform.Result)42 TransformerFactory (javax.xml.transform.TransformerFactory)41 Test (org.junit.Test)39 StringWriter (java.io.StringWriter)35 InputStream (java.io.InputStream)32 SAXException (org.xml.sax.SAXException)32 Schema (javax.xml.validation.Schema)29 Validator (javax.xml.validation.Validator)29 SchemaFactory (javax.xml.validation.SchemaFactory)28 ByteArrayInputStream (java.io.ByteArrayInputStream)26