Example usage for javax.xml.stream XMLStreamConstants COMMENT

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

Introduction

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

Prototype

int COMMENT

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

Click Source Link

Document

Indicates an event is a comment

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;//from w  ww  . ja v a2 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.slc.sli.modeling.wadl.reader.WadlReader.java

private static final Documentation readDocumentation(final XMLStreamReader reader) throws XMLStreamException {
    assertName(WadlElementName.DOCUMENTATION, reader);
    final String title = getTitle(reader, null);
    final String lang = getLang(reader, null);
    final List<DmNode> content = new LinkedList<DmNode>();
    while (reader.hasNext()) {
        reader.next();//from  w  w w. j  a v  a2  s.  c  om
        switch (reader.getEventType()) {
        case XMLStreamConstants.START_ELEMENT: {
            content.add(readMixedElement(reader));
            break;
        }
        case XMLStreamConstants.END_ELEMENT: {
            assertName(WadlElementName.DOCUMENTATION, reader);
            return new Documentation(title, lang, content);
        }
        case XMLStreamConstants.CHARACTERS: {
            content.add(new DmText(reader.getText()));
            break;
        }
        case XMLStreamConstants.COMMENT: {
            content.add(new DmComment(reader.getText()));
            break;
        }
        case XMLStreamConstants.PROCESSING_INSTRUCTION: {
            content.add(new DmProcessingInstruction(reader.getPITarget(), reader.getPIData()));
            break;
        }
        default: {
            throw new AssertionError(reader.getEventType());
        }
        }
    }
    throw new AssertionError();
}

From source file:org.slc.sli.modeling.wadl.reader.WadlReader.java

private static final DmElement readMixedElement(final XMLStreamReader reader) throws XMLStreamException {
    final QName name = reader.getName();
    final List<DmNode> children = new LinkedList<DmNode>();
    while (reader.hasNext()) {
        reader.next();/*  w ww  .  j av a  2 s . c o  m*/
        switch (reader.getEventType()) {
        case XMLStreamConstants.START_ELEMENT: {
            children.add(readMixedElement(reader));
            break;
        }
        case XMLStreamConstants.END_ELEMENT: {
            if (name.equals(reader.getName())) {
                return new DmElement(name, children);
            } else {
                throw new AssertionError(reader.getLocalName());
            }
        }
        case XMLStreamConstants.CHARACTERS: {
            // Assume that the element will coalesce text nodes.
            children.add(new DmText(reader.getText()));
            break;
        }
        case XMLStreamConstants.COMMENT: {
            // Ignore for now.
            break;
        }
        default: {
            throw new AssertionError(reader.getEventType());
        }
        }
    }
    throw new AssertionError();
}

From source file:org.slc.sli.modeling.wadl.reader.WadlReader.java

private static final Resource readResource(final XMLStreamReader reader) throws XMLStreamException {
    assertName(WadlElementName.RESOURCE, reader);
    final String id = getId(reader);
    final List<String> type = getStringListType(WadlAttributeName.TYPE, reader);
    final String queryType = getQueryType(reader, WadlSyntax.QUERY_TYPE_APPLICATION_X_WWW_FORM_URLENCODED);
    final String path = getStringAttribute(WadlAttributeName.PATH, null, reader);
    final String resourceClass = getStringAttribute(WadlAttributeName.RESOURCE_CLASS, null, reader);
    final List<Documentation> documentation = new LinkedList<Documentation>();
    final List<Param> params = new LinkedList<Param>();
    final List<Method> methods = new LinkedList<Method>();
    final List<Resource> resources = new LinkedList<Resource>();
    while (reader.hasNext()) {
        reader.next();// w  ww .  ja v a  2s  .  c  om
        switch (reader.getEventType()) {
        case XMLStreamConstants.START_ELEMENT: {
            if (match(WadlElementName.RESOURCE, reader)) {
                resources.add(readResource(reader));
            } else if (match(WadlElementName.DOCUMENTATION, reader)) {
                documentation.add(readDocumentation(reader));
            } else if (match(WadlElementName.PARAM, reader)) {
                params.add(readParam(reader));
            } else if (match(WadlElementName.METHOD, reader)) {
                methods.add(readMethod(reader));
            } else {
                skipForeignNamespaceElement(reader);
            }
            break;
        }
        case XMLStreamConstants.END_ELEMENT: {
            assertName(WadlElementName.RESOURCE, reader);
            return new Resource(id, type, queryType, path, documentation, params, methods, resources,
                    resourceClass);
        }
        case XMLStreamConstants.CHARACTERS:
        case XMLStreamConstants.COMMENT: {
            // Ignore.
            break;
        }
        default: {
            throw new AssertionError(reader.getEventType());
        }
        }
    }
    throw new AssertionError();
}

