Example usage for javax.xml.stream XMLStreamWriter writeCharacters

List of usage examples for javax.xml.stream XMLStreamWriter writeCharacters

Introduction

In this page you can find the example usage for javax.xml.stream XMLStreamWriter writeCharacters.

Prototype

public void writeCharacters(String text) throws XMLStreamException;

Source Link

Document

Write text to the output

Usage

From source file:org.deegree.services.wps.execute.ExecuteResponseXMLWriter.java

private static void exportProcess(XMLStreamWriter writer, ProcessDefinition process) throws XMLStreamException {

    // "wps:Process" (minOccurs="1", maxOccurs="1")
    writer.writeStartElement(WPS_NS, "Process");
    writer.writeAttribute(WPS_NS, "processVersion", process.getProcessVersion());

    // "ows:Identifier" (minOccurs="1", maxOccurs="1")
    writer.writeStartElement(OWS_NS, "Identifier");
    if (process.getIdentifier().getCodeSpace() != null) {
        writer.writeAttribute("codeSpace", process.getIdentifier().getCodeSpace());
    }/*  ww  w .  j  a  v a  2  s .c  om*/
    writer.writeCharacters(process.getIdentifier().getValue());
    writer.writeEndElement();

    // "ows:Title" (minOccurs="1", maxOccurs="1")
    if (process.getTitle() != null) {
        writer.writeStartElement(OWS_NS, "Title");
        if (process.getTitle().getLang() != null) {
            writer.writeAttribute("xml:lang", process.getTitle().getLang());
        }
        writer.writeCharacters(process.getTitle().getValue());
        writer.writeEndElement();
    }

    // "ows:Abstract" (minOccurs="0", maxOccurs="1")
    if (process.getAbstract() != null) {
        writer.writeStartElement(OWS_NS, "Abstract");
        if (process.getAbstract().getLang() != null) {
            writer.writeAttribute("xml:lang", process.getAbstract().getLang());
        }
        writer.writeCharacters(process.getAbstract().getValue());
        writer.writeEndElement();
    }

    // "ows:Metadata" (minOccurs="0", maxOccurs="unbounded")
    if (process.getMetadata() != null) {
        for (Metadata metadata : process.getMetadata()) {
            writer.writeStartElement(OWS_NS, "Metadata");
            if (metadata.getAbout() != null) {
                writer.writeAttribute("about", metadata.getAbout());
            }
            if (metadata.getHref() != null) {
                writer.writeAttribute(XLN_NS, "href", metadata.getHref());
            }
            writer.writeEndElement();
        }
    }

    // "wps:Profile" (minOccurs="0", maxOccurs="unbounded")
    if (process.getProfile() != null) {
        for (String profile : process.getProfile()) {
            writeElement(writer, WPS_NS, "Profile", profile);
        }
    }

    // "wps:WSDL" (minOccurs="0", maxOccurs="unbounded")
    if (process.getWSDL() != null) {
        writeElement(writer, WPS_NS, "WSDL", XLN_NS, "href", process.getWSDL());
    }

    writer.writeEndElement(); // wps:Process
}

From source file:org.deegree.services.wps.execute.ExecuteResponseXMLWriter.java

private static void exportStatus(XMLStreamWriter writer, ProcessExecution state) throws XMLStreamException {

    writer.writeStartElement(WPS_NS, "Status");

    // "creationTime" attribute (mandatory)
    long creationTime = state.getFinishTime();
    if (creationTime == -1) {
        // use creation time of document if process execution has not been finished yet
        creationTime = System.currentTimeMillis();
    }/*from   w w w.j a  v  a 2 s  . c  o  m*/
    writer.writeAttribute("creationTime", ISO8601Converter.formatDateTime(new Date(creationTime)));

    switch (state.getExecutionState()) {
    case ACCEPTED:
        writeElement(writer, WPS_NS, "ProcessAccepted", state.getAcceptedMessage());
        break;
    case STARTED:
        writer.writeStartElement(WPS_NS, "ProcessStarted");
        writer.writeAttribute("percentCompleted", "" + state.getPercentCompleted());
        if (state.getStartMessage() != null) {
            writer.writeCharacters(state.getStartMessage());
        }
        writer.writeEndElement();
        break;
    case PAUSED:
        writer.writeStartElement(WPS_NS, "ProcessPaused");
        writer.writeAttribute("percentCompleted", "" + state.getPercentCompleted());
        if (state.getPauseMessage() != null) {
            writer.writeCharacters(state.getPauseMessage());
        }
        writer.writeEndElement();
        break;
    case SUCCEEDED:
        writeElement(writer, WPS_NS, "ProcessSucceeded", state.getSucceededMessage());
        break;
    case FAILED:
        writer.writeStartElement(WPS_NS, "ProcessFailed");
        OWS110ExceptionReportSerializer exceptionAdapter = new OWS110ExceptionReportSerializer(VERSION_100);
        exceptionAdapter.serializeExceptionToXML(writer, state.getFailedException());
        writer.writeEndElement();
        break;
    }
    writer.writeEndElement();
}

