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.osaf.cosmo.model.text.XhtmlTicketFormat.java

public String format(Ticket ticket) {
    try {//from  w ww. j  av  a  2s .c o  m
        StringWriter sw = new StringWriter();
        XMLStreamWriter writer = createXmlWriter(sw);

        writer.writeStartElement("div");
        writer.writeAttribute("class", "ticket");

        if (ticket.getKey() != null) {
            writer.writeCharacters("Key: ");
            writer.writeStartElement("span");
            writer.writeAttribute("class", "key");
            writer.writeCharacters(ticket.getKey());
            writer.writeEndElement();
        }

        if (ticket.getType() != null) {
            writer.writeCharacters("Type: ");
            writer.writeStartElement("span");
            writer.writeAttribute("class", "type");
            writer.writeAttribute("title", ticket.getType().toString());
            writer.writeCharacters(ticket.getType().toString());
            writer.writeEndElement();
        }

        if (ticket.getTimeout() != null) {
            writer.writeCharacters("Timeout: ");
            writer.writeStartElement("span");
            writer.writeAttribute("class", "timeout");
            writer.writeAttribute("title", ticket.getTimeout().toString());
            writer.writeCharacters(ticket.getTimeout().toString());
            writer.writeEndElement();
        }

        writer.writeEndElement();
        writer.close();

        return sw.toString();
    } catch (XMLStreamException e) {
        throw new RuntimeException("Error formatting XML", e);
    }
}

From source file:org.osaf.cosmo.xml.DomWriter.java

private static void writeCharacters(CharacterData cd, XMLStreamWriter writer) throws XMLStreamException {
    //if (log.isDebugEnabled())
    //log.debug("Writing characters: '" + cd.getData() + "'");
    writer.writeCharacters(cd.getData());
}

From source file:org.plasma.sdo.xml.StreamMarshaller.java

private void writeContent(XMLStreamWriter writer, DataObject dataObject, DataObject source,
        Property sourceProperty, PlasmaType targetType, PlasmaType sourceType, int level)
        throws IOException, XMLStreamException {

    int externKeyCount = 0;
    for (Property property : targetType.getProperties()) {
        PlasmaProperty prop = (PlasmaProperty) property;
        if (prop.isKey(KeyType.external)) {
            externKeyCount++;/*w  w w .  java 2  s  .co m*/
        }
    }

    // create XSI type on demand for containment refs
    // FIXME: SDO namespaces are necessary in some cases
    // to determine exact XSI type to unmarshal. Can't determine
    // this from the property type on unmarshalling. 
    if (externKeyCount > 0)
        writer.writeAttribute("xsi", XMLConstants.XMLSCHEMA_INSTANCE_NAMESPACE_URI, "type",
                namespacePrefix + ":" + SchemaUtil.getContainmentReferenceName(targetType));

    writer.writeAttribute(SchemaUtil.getSerializationAttributeName(),
            ((PlasmaDataObject) dataObject).getUUIDAsString());

    for (Property property : targetType.getProperties()) {
        PlasmaProperty prop = (PlasmaProperty) property;
        if (!prop.getType().isDataType() || !prop.isXMLAttribute()) {
            continue;
        }
        // FIXME - what about pri-keys which are not sequences
        //VisibilityKind visibility = (VisibilityKind)prop.get(PlasmaProperty.INSTANCE_PROPERTY_OBJECT_VISIBILITY);
        //if (visibility != null && visibility.ordinal() == VisibilityKind.private_.ordinal())
        //   continue; // for properties defined as private no XML or XML Schema property generated
        Object value = dataObject.get(prop);
        if (value == null)
            continue;
        writer.writeAttribute(helper.getLocalName(prop), fromObject(prop.getType(), value));
    }

    // add element properties
    List<Property> list = targetType.getProperties();
    PlasmaProperty[] properties = new PlasmaProperty[list.size()];
    list.toArray(properties);
    Arrays.sort(properties, this.comparator);
    for (Property property : properties) {
        PlasmaProperty prop = (PlasmaProperty) property;
        if (!prop.getType().isDataType() || prop.isXMLAttribute())
            continue;
        // FIXME - what about pri-keys which are not sequences
        //VisibilityKind visibility = (VisibilityKind)prop.get(PlasmaProperty.INSTANCE_PROPERTY_OBJECT_VISIBILITY);
        //if (visibility != null && visibility.ordinal() == VisibilityKind.private_.ordinal())
        //   continue; // for properties defined as private no XML or XML Schema property generated
        Object value = dataObject.get(prop);
        if (value == null)
            continue;
        writer.writeStartElement(helper.getLocalName(prop));
        writer.writeCharacters(this.fromObject(prop.getType(), value));
        writer.writeEndElement();
    }
}