From source file:org.slc.sli.modeling.wadl.reader.WadlReader.java

/**
 * Skips (recursively) over the element in question. Also useful during development.
 *
 * @param reader/*from   w w  w .jav a 2 s .  c om*/
 *            The StAX {@link XMLStreamReader}.
 */
private static final void skipElementReally(final XMLStreamReader reader) throws XMLStreamException {
    final String localName = reader.getLocalName();
    while (reader.hasNext()) {
        reader.next();
        switch (reader.getEventType()) {
        case XMLStreamConstants.START_ELEMENT: {
            skipElementReally(reader);
            break;
        }
        case XMLStreamConstants.END_ELEMENT: {
            if (localName.equals(reader.getLocalName())) {
                return;
            } else {
                throw new AssertionError(reader.getLocalName());
            }
        }
        case XMLStreamConstants.CHARACTERS:
        case XMLStreamConstants.COMMENT: {
            // Ignore.
            break;
        }
        default: {
            throw new AssertionError(reader.getEventType());
        }
        }
    }
    throw new AssertionError();
}

From source file:org.slc.sli.modeling.xmi.comp.XmiMappingReader.java

private static final XmiFeature readFeature(final QName elementName, final XMLStreamReader reader)
        throws XMLStreamException {
    assertStartElement(reader);//ww  w  .j  a v a 2s. c  o  m
    assertName(elementName, reader);
    String className = null;
    String name = null;
    boolean exists = true;
    boolean classExists = true;
    boolean done = false;
    while (!done && reader.hasNext()) {
        reader.next();
        switch (reader.getEventType()) {
        case XMLStreamConstants.START_ELEMENT: {
            if (match(XmiMappingConstants.NAME, reader)) {
                name = readStringContent(reader.getName(), reader);
            } else if (match(XmiMappingConstants.OWNER_NAME, reader)) {
                className = readStringContent(reader.getName(), reader);
            } else if (match(XmiMappingConstants.EXISTS, reader)) {
                exists = readBooleanContent(reader.getName(), reader, true);
            } else if (match(XmiMappingConstants.OWNER_EXISTS, reader)) {
                classExists = readBooleanContent(reader.getName(), reader, true);
            } else {
                throw new AssertionError(reader.getLocalName());
            }
            break;
        }
        case XMLStreamConstants.END_ELEMENT: {
            assertName(elementName, reader);
            done = true;
            break;
        }
        case XMLStreamConstants.CHARACTERS:
        case XMLStreamConstants.COMMENT: {
            // Ignore.
            break;
        }
        default: {
            throw new AssertionError(reader.getEventType());
        }
        }
    }
    return new XmiFeature(name, exists, className, classExists);
}

From source file:org.slc.sli.modeling.xmi.comp.XmiMappingReader.java

