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.apache.chemistry.opencmis.server.impl.atompub.AtomDocumentBase.java

/**
 * Writes a simple tag.//from w  w w .  j a v  a  2s .com
 */
public void writeSimpleTag(String namespace, String name, String value) throws XMLStreamException {
    if (value == null) {
        return;
    }

    XMLStreamWriter xsw = getWriter();

    xsw.writeStartElement(namespace, name);
    xsw.writeCharacters(value);
    xsw.writeEndElement();
}

From source file:org.apache.chemistry.opencmis.server.impl.atompub.AtomDocumentBase.java

/**
 * Writes an Atom collection./*from w w  w. j a va2 s  .  c  om*/
 */
public void writeCollection(String href, String collectionType, String text, String... accept)
        throws XMLStreamException {
    XMLStreamWriter xsw = getWriter();

    xsw.writeStartElement(Constants.NAMESPACE_APP, "collection");
    xsw.writeAttribute("href", href);

    if (collectionType != null) {
        xsw.writeStartElement(Constants.NAMESPACE_RESTATOM, "collectionType");
        xsw.writeCharacters(collectionType);
        xsw.writeEndElement();
    }

    xsw.writeStartElement(Constants.NAMESPACE_ATOM, "title");
    xsw.writeAttribute("type", "text");
    xsw.writeCharacters(text);
    xsw.writeEndElement();

    for (String ct : accept) {
        xsw.writeStartElement(Constants.NAMESPACE_APP, "accept");
        xsw.writeCharacters(ct);
        xsw.writeEndElement();
    }

    xsw.writeEndElement();
}

From source file:org.apache.flex.compiler.config.Configuration.java

/**
 * @return Metadata XML string.//w  ww. java  2 s .com
 */
private final String generateMetadata() {
    final XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newInstance();
    assert xmlOutputFactory != null : "Expect XMLOutputFactory implementation.";
    final StringWriter stringWriter = new StringWriter();
    XMLStreamWriter xmlWriter = null;

    try {
        xmlWriter = new XMLFormatter(xmlOutputFactory.createXMLStreamWriter(stringWriter));
        xmlWriter.writeStartDocument();

        xmlWriter.writeStartElement("rdf", "RDF", RDF_URI);
        xmlWriter.setPrefix("rdf", RDF_URI);
        xmlWriter.writeNamespace("rdf", RDF_URI);

        // write rdf:Description
        xmlWriter.writeStartElement(RDF_URI, "Description");
        xmlWriter.setPrefix("dc", DC_URI);
        xmlWriter.setPrefix(VersionInfo.COMPILER_NAMESPACE_PREFIX, VersionInfo.COMPILER_NAMESPACE_URI);
        xmlWriter.writeNamespace("dc", DC_URI);
        xmlWriter.writeNamespace(VersionInfo.COMPILER_NAMESPACE_PREFIX, VersionInfo.COMPILER_NAMESPACE_URI);

        // write dc:format
        xmlWriter.writeStartElement(DC_URI, "format");
        xmlWriter.writeCharacters("application/x-shockwave-flash");
        xmlWriter.writeEndElement();

        if (isFlex()) {
            // write localizedTitles
            writeMap(xmlWriter, DC_URI, "description", localizedDescriptions);

            // write localizedDescription
            writeMap(xmlWriter, DC_URI, "title", localizedTitles);

            // write publisher
            writeCollection(xmlWriter, DC_URI, "publisher", publishers);

            // write creators
            writeCollection(xmlWriter, DC_URI, "creator", creators);

            // write contributor
            writeCollection(xmlWriter, DC_URI, "contributor", contributors);

            // write language
            writeCollection(xmlWriter, DC_URI, "language", langs);

            // write date
            writeDate(xmlWriter);
        }

        // write compiledBy
        writeCompiledBy(xmlWriter);

        // write
        xmlWriter.writeEndElement(); // Description
        xmlWriter.writeEndDocument();
    } catch (XMLStreamException e) {
        return "";
    }

    return stringWriter.toString();
}

From source file:org.apache.flex.compiler.config.Configuration.java

/**
 * Write the data to rdf/xml/*  w  w w  .j  av a 2  s .c  o  m*/
 * 
 * @param writer
 * @throws XMLStreamException
 */
private void writeDate(XMLStreamWriter writer) throws XMLStreamException {
    if (date == null) {
        date = DateFormat.getDateInstance().format(new Date());
    }

    writer.writeStartElement(DC_URI, "date");
    writer.writeCharacters(date);
    writer.writeEndElement();
}

From source file:org.apache.flex.compiler.config.Configuration.java

