Example usage for javax.xml.stream XMLStreamConstants CDATA

List of usage examples for javax.xml.stream XMLStreamConstants CDATA

Introduction

In this page you can find the example usage for javax.xml.stream XMLStreamConstants CDATA.

Prototype

int CDATA

To view the source code for javax.xml.stream XMLStreamConstants CDATA.

Click Source Link

Document

Indicates an event is a CDATA section

Usage

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;//  w w w . jav a  2  s .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.wso2.carbon.inbound.endpoint.protocol.hl7.util.HL7MessageUtils.java

public static OMElement generateHL7RawMessaegElement(String hl7XmlMessage) {
    OMElement hl7Element = omFactory.createOMElement(new QName("rawMessage"));
    OMText rawMessage = hl7Element.getOMFactory().createOMText(hl7Element, hl7XmlMessage,
            XMLStreamConstants.CDATA);
    hl7Element.addChild(rawMessage);
    OMElement messageEl = fac.createOMElement(Axis2HL7Constants.HL7_MESSAGE_ELEMENT_NAME, ns);
    messageEl.addChild(hl7Element);
    return messageEl;
}

From source file:org.wso2.carbon.mediation.initializer.multitenancy.StratosEntrySerialiser.java

public OMElement serializeEntry(Entry entry, OMElement parent) {
    OMElement entryElement = fac.createOMElement("localEntry", synNS);

    if (entry.getDescription() != null) {
        OMElement descriptionElem = fac/* www  . jav a  2 s.  c  o  m*/
                .createOMElement(new QName(SynapseConstants.SYNAPSE_NAMESPACE, "description"));
        descriptionElem.setText(entry.getDescription());
        entryElement.addChild(descriptionElem);
    }

    entryElement.addAttribute(fac.createOMAttribute("key", nullNS, entry.getKey().trim()));
    int type = entry.getType();
    if (type == Entry.URL_SRC) {
        URL srcUrl = entry.getSrc();
        if (srcUrl != null && !"file".equals(srcUrl.getProtocol())) {
            entryElement.addAttribute(fac.createOMAttribute("src", nullNS, srcUrl.toString().trim()));
        } else if (srcUrl != null) {
            handleException(
                    "Invalid LocalEntry. Read blocked for local file system with path : " + srcUrl.getPath());
        }
    } else if (type == Entry.INLINE_XML) {
        Object value = entry.getValue();
        if (value != null && value instanceof OMElement) {
            entryElement.addChild((OMElement) value);
        }
    } else if (type == Entry.INLINE_TEXT) {
        Object value = entry.getValue();
        if (value != null && value instanceof String) {
            OMTextImpl textData = (OMTextImpl) fac.createOMText(((String) value).trim());
            textData.setType(XMLStreamConstants.CDATA);
            entryElement.addChild(textData);
        }
    } else if (type == Entry.REMOTE_ENTRY) {
        // nothing to serialize
        return null;
    } else {
        handleException("Entry type undefined");
    }

    if (parent != null) {
        parent.addChild(entryElement);
    }
    return entryElement;
}

From source file:org.wso2.codegen.service.wsdl2code.WSDL2Code.java

/**
 * uploadFileValue element will be generated for given codegen option and value.
 *
 * @param codegenOption      codegen option that need to be fill with
 * @param codegenOptionValue value//w  w w  . j  a v  a 2  s .  c  o m
 * @return OMElement return
 * @throws AxisFault with be thrown
 */
public OMElement getCodegenOptionsWithValues(String codegenOption, String codegenOptionValue) throws AxisFault {
    OMElement docEle = loadCodegenOptions();
    try {
        XPath xp = new AXIOMXPath("/codegen/argument[@name='" + codegenOption + "']");
        OMElement nameEle = (OMElement) xp.selectSingleNode(docEle);
        if (nameEle != null) {
            OMFactory fac = OMAbstractFactory.getOMFactory();
            OMElement uploadFileValueEle = fac.createOMElement(new QName("uploadFileValue"));
            nameEle.addChild(uploadFileValueEle);
            Map fileResourcesMap = (Map) MessageContext.getCurrentMessageContext().getConfigurationContext()
                    .getProperty(WSO2Constants.FILE_RESOURCE_MAP);
            if (fileResourcesMap == null) {
                String msg = "File resource is not available";
                log.error(msg);
                throw new AxisFault(msg);
            }
            String absFilePath = (String) fileResourcesMap.get(codegenOptionValue);
            if (absFilePath == null) {
                String msg = "Uploaded file is not available";
                log.error(msg);
                throw new AxisFault(msg);
            }
            OMText omText = fac.createOMText(absFilePath, XMLStreamConstants.CDATA);
            uploadFileValueEle.addChild(omText);
        }
        return nameEle;
    } catch (JaxenException e) {
        String msg = "Xpath error has been encounted while looking for the argument : " + codegenOption;
        log.error(msg, e);
        throw new AxisFault(msg, e);
    }
}