From source file:org.qi4j.valueserialization.stax.StaxValueSerializer.java

@Override
protected void onFieldStart(XMLStreamWriter output, String key) throws Exception {
    output.writeStartElement("field");
    output.writeStartElement("name");
    output.writeCharacters(key);
    output.writeEndElement();//  w w  w.jav a  2 s  .c o  m
}

From source file:org.qi4j.valueserialization.stax.StaxValueSerializer.java

@Override
protected void onValue(XMLStreamWriter output, Object value) throws Exception {
    if (value == null) {
        output.writeStartElement("null");
        output.writeEndElement();//  w  w  w. java 2s  . c o  m
    } else {
        output.writeCharacters(StringEscapeUtils.escapeXml(value.toString()));
    }
}

From source file:org.rhq.enterprise.server.sync.ExportingInputStream.java

/**
 * @param wrt// w  w  w . j ava 2  s.c o m
 * @param syn
 * @return
 * @throws XMLStreamException 
 */
private void exportSingle(XMLStreamWriter wrt, Synchronizer<?, ?> syn) throws XMLStreamException {
    ExporterMessages messages = new ExporterMessages();

    messagesPerExporter.put(syn.getClass().getName(), messages);

    wrt.writeStartElement(SynchronizationConstants.EXPORT_NAMESPACE,
            SynchronizationConstants.ENTITIES_EXPORT_ELEMENT);
    wrt.writeAttribute(SynchronizationConstants.ID_ATTRIBUTE, syn.getClass().getName());

    Exporter<?, ?> exp = syn.getExporter();
    ExportingIterator<?> it = exp.getExportingIterator();

    DefaultImportConfigurationDescriptor importConfig = getDefaultImportConfiguraton(syn);

    if (importConfig != null) {
        try {
            configurationMarshaller.marshal(importConfig, wrt);
        } catch (JAXBException e) {
            throw new XMLStreamException(e);
        }
    }

    messages.setPerEntityErrorMessages(new ArrayList<String>());
    messages.setPerEntityNotes(new ArrayList<String>());

    while (it.hasNext()) {
        it.next();

        wrt.writeStartElement(SynchronizationConstants.EXPORT_NAMESPACE,
                SynchronizationConstants.ENTITY_EXPORT_ELEMENT);

        wrt.writeStartElement(SynchronizationConstants.EXPORT_NAMESPACE, SynchronizationConstants.DATA_ELEMENT);

        Exception exportError = null;
        try {
            it.export(new ExportWriter(wrt));
        } catch (XMLStreamException e) {
            //there's not much we can do about these but to give up.
            throw e;
        } catch (Exception e) {
            exportError = e;
        }

        wrt.writeEndElement(); //data

        if (exportError == null) {
            String notes = it.getNotes();

            if (notes != null) {
                messages.getPerEntityNotes().add(notes);
                wrt.writeStartElement(SynchronizationConstants.EXPORT_NAMESPACE,
                        SynchronizationConstants.NOTES_ELEMENT);
                wrt.writeCharacters(notes);
                wrt.writeEndElement();
            }
        } else {
            String message = ThrowableUtil.getStackAsString(exportError);
            messages.getPerEntityErrorMessages().add(message);
            wrt.writeStartElement(SynchronizationConstants.EXPORT_NAMESPACE,
                    SynchronizationConstants.ERROR_MESSAGE_ELEMENT);
            wrt.writeCharacters(message);
            wrt.writeEndElement();
        }

        wrt.writeEndElement(); //entity
    }

    String notes = exp.getNotes();

    messages.setExporterNotes(notes);

    if (notes != null) {
        wrt.writeStartElement(SynchronizationConstants.EXPORT_NAMESPACE,
                SynchronizationConstants.NOTES_ELEMENT);
        wrt.writeCharacters(notes);
        wrt.writeEndElement();
    }

    wrt.writeEndElement(); //entities
}

From source file:org.rhq.plugins.hadoop.HadoopServerConfigurationDelegate.java