/**
 * Write a map of values to rdf/xml/*from ww w  .j ava  2 s .c  o  m*/
 * 
 * @param writer
 * @param namespaceURI
 * @param localName
 * @param mapData
 * @throws XMLStreamException
 */
private void writeMap(XMLStreamWriter writer, String namespaceURI, String localName,
        Map<String, String> mapData) throws XMLStreamException {
    if (mapData.size() > 0) {
        writer.writeStartElement(namespaceURI, localName);
        if ((mapData.size() == 1) && (mapData.get("x-default") != null)) {
            String data = mapData.get("x-default");
            writer.writeCharacters(data);
        } else {
            writer.writeStartElement(RDF_URI, "Alt");
            for (final String key : mapData.keySet()) {
                final String value = mapData.get(key);
                writer.writeStartElement(RDF_URI, "li");
                writer.writeAttribute("xml", "", "lang", key);
                writer.writeCharacters(value);
                writer.writeEndElement();
            }
            writer.writeEndElement();
        }

        writer.writeEndElement();
    }

}

From source file:org.apache.flex.compiler.config.Configuration.java

/**
 * Write a collection values to rdf/xml.
 * /*from  w  ww  .j a va 2s. c o  m*/
 * @param writer
 * @param namespaceURI
 * @param localName
 * @param values
 * @throws XMLStreamException
 */
private void writeCollection(XMLStreamWriter writer, String namespaceURI, String localName,
        Collection<String> values) throws XMLStreamException {
    if (values.isEmpty())
        return;

    writer.writeStartElement(namespaceURI, localName);

    if (values.size() > 1)
        writer.writeStartElement(RDF_URI, "Bag");

    for (String value : values) {
        writer.writeCharacters(value);
    }

    if (values.size() > 1)
        writer.writeEndElement();

    writer.writeEndElement();

}

From source file:org.apache.nifi.authorization.StandardManagedAuthorizer.java

@Override
public String getFingerprint() throws AuthorizationAccessException {
    XMLStreamWriter writer = null;
    final StringWriter out = new StringWriter();
    try {//from  w  ww  .  j  av  a  2  s. c  om
        writer = XML_OUTPUT_FACTORY.createXMLStreamWriter(out);
        writer.writeStartDocument();
        writer.writeStartElement("managedAuthorizations");

        writer.writeStartElement(ACCESS_POLICY_PROVIDER_ELEMENT);
        if (accessPolicyProvider instanceof ConfigurableAccessPolicyProvider) {
            writer.writeCharacters(((ConfigurableAccessPolicyProvider) accessPolicyProvider).getFingerprint());
        }
        writer.writeEndElement();

        writer.writeStartElement(USER_GROUP_PROVIDER_ELEMENT);
        if (userGroupProvider instanceof ConfigurableUserGroupProvider) {
            writer.writeCharacters(((ConfigurableUserGroupProvider) userGroupProvider).getFingerprint());
        }
        writer.writeEndElement();

        writer.writeEndElement();
        writer.writeEndDocument();
        writer.flush();
    } catch (XMLStreamException e) {
        throw new AuthorizationAccessException("Unable to generate fingerprint", e);
    } finally {
        if (writer != null) {
            try {
                writer.close();
            } catch (XMLStreamException e) {
                // nothing to do here
            }
        }
    }

    return out.toString();
}

From source file:org.apache.olingo.client.core.serialization.AtomSerializer.java

private void value(final XMLStreamWriter writer, final ValueType valueType, final EdmPrimitiveTypeKind kind,
        final Object value) throws XMLStreamException, EdmPrimitiveTypeException {
    if (value == null || (valueType == ValueType.COMPLEX && ((ComplexValue) value).getValue().isEmpty())) {
        writer.writeAttribute(Constants.PREFIX_METADATA, namespaceMetadata, Constants.ATTR_NULL,
                Boolean.TRUE.toString());
        return;// ww w .j a v a  2  s. co  m
    }
    switch (valueType) {
    case PRIMITIVE:
        writer.writeCharacters(kind == null ? value.toString()
                : EdmPrimitiveTypeFactory.getInstance(kind) // TODO: add facets
                        .valueToString(value, null, null, Constants.DEFAULT_PRECISION, Constants.DEFAULT_SCALE,
                                null));
        break;
    case ENUM:
        writer.writeCharacters(value.toString());
        break;
    case GEOSPATIAL:
        geoSerializer.serialize(writer, (Geospatial) value);
        break;
    case COLLECTION_PRIMITIVE:
    case COLLECTION_GEOSPATIAL:
    case COLLECTION_ENUM:
    case COLLECTION_COMPLEX:
        collection(writer, valueType.getBaseType(), kind, (List<?>) value);
        break;
    case COMPLEX:
        for (Property property : ((ComplexValue) value).getValue()) {
            property(writer, property, false);
        }
        break;
    case ENTITY:
    case COLLECTION_ENTITY:
        throw new ODataRuntimeException("Entities cannot appear in this payload");
    }
}

