Example usage for javax.xml.transform.stream StreamResult setSystemId

List of usage examples for javax.xml.transform.stream StreamResult setSystemId

Introduction

In this page you can find the example usage for javax.xml.transform.stream StreamResult setSystemId.

Prototype

public void setSystemId(File f) 

Source Link

Document

Set the system ID from a File reference.

Usage

From source file:test.com.azaptree.services.spring.application.SpringApplicationServiceTest.java

@Test
public void testGenerateXSD() throws JAXBException, IOException {
    final JAXBContext jc = JAXBContext
            .newInstance(com.azaptree.services.spring.application.config.SpringApplicationService.class);
    jc.generateSchema(new SchemaOutputResolver() {

        @Override/*from  w ww  . j  a  v  a 2 s  .  c o  m*/
        public Result createOutput(String namespaceUri, String suggestedFileName) throws IOException {
            File file = new File("target", suggestedFileName);
            StreamResult result = new StreamResult(file);
            result.setSystemId(file.toURI().toURL().toString());
            return result;
        }
    });

    final ByteArrayOutputStream bos = new ByteArrayOutputStream();

    jc.generateSchema(new SchemaOutputResolver() {

        @Override
        public Result createOutput(String namespaceUri, String suggestedFileName) throws IOException {
            StreamResult result = new StreamResult(bos);
            result.setSystemId("");
            return result;
        }
    });

    System.out.println(bos.toString());
}

From source file:com.azaptree.services.command.http.WebXmlRequestCommand.java

@Override
public void generateSchema(final OutputStream os) {
    final Optional<JAXBContext> jaxbContext = getJaxbContext();
    if (!jaxbContext.isPresent()) {
        return;//ww w.  ja v a 2  s  .  com
    }
    try {
        jaxbContext.get().generateSchema(new SchemaOutputResolver() {

            @Override
            public Result createOutput(final String namespaceUri, final String suggestedFileName)
                    throws IOException {
                final StreamResult result = new StreamResult(os);
                result.setSystemId("");
                return result;
            }
        });
    } catch (final IOException e) {
        throw new RuntimeException("generateSchema() failed", e);
    }
}

From source file:com.azaptree.services.spring.application.config.SpringApplicationServiceConfig.java

private void generateSchema(final OutputStream os) {
    try {/*  ww w. j a v a  2 s.c o m*/
        JAXBContext.newInstance(SpringApplicationService.class).generateSchema(new SchemaOutputResolver() {

            @Override
            public Result createOutput(final String namespaceUri, final String suggestedFileName)
                    throws IOException {
                final StreamResult result = new StreamResult(os);
                result.setSystemId("");
                return result;
            }
        });
    } catch (final IOException | JAXBException e) {
        throw new RuntimeException("generateSchema() failed", e);
    }
}

From source file:cz.cas.lib.proarc.common.workflow.profile.WorkflowProfilesTest.java

public void testCreateSchema() throws Exception {
    JAXBContext jctx = JAXBContext.newInstance(WorkflowDefinition.class);
    final Map<String, StreamResult> schemas = new HashMap<String, StreamResult>();
    jctx.generateSchema(new SchemaOutputResolver() {

        @Override/*from   w ww.j ava 2  s  . c o  m*/
        public Result createOutput(String namespaceUri, String suggestedFileName) throws IOException {
            StreamResult sr = new StreamResult(new StringWriter());
            sr.setSystemId(namespaceUri);
            schemas.put(namespaceUri, sr);
            return sr;
        }
    });
    System.out.println(schemas.get(WorkflowProfileConsts.NS_WORKFLOW_V1).getWriter().toString());
}

From source file:org.apache.fop.render.intermediate.IFRenderer.java

/** {@inheritDoc} */
public void startRenderer(OutputStream outputStream) throws IOException {
    try {//w  w  w  . jav a  2  s.co m
        if (outputStream != null) {
            StreamResult result = new StreamResult(outputStream);
            if (getUserAgent().getOutputFile() != null) {
                result.setSystemId(getUserAgent().getOutputFile().toURI().toURL().toExternalForm());
            }
            if (this.documentHandler == null) {
                this.documentHandler = createDefaultDocumentHandler();
            }
            this.documentHandler.setResult(result);
        }
        super.startRenderer(null);
        if (log.isDebugEnabled()) {
            log.debug("Rendering areas via IF document handler (" + this.documentHandler.getClass().getName()
                    + ")...");
        }
        documentHandler.startDocument();
        documentHandler.startDocumentHeader();
    } catch (IFException e) {
        handleIFExceptionWithIOException(e);
    }
}

From source file:org.artifactory.jaxb.JaxbHelper.java

public void generateSchema(final OutputStream stream, final Class<T> clazz, final String namespace) {
    try {/*w ww .  j  ava  2 s  .c  o  m*/
        JAXBContext context = JAXBContext.newInstance(clazz);
        context.generateSchema(new SchemaOutputResolver() {
            @Override
            public Result createOutput(String namespaceUri, String suggestedFileName) throws IOException {
                StreamResult result = new StreamResult(stream);
                result.setSystemId(namespace);
                return result;
            }
        });
    } catch (Exception e) {
        throw new RuntimeException("Failed to write object to stream.", e);
    }
}