From source file:org.deegree.services.wps.execute.ExecuteResponseXMLWriter.java

private static void exportOutput(XMLStreamWriter writer, ProcessletOutputImpl output,
        RequestedOutput requestedOutput) throws XMLStreamException {

    // "wps:Output" element
    writer.writeStartElement(WPS_NS, "Output");

    ProcessletOutputDefinition outputType = output.getDefinition();

    // "ows:Identifier" (minOccurs="1", maxOccurs="1")
    writer.writeStartElement(OWS_NS, "Identifier");
    if (outputType.getIdentifier().getCodeSpace() != null) {
        writer.writeAttribute("codeSpace", outputType.getIdentifier().getCodeSpace());
    }/*from   w  w  w .j  a  v  a 2 s.  c  om*/
    writer.writeCharacters(outputType.getIdentifier().getValue());
    writer.writeEndElement();

    // "ows:Title" (minOccurs="1", maxOccurs="1")
    if (outputType.getTitle() != null) {
        writer.writeStartElement(OWS_NS, "Title");
        if (outputType.getTitle().getLang() != null) {
            writer.writeAttribute("xml:lang", outputType.getTitle().getLang());
        }
        writer.writeCharacters(outputType.getTitle().getValue());
        writer.writeEndElement();
    }

    // "ows:Abstract" (minOccurs="0", maxOccurs="1")
    if (outputType.getAbstract() != null) {
        writer.writeStartElement(OWS_NS, "Abstract");
        if (outputType.getAbstract().getLang() != null) {
            writer.writeAttribute("xml:lang", outputType.getAbstract().getLang());
        }
        writer.writeCharacters(outputType.getAbstract().getValue());
        writer.writeEndElement();
    }

    // "ows:Metadata" (minOccurs="0", maxOccurs="unbounded")
    if (outputType.getMetadata() != null) {
        for (ProcessletOutputDefinition.Metadata metadata : outputType.getMetadata()) {
            writer.writeStartElement(OWS_NS, "Metadata");
            if (metadata.getAbout() != null) {
                writer.writeAttribute("about", metadata.getAbout());
            }
            if (metadata.getHref() != null) {
                writer.writeAttribute(XLN_NS, "href", metadata.getHref());
            }
            writer.writeEndElement();
        }
    }

    // choice: "wps:Reference" or "wps:Data" (minOccurs="1", maxOccurs="1")
    // (ignore asReference for Literal or BoundingBoxOutput)
    if (!requestedOutput.getAsReference() || output instanceof BoundingBoxOutputImpl
            || output instanceof LiteralOutputImpl) {

        writer.writeStartElement(WPS_NS, "Data");

        if (output instanceof BoundingBoxOutputImpl) {
            exportBoundingBoxOutput(writer, (BoundingBoxOutputImpl) output);
        } else if (output instanceof LiteralOutputImpl) {
            exportLiteralOutput(writer, (LiteralOutputImpl) output);
        } else if (output instanceof ComplexOutputImpl) {
            exportComplexOutput(writer, (ComplexOutputImpl) output);
        }

        writer.writeEndElement();
    } else {
        writer.writeStartElement(WPS_NS, "Reference");

        String href = null;

        String mimeType = null;
        if (output instanceof ComplexOutputImpl) {
            ComplexOutputImpl complexOutput = (ComplexOutputImpl) output;
            href = complexOutput.getWebURL();
            mimeType = complexOutput.getRequestedMimeType();
        }

        if (mimeType == null) {
            LOG.warn("No mime type info available -> text/xml");
            mimeType = "text/xml";
        }

        writer.writeAttribute("href", "" + href);
        writer.writeAttribute("mimeType", mimeType);
        writer.writeEndElement();
    }
    writer.writeEndElement();
}

From source file:org.deegree.services.wps.execute.ExecuteResponseXMLWriter.java

private static void exportLiteralOutput(XMLStreamWriter writer, LiteralOutputImpl output)
        throws XMLStreamException {

    // "wps:LiteralData" element
    writer.writeStartElement(WPS_NS, "LiteralData");

    // "dataType" attribute (optional)
    if (output.getDataType() != null) {
        writer.writeAttribute("dataType", output.getDataType());
    }/*from   w w w . jav  a2 s.co m*/

    // "uom" attribute (optional)
    if (output.getRequestedUOM() != null) {
        writer.writeAttribute("uom", output.getRequestedUOM());
    }

    writer.writeCharacters(output.getValue());

    writer.writeEndElement();
}