private static final XmiMapping readMapping(final XMLStreamReader reader) throws XMLStreamException {
    assertStartElement(reader);//from  w ww.j a  v a2 s .  c  o m
    assertName(XmiMappingConstants.MAPPING, reader);
    XmiFeature lhs = null;
    XmiFeature rhs = null;
    XmiMappingStatus status = null;
    String comment = null;
    String tracking = "";
    boolean done = false;
    while (!done && reader.hasNext()) {
        reader.next();
        switch (reader.getEventType()) {
        case XMLStreamConstants.START_ELEMENT: {
            if (match(XmiMappingConstants.LHS_FEATURE, reader)) {
                if (lhs == null) {
                    lhs = readFeature(reader.getName(), reader);
                } else {
                    throw new XMLStreamException("duplicate " + XmiMappingConstants.LHS_FEATURE + " tag.");
                }
            } else if (match(XmiMappingConstants.RHS_FEATURE, reader)) {
                rhs = readFeature(reader.getName(), reader);
            } else if (match(XmiMappingConstants.STATUS, reader)) {
                status = readStatus(reader);
            } else if (match(XmiMappingConstants.COMMENT, reader)) {
                comment = readStringContent(reader.getName(), reader);
            } else if (match(XmiMappingConstants.TRACKING, reader)) {
                tracking = readStringContent(reader.getName(), reader);
            } else if (match(XmiMappingConstants.LHS_MISSING, reader)) {
                XMLStreamReaderTools.skipElement(reader);
            } else if (match(XmiMappingConstants.RHS_MISSING, reader)) {
                XMLStreamReaderTools.skipElement(reader);
            } else {
                throw new AssertionError(reader.getLocalName());
            }
            break;
        }
        case XMLStreamConstants.END_ELEMENT: {
            assertName(XmiMappingConstants.MAPPING, reader);
            done = true;
            break;
        }
        case XMLStreamConstants.CHARACTERS:
        case XMLStreamConstants.COMMENT: {
            // Ignore.
            break;
        }
        default: {
            throw new AssertionError(reader.getEventType());
        }
        }
    }
    return new XmiMapping(lhs, rhs, status, tracking, comment);
}

From source file:org.slc.sli.modeling.xmi.comp.XmiMappingReader.java

private static final XmiComparison readMappingList(final XMLStreamReader reader) throws XMLStreamException {
    assertStartElement(reader);/* w  ww .  j a  v a 2  s.c  o m*/
    assertName(XmiMappingConstants.DOCUMENT_ELEMENT, reader);
    XmiDefinition lhsModel = null;
    XmiDefinition rhsModel = null;
    final List<XmiMapping> mappingList = new LinkedList<XmiMapping>();
    while (reader.hasNext()) {
        reader.next();
        switch (reader.getEventType()) {
        case XMLStreamConstants.START_ELEMENT: {
            if (match(XmiMappingConstants.MAPPING, reader)) {
                mappingList.add(assertNotNull(readMapping(reader)));
            } else if (match(XmiMappingConstants.LHS_MODEL, reader)) {
                lhsModel = assertNotNull(readModel(reader));
            } else if (match(XmiMappingConstants.RHS_MODEL, reader)) {
                rhsModel = assertNotNull(readModel(reader));
            } else {
                throw new AssertionError(reader.getLocalName());
            }
            break;
        }
        case XMLStreamConstants.END_ELEMENT: {
            assertName(XmiMappingConstants.DOCUMENT_ELEMENT, reader);
            return new XmiComparison(lhsModel, rhsModel, mappingList);
        }
        case XMLStreamConstants.CHARACTERS:
        case XMLStreamConstants.COMMENT:
        case XMLStreamConstants.PROCESSING_INSTRUCTION: {
            // Ignore.
            break;
        }
        default: {
            throw new AssertionError(reader.getEventType());
        }
        }
    }
    throw new AssertionError();
}

From source file:org.slc.sli.modeling.xmi.comp.XmiMappingReader.java

private static final XmiDefinition readModel(final XMLStreamReader reader) throws XMLStreamException {
    assertStartElement(reader);//from w ww .  j  av a 2 s .  c om
    final QName elementName = reader.getName();
    assertName(elementName, reader);
    String name = null;
    String version = null;
    String xmi = null;
    boolean done = false;
    while (!done && reader.hasNext()) {
        reader.next();
        switch (reader.getEventType()) {
        case XMLStreamConstants.START_ELEMENT: {
            if (match(XmiMappingConstants.NAME, reader)) {
                name = readStringContent(reader.getName(), reader);
            } else if (match(XmiMappingConstants.VERSION, reader)) {
                version = readStringContent(reader.getName(), reader);
            } else if (match(XmiMappingConstants.FILE, reader)) {
                xmi = readStringContent(reader.getName(), reader);
            } else {
                throw new AssertionError(reader.getLocalName());
            }
            break;
        }
        case XMLStreamConstants.END_ELEMENT: {
            assertName(elementName, reader);
            done = true;
            break;
        }
        case XMLStreamConstants.CHARACTERS:
        case XMLStreamConstants.COMMENT: {
            // Ignore.
            break;
        }
        default: {
            throw new AssertionError(reader.getEventType());
        }
        }
    }
    return new XmiDefinition(name, version, xmi);
}