From source file:org.apache.olingo.client.core.serialization.AtomSerializer.java

private void common(final XMLStreamWriter writer, final AbstractODataObject object) throws XMLStreamException {
    if (StringUtils.isNotBlank(object.getTitle())) {
        writer.writeStartElement(Constants.ATOM_ELEM_TITLE);
        writer.writeAttribute(Constants.ATTR_TYPE, TYPE_TEXT);
        writer.writeCharacters(object.getTitle());
        writer.writeEndElement();//from  w  w  w  .j  ava2s . c  o m
    }
}

From source file:org.apache.olingo.client.core.serialization.AtomSerializer.java

private void entity(final XMLStreamWriter writer, final Entity entity)
        throws XMLStreamException, EdmPrimitiveTypeException {
    if (entity.getBaseURI() != null) {
        writer.writeAttribute(XMLConstants.XML_NS_URI, Constants.ATTR_XML_BASE,
                entity.getBaseURI().toASCIIString());
    }/*from  ww w .j  ava2s.  co m*/

    if (serverMode && StringUtils.isNotBlank(entity.getETag())) {
        writer.writeAttribute(namespaceMetadata, Constants.ATOM_ATTR_ETAG, entity.getETag());
    }

    if (entity.getId() != null) {
        writer.writeStartElement(Constants.ATOM_ELEM_ID);
        writer.writeCharacters(entity.getId().toASCIIString());
        writer.writeEndElement();
    }

    writer.writeStartElement(Constants.ATOM_ELEM_CATEGORY);
    writer.writeAttribute(Constants.ATOM_ATTR_SCHEME, Constants.NS_SCHEME);
    if (StringUtils.isNotBlank(entity.getType())) {
        writer.writeAttribute(Constants.ATOM_ATTR_TERM,
                new EdmTypeInfo.Builder().setTypeExpression(entity.getType()).build().external());
    }
    writer.writeEndElement();

    common(writer, entity);

    if (serverMode) {
        if (entity.getEditLink() != null) {
            links(writer, Collections.singletonList(entity.getEditLink()));
        }

        if (entity.getSelfLink() != null) {
            links(writer, Collections.singletonList(entity.getSelfLink()));
        }
    }

    links(writer, entity.getAssociationLinks());
    if (serverMode) {
        links(writer, entity.getNavigationLinks());
    } else {
        writeNavigationLinks(writer, entity.getNavigationLinks());
        writeNavigationLinks(writer, entity.getNavigationBindings());
    }
    links(writer, entity.getMediaEditLinks());

    if (serverMode) {
        for (Operation operation : entity.getOperations()) {
            writer.writeStartElement(namespaceMetadata, Constants.ATOM_ELEM_ACTION);
            writer.writeAttribute(Constants.ATTR_METADATA, operation.getMetadataAnchor());
            writer.writeAttribute(Constants.ATTR_TITLE, operation.getTitle());
            writer.writeAttribute(Constants.ATTR_TARGET, operation.getTarget().toASCIIString());
            writer.writeEndElement();
        }
    }

    writer.writeStartElement(Constants.ATOM_ELEM_CONTENT);
    if (entity.isMediaEntity()) {
        if (StringUtils.isNotBlank(entity.getMediaContentType())) {
            writer.writeAttribute(Constants.ATTR_TYPE, entity.getMediaContentType());
        }
        if (entity.getMediaContentSource() != null) {
            writer.writeAttribute(Constants.ATOM_ATTR_SRC, entity.getMediaContentSource().toASCIIString());
        }
        writer.writeEndElement();

        writer.writeStartElement(namespaceMetadata, Constants.PROPERTIES);
        properties(writer, entity.getProperties());
    } else {
        writer.writeAttribute(Constants.ATTR_TYPE, ContentType.APPLICATION_XML.toContentTypeString());
        writer.writeStartElement(namespaceMetadata, Constants.PROPERTIES);
        properties(writer, entity.getProperties());
        writer.writeEndElement();
    }
    writer.writeEndElement();

    for (Annotation annotation : entity.getAnnotations()) {
        annotation(writer, annotation, null);
    }
}