From source file:org.deegree.services.wps.execute.ExecuteResponseXMLWriter.java

private static void exportBinaryOutput(XMLStreamWriter writer, ComplexOutputImpl output)
        throws XMLStreamException {

    // "wps:ComplexData" element
    writer.writeStartElement(WPS_NS, "ComplexData");

    String mimeType = output.getRequestedMimeType();
    if (mimeType != null) {
        writer.writeAttribute("mimeType", mimeType);
    }//from   w  w w .j ava  2s .  c  om

    LOG.warn("TODO Handle other encodings. Using fixed encoding: base64.");
    writer.writeAttribute("encoding", "base64");

    ByteArrayOutputStream os = new ByteArrayOutputStream();
    InputStream is = output.getInputStream();
    byte[] buffer = new byte[1024];
    int bytesRead = 0;
    try {
        while ((bytesRead = is.read(buffer)) != -1) {
            os.write(buffer, 0, bytesRead);
        }
        os.flush();
        os.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    byte[] binary = os.toByteArray();
    String base64 = Base64.encodeBase64String(binary);
    writer.writeCharacters(base64);

    writer.writeEndElement();
}

From source file:org.deegree.tools.services.wms.FeatureTypesToLayerTree.java

private static void writeLayer(HashSet<FeatureType> visited, XMLStreamWriter out, FeatureType ft,
        String storeId) throws XMLStreamException {
    if (visited.contains(ft) || ft == null) {
        return;/*from ww  w.ja  va2s . co  m*/
    }
    visited.add(ft);

    out.writeCharacters("\n");
    out.writeStartElement(ns, "RequestableLayer");

    XMLAdapter.writeElement(out, ns, "Name", ft.getName().getLocalPart());
    XMLAdapter.writeElement(out, ns, "Title", ft.getName().getLocalPart());

    for (FeatureType sub : ft.getSchema().getDirectSubtypes(ft)) {
        writeLayer(visited, out, sub, storeId);
    }

    XMLAdapter.writeElement(out, ns, "FeatureStoreId", storeId);

    out.writeEndElement();
    out.writeCharacters("\n");
}

From source file:org.deegree.tools.services.wms.FeatureTypesToLayerTree.java

/**
 * @param args//from   w w  w.  j a v a2 s .  c o m
 */
public static void main(String[] args) {
    Options options = initOptions();

    // for the moment, using the CLI API there is no way to respond to a help argument; see
    // https://issues.apache.org/jira/browse/CLI-179
    if (args.length == 0 || (args.length > 0 && (args[0].contains("help") || args[0].contains("?")))) {
        CommandUtils.printHelp(options, FeatureTypesToLayerTree.class.getSimpleName(), null, null);
    }

    XMLStreamWriter out = null;
    try {
        CommandLine line = new PosixParser().parse(options, args);

        String storeFile = line.getOptionValue("f");
        String nm = new File(storeFile).getName();
        String storeId = nm.substring(0, nm.length() - 4);

        FileOutputStream os = new FileOutputStream(line.getOptionValue("o"));
        XMLOutputFactory fac = XMLOutputFactory.newInstance();
        out = new IndentingXMLStreamWriter(fac.createXMLStreamWriter(os));
        out.setDefaultNamespace(ns);

        Workspace ws = new DefaultWorkspace(new File("nix"));
        ws.initAll();
        DefaultResourceIdentifier<FeatureStore> identifier = new DefaultResourceIdentifier<FeatureStore>(
                FeatureStoreProvider.class, "unknown");
        ws.add(new DefaultResourceLocation<FeatureStore>(new File(storeFile), identifier));
        ws.prepare(identifier);
        FeatureStore store = ws.init(identifier, null);

        AppSchema schema = store.getSchema();

        // prepare document
        out.writeStartDocument();
        out.writeStartElement(ns, "deegreeWMS");
        out.writeDefaultNamespace(ns);
        out.writeAttribute("configVersion", "0.5.0");
        out.writeNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
        out.writeAttribute("http://www.w3.org/2001/XMLSchema-instance", "schemaLocation",
                "http://www.deegree.org/services/wms http://schemas.deegree.org/wms/0.5.0/wms_configuration.xsd");
        out.writeStartElement(ns, "ServiceConfiguration");

        HashSet<FeatureType> visited = new HashSet<FeatureType>();

        if (schema.getRootFeatureTypes().length == 1) {
            writeLayer(visited, out, schema.getRootFeatureTypes()[0], storeId);
        } else {
            out.writeCharacters("\n");
            out.writeStartElement(ns, "UnrequestableLayer");
            XMLAdapter.writeElement(out, ns, "Title", "Root Layer");
            for (FeatureType ft : schema.getRootFeatureTypes()) {
                writeLayer(visited, out, ft, storeId);
            }
            out.writeEndElement();
            out.writeCharacters("\n");
        }

        out.writeEndElement();
        out.writeEndElement();
        out.writeEndDocument();
    } catch (ParseException exp) {
        System.err.println(Messages.getMessage("TOOL_COMMANDLINE_ERROR", exp.getMessage()));
        CommandUtils.printHelp(options, FeatureTypesToLayerTree.class.getSimpleName(), null, null);
    } catch (ResourceInitException e) {
        LOG.info("The feature store could not be loaded: '{}'", e.getLocalizedMessage());
        LOG.trace("Stack trace:", e);
    } catch (FileNotFoundException e) {
        LOG.info("A file could not be found: '{}'", e.getLocalizedMessage());
        LOG.trace("Stack trace:", e);
    } catch (XMLStreamException e) {
        LOG.info("The XML output could not be written: '{}'", e.getLocalizedMessage());
        LOG.trace("Stack trace:", e);
    } catch (FactoryConfigurationError e) {
        LOG.info("The XML system could not be initialized: '{}'", e.getLocalizedMessage());
        LOG.trace("Stack trace:", e);
    } finally {
        if (out != null) {
            try {
                out.close();
            } catch (XMLStreamException e) {
                LOG.trace("Stack trace:", e);
            }
        }
    }

}

From source file:org.eclipse.gyrex.logback.config.model.Appender.java

@Override
public void toXml(final XMLStreamWriter writer) throws XMLStreamException {
    final boolean wrapIntoSiftingAppender = canSift() && isSeparateLogOutputsPerMdcProperty();

    writer.writeStartElement("appender");
    writer.writeAttribute("name", getName());
    if (wrapIntoSiftingAppender) {
        writer.writeAttribute("class", SiftingAppender.class.getName());
    } else {//from   ww  w.  j a va  2s .  c  om
        writer.writeAttribute("class", getAppenderClassName());
    }

    final Level threshold = getThreshold();
    if (null != threshold) {
        writer.writeStartElement("filter");
        writer.writeAttribute("class", ThresholdFilter.class.getName());
        writer.writeStartElement("level");
        writer.writeCharacters(threshold.toString());
        writer.writeEndElement();
        writer.writeEndElement();
    }

    if (wrapIntoSiftingAppender) {
        // start discriminator and wrap regular appender into <sift><appender>
        writer.writeStartElement("discriminator");
        writer.writeAttribute("class", MDCBasedDiscriminator.class.getName());
        writer.writeStartElement("key");
        writer.writeCharacters(getSiftingMdcPropertyName());
        writer.writeEndElement();
        writer.writeStartElement("defaultValue");
        writer.writeCharacters(getSiftingMdcPropertyDefaultValue());
        writer.writeEndElement();
        writer.writeEndElement();
        writer.writeStartElement("sift");
        writer.writeStartElement("appender");
        writer.writeAttribute("name", String.format("%s-${%s}", getName(), getSiftingMdcPropertyName()));
    }

    writeAppenderContent(writer);
    writeEncoder(writer);

    if (wrapIntoSiftingAppender) {
        // finish <sift><appender>
        writer.writeEndElement();
        writer.writeEndElement();
    }
    writer.writeEndElement();

}

From source file:org.eclipse.gyrex.logback.config.model.Appender.java

private void writeEncoder(final XMLStreamWriter writer) throws XMLStreamException {
    writer.writeStartElement("encoder");
    writer.writeStartElement("pattern");
    String text = getPattern();/*w  w  w  .ja  v  a  2s . c o m*/
    if (StringUtils.isBlank(text)) {
        text = preferShortPattern() ? "${PATTERN_SHORT}" : "${PATTERN_LONG}";
    } else if (StringUtils.containsIgnoreCase(text, "PATTERN_LONG")) {
        text = "${PATTERN_LONG}";
    } else if (StringUtils.containsIgnoreCase(text, "PATTERN_SHORT")) {
        text = "${PATTERN_SHORT}";
    }
    writer.writeCharacters(text);
    writer.writeEndElement();
    writer.writeEndElement();
}

From source file:org.eclipse.gyrex.logback.config.model.FileAppender.java

@Override
protected void writeAppenderContent(final XMLStreamWriter writer) throws XMLStreamException {
    writer.writeStartElement("file");
    writer.writeCharacters(String.format("${BASE_PATH}/%s", getFileName()));
    writer.writeEndElement();// w w  w  .ja v a 2s .  c om
    writeRotation(writer);
}