private static void updateFile(File configFile, Map<String, PropertySimple> allProps)
        throws IOException, InterruptedException, XMLStreamException {
    InputStream in = null;/*from   ww  w .  j  a  v a  2s . c om*/
    XMLStreamReader rdr = null;

    OutputStream out = null;
    XMLStreamWriter outWrt = null;

    try {
        Set<String> processedPropertyNames = new HashSet<String>();

        in = new BufferedInputStream(new FileInputStream(configFile));
        rdr = XML_INPUT_FACTORY.createXMLStreamReader(in);

        File tmpFile = File.createTempFile("hadoop-plugin", null);
        out = new FileOutputStream(tmpFile);
        outWrt = XML_OUTPUT_FACTORY.createXMLStreamWriter(out);

        ByteArrayOutputStream stash = new ByteArrayOutputStream();
        XMLStreamWriter stashWrt = XML_OUTPUT_FACTORY.createXMLStreamWriter(stash);
        boolean outputActive = true;

        outWrt.writeStartDocument();

        while (rdr.hasNext()) {
            int event = rdr.next();

            XMLStreamWriter wrt = outputActive ? outWrt : stashWrt;

            switch (event) {
            case XMLStreamConstants.ATTRIBUTE:
                break;
            case XMLStreamConstants.CDATA:
                wrt.writeCData(rdr.getText());
                break;
            case XMLStreamConstants.CHARACTERS:
                wrt.writeCharacters(rdr.getText());
                break;
            case XMLStreamConstants.COMMENT:
                wrt.writeComment(rdr.getText());
                break;
            case XMLStreamConstants.DTD:
                wrt.writeDTD(rdr.getText());
                break;
            case XMLStreamConstants.END_DOCUMENT:
                wrt.writeEndDocument();
                break;
            case XMLStreamConstants.END_ELEMENT:
                if (PROPERTY_TAG_NAME.equals(rdr.getName().getLocalPart())) {
                    String encoding = rdr.getEncoding();
                    if (encoding == null) {
                        encoding = "UTF-8";
                    }

                    String propertyTagSoFar = Charset.forName(encoding)
                            .decode(ByteBuffer.wrap(stash.toByteArray())).toString();
                    DetectedPropertyNameAndUpdatedTag propAndTag = updateProperty(propertyTagSoFar, allProps);

                    //yes, we're intentionally circumventing the xml stream writer, because we already have the XML data we want to write.
                    outWrt.flush();
                    out.write(propAndTag.updatedTag.getBytes("UTF-8"));

                    processedPropertyNames.add(propAndTag.propertyName);

                    //reset stuff
                    stash.reset();
                    wrt = outWrt;
                    outputActive = true;
                } else if (CONFIGURATION_TAG_NAME.equals(rdr.getName().getLocalPart())) {
                    //now add the new props
                    for (String prop : processedPropertyNames) {
                        allProps.remove(prop);
                    }

                    for (Map.Entry<String, PropertySimple> e : allProps.entrySet()) {
                        outWrt.writeStartElement(PROPERTY_TAG_NAME);

                        outWrt.writeStartElement(NAME_TAG_NAME);
                        outWrt.writeCharacters(e.getKey());
                        outWrt.writeEndElement();

                        outWrt.writeStartElement(VALUE_TAG_NAME);
                        outWrt.writeCharacters(e.getValue().getStringValue());
                        outWrt.writeEndElement();

                        outWrt.writeEndElement();
                    }
                }
                wrt.writeEndElement();
                break;
            case XMLStreamConstants.ENTITY_DECLARATION:
                //XXX could not find what to do with this
                break;
            case XMLStreamConstants.ENTITY_REFERENCE:
                wrt.writeEntityRef(rdr.getText());
                break;
            case XMLStreamConstants.NAMESPACE:
                for (int i = 0; i < rdr.getNamespaceCount(); ++i) {
                    wrt.writeNamespace(rdr.getNamespacePrefix(i), rdr.getNamespaceURI(i));
                }
                break;
            case XMLStreamConstants.NOTATION_DECLARATION:
                //XXX could not find what to do with this
                break;
            case XMLStreamConstants.PROCESSING_INSTRUCTION:
                wrt.writeProcessingInstruction(rdr.getPITarget(), rdr.getPIData());
                break;
            case XMLStreamConstants.SPACE:
                wrt.writeCharacters(rdr.getText());
                break;
            case XMLStreamConstants.START_DOCUMENT:
                //this seems to be never called for some strange reason
                //wrt.writeStartDocument();
                break;
            case XMLStreamConstants.START_ELEMENT:
                wrt.writeStartElement(rdr.getName().getPrefix(), rdr.getName().getLocalPart(),
                        rdr.getName().getNamespaceURI());

                for (int i = 0; i < rdr.getAttributeCount(); ++i) {
                    wrt.writeAttribute(rdr.getAttributePrefix(i), rdr.getAttributeNamespace(i),
                            rdr.getAttributeLocalName(i), rdr.getAttributeValue(i));
                }

                if (PROPERTY_TAG_NAME.equals(rdr.getName().getLocalPart())) {
                    wrt.writeCharacters("");
                    outputActive = false;
                }
                break;
            }
        }

        outWrt.flush();
        out.flush();
        out.close();

        in.close();

        //now copy the temp file in the place of the original one
        FileUtil.copyFile(tmpFile, configFile);
    } finally {
        rdr.close();

        outWrt.flush();
        outWrt.close();

        try {
            in.close();
        } finally {
            out.flush();
            out.close();
        }
    }
}