From source file:org.xchain.namespaces.sax.ResultCommand.java

/**
 * <p>Returns the Result object for the system-id attribute.</p>
 * @param context the JXPathContext to evaluate against.
 * @return a stream result for the system id.
 *//*www.  j  a  va2s .co  m*/
public Result createResultForSystemId(JXPathContext context) throws Exception {
    // set the system id.
    //String systemId = getSystemId(context);
    String systemId = null;
    if (hasSystemId()) {
        systemId = getSystemId(context);
    } else {
        systemId = getSystemIdDepricated(context);
    }

    // create a result object for the system id.
    URL url = UrlFactory.getInstance().newUrl(systemId);

    // create an output stream for this url.
    OutputStream out = url.openConnection().getOutputStream();

    // create a stream result for the output stream.
    StreamResult streamResult = new StreamResult();
    streamResult.setSystemId(systemId);
    streamResult.setOutputStream(out);

    return streamResult;
}

From source file:org.xchain.namespaces.sax.ResultCommand.java

/**
 * <p>Returns the Result object for the path attribute.</p>
 * @param context the JXPathContext to evaluate against.
 * @return a stream result for the path specified.
 *//*from w w  w. j av a2s.  co m*/
public Result createResultForPath(JXPathContext context) throws Exception {
    // set the system id.
    String path = getPath(context);

    // get the file object for the path.
    File file = new File(path);

    // create the directories leading up to the path.
    File parentFile = file.getParentFile();
    if (!parentFile.exists()) {
        parentFile.mkdirs();
    }

    // make sure that the file also exists.
    file.createNewFile();

    // create an output stream for this url.
    OutputStream out = new FileOutputStream(file);

    // create a stream result for the output stream.
    StreamResult streamResult = new StreamResult();
    streamResult.setSystemId(file.toURL().toExternalForm());
    streamResult.setOutputStream(out);

    return streamResult;
}

From source file:se.mithlond.services.shared.test.entity.PlainJaxbContextRule.java

/**
 * Acquires a JAXB Schema from the provided JAXBContext.
 *
 * @param ctx The context for which am XSD should be constructed.
 * @return A tuple holding the constructed XSD from the provided JAXBContext, and
 * the LSResourceResolver synthesized during the way.
 * @throws NullPointerException     if ctx was {@code null}.
 * @throws IllegalArgumentException if a JAXB-related exception occurred while extracting the schema.
 *///  www .  j a  va  2 s .co  m
public static Tuple<Schema, LSResourceResolver> generateTransientXSD(final JAXBContext ctx)
        throws NullPointerException, IllegalArgumentException {

    // Check sanity
    org.apache.commons.lang3.Validate.notNull(ctx, "Cannot handle null ctx argument.");

    final SortedMap<String, ByteArrayOutputStream> namespace2SchemaMap = new TreeMap<>();

    try {
        ctx.generateSchema(new SchemaOutputResolver() {

            /**
             * {@inheritDoc}
             */
            @Override
            public Result createOutput(final String namespaceUri, final String suggestedFileName)
                    throws IOException {

                // The types should really be annotated with @XmlType(namespace = "... something ...")
                // to avoid using the default ("") namespace.
                if (namespaceUri.isEmpty()) {
                    log.warn("Received empty namespaceUri while resolving a generated schema. "
                            + "Did you forget to add a @XmlType(namespace = \"... something ...\") annotation "
                            + "to your class?");
                }

                // Create the result ByteArrayOutputStream
                final ByteArrayOutputStream out = new ByteArrayOutputStream();
                final StreamResult toReturn = new StreamResult(out);
                toReturn.setSystemId("");

                // Map the namespaceUri to the schemaResult.
                namespace2SchemaMap.put(namespaceUri, out);

                // All done.
                return toReturn;
            }
        });
    } catch (IOException e) {
        throw new IllegalArgumentException("Could not acquire Schema snippets.", e);
    }

    // Convert to an array of StreamSource.
    final MappedSchemaResourceResolver resourceResolver = new MappedSchemaResourceResolver();
    final StreamSource[] schemaSources = new StreamSource[namespace2SchemaMap.size()];
    int counter = 0;
    for (Map.Entry<String, ByteArrayOutputStream> current : namespace2SchemaMap.entrySet()) {

        final byte[] schemaSnippetAsBytes = current.getValue().toByteArray();
        resourceResolver.addNamespace2SchemaEntry(current.getKey(), new String(schemaSnippetAsBytes));

        if (log.isDebugEnabled()) {
            log.info("Generated schema [" + (counter + 1) + "/" + schemaSources.length + "]:\n "
                    + new String(schemaSnippetAsBytes));
        }

        // Copy the schema source to the schemaSources array.
        schemaSources[counter] = new StreamSource(new ByteArrayInputStream(schemaSnippetAsBytes), "");

        // Increase the counter
        counter++;
    }

    try {

        // All done.
        final SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        schemaFactory.setResourceResolver(resourceResolver);
        final Schema transientSchema = schemaFactory.newSchema(schemaSources);

        // All done.
        return new Tuple<>(transientSchema, resourceResolver);

    } catch (final SAXException e) {
        throw new IllegalArgumentException("Could not create Schema from snippets.", e);
    }
}