From source file:org.simbasecurity.core.saml.SAMLServiceImpl.java

@Override
public String createAuthRequest(String authRequestId, Date issueInstant)
        throws XMLStreamException, IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    XMLOutputFactory factory = XMLOutputFactory.newInstance();
    XMLStreamWriter writer = factory.createXMLStreamWriter(baos);

    writer.writeStartElement("samlp", "AuthnRequest", NS_SAMLP);
    writer.writeNamespace("samlp", NS_SAMLP);

    writer.writeAttribute("ID", "_" + authRequestId);
    writer.writeAttribute("Version", "2.0");
    writer.writeAttribute("IssueInstant", SAML_DATE_FORMAT.format(issueInstant));
    writer.writeAttribute("ForceAuthn", "false");
    writer.writeAttribute("IsPassive", "false");
    writer.writeAttribute("ProtocolBinding", BINDING_HTTP_POST);
    writer.writeAttribute("AssertionConsumerServiceURL",
            configurationService.getValue(SimbaConfigurationParameter.SAML_ASSERTION_CONSUMER_SERVICE_URL));

    writer.writeStartElement("saml", "Issuer", NS_SAML);
    writer.writeNamespace("saml", NS_SAML);
    writer.writeCharacters(configurationService.getValue(SimbaConfigurationParameter.SAML_ISSUER));
    writer.writeEndElement();//from w  ww.j a va2s .  c  o m

    writer.writeStartElement("samlp", "NameIDPolicy", NS_SAMLP);

    writer.writeAttribute("Format", NAMEID_TRANSIENT);
    writer.writeAttribute("SPNameQualifier",
            configurationService.getValue(SimbaConfigurationParameter.SAML_ISSUER));
    writer.writeAttribute("AllowCreate", "true");
    writer.writeEndElement();

    writer.writeStartElement("samlp", "RequestedAuthnContext", NS_SAMLP);
    writer.writeNamespace("samlp", NS_SAMLP);
    writer.writeAttribute("Comparison", "exact");

    writer.writeStartElement("saml", "AuthnContextClassRef", NS_SAML);
    writer.writeNamespace("saml", NS_SAML);
    writer.writeCharacters(AC_FAS_EID);
    writer.writeEndElement();

    writer.writeEndElement();
    writer.writeEndElement();
    writer.flush();

    return encodeSAMLRequest(baos.toByteArray());
}

From source file:org.simbasecurity.core.saml.SAMLServiceImpl.java

@Override
public String createLogoutRequest(String logoutRequestId, Date issueInstant, String nameId, String sessionIndex)
        throws XMLStreamException, IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    XMLOutputFactory factory = XMLOutputFactory.newInstance();
    XMLStreamWriter writer = factory.createXMLStreamWriter(baos);

    writer.writeStartElement("samlp", "LogoutRequest", NS_SAMLP);
    writer.writeNamespace("samlp", NS_SAMLP);

    writer.writeAttribute("ID", "_" + logoutRequestId);
    writer.writeAttribute("Version", "2.0");
    writer.writeAttribute("IssueInstant", SAML_DATE_FORMAT.format(issueInstant));

    writer.writeStartElement("saml", "Issuer", NS_SAML);
    writer.writeNamespace("saml", NS_SAML);
    writer.writeCharacters("https://iamapps.belgium.be/");
    writer.writeEndElement();/*  w  w w .ja va 2s  .  c o m*/

    writer.writeStartElement("saml", "NameID", NS_SAML);
    writer.writeNamespace("saml", NS_SAML);
    writer.writeAttribute("NameQualifier",
            configurationService.getValue(SimbaConfigurationParameter.SAML_IDP_SLO_TARGET_URL));
    writer.writeAttribute("SPNameQualifier", "https://iamapps.belgium.be/");
    writer.writeAttribute("Format", NAMEID_TRANSIENT);
    writer.writeCharacters(nameId);
    writer.writeEndElement();

    writer.writeStartElement("samlp", "SessionIndex", NS_SAMLP);
    writer.writeNamespace("saml", NS_SAMLP);
    writer.writeCharacters(sessionIndex);
    writer.writeEndElement();

    writer.writeEndElement();
    writer.flush();

    return encodeSAMLRequest(baos.toByteArray());
}

From source file:org.slc.sli.modeling.tools.xsdgen.Uml2XsdWriter.java

private static final void writeDocumentation(final TaggedValue taggedValue, final XMLStreamWriter xsw)
        throws XMLStreamException {
    writeStartElement(XsdElementName.DOCUMENTATION, xsw);
    try {//from  w w  w.  j a  va2s . c  om
        xsw.writeCharacters(collapseWhitespace(taggedValue.getValue()));
    } finally {
        writeEndElement(xsw);
    }
}