Example usage for javax.xml.stream XMLStreamConstants DTD

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

Introduction

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

Prototype

int DTD

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

Click Source Link

Document

Indicates an event is a DTD

Usage

From source file:Main.java

/** Get the human readable event name for the numeric event id */
public static String getEventName(int eventId) {
    switch (eventId) {
    case XMLStreamConstants.START_ELEMENT:
        return "StartElementEvent";
    case XMLStreamConstants.END_ELEMENT:
        return "EndElementEvent";
    case XMLStreamConstants.PROCESSING_INSTRUCTION:
        return "ProcessingInstructionEvent";
    case XMLStreamConstants.CHARACTERS:
        return "CharacterEvent";
    case XMLStreamConstants.COMMENT:
        return "CommentEvent";
    case XMLStreamConstants.START_DOCUMENT:
        return "StartDocumentEvent";
    case XMLStreamConstants.END_DOCUMENT:
        return "EndDocumentEvent";
    case XMLStreamConstants.ENTITY_REFERENCE:
        return "EntityReferenceEvent";
    case XMLStreamConstants.ATTRIBUTE:
        return "AttributeBase";
    case XMLStreamConstants.DTD:
        return "DTDEvent";
    case XMLStreamConstants.CDATA:
        return "CDATA";
    }/*from ww  w.  ja va2s . c  om*/
    return "UNKNOWN_EVENT_TYPE";
}

From source file:Main.java

/**
 * Get the string representation of a given StAX event type. The returned
 * value is the name of the constant in {@link XMLStreamConstants}
 * corresponding to the event type.//from   w w  w.  j a v a2s. c o m
 * 
 * @param event
 *            the event type as returned by
 *            {@link javax.xml.stream.events.XMLEvent#getEventType()},
 *            {@link javax.xml.stream.XMLStreamReader#getEventType()} or
 *            {@link javax.xml.stream.XMLStreamReader#next()}
 * @return a string representation of the event type
 */
public static String getEventTypeString(int event) {
    String state = null;
    switch (event) {
    case XMLStreamConstants.START_ELEMENT:
        state = "START_ELEMENT";
        break;
    case XMLStreamConstants.START_DOCUMENT:
        state = "START_DOCUMENT";
        break;
    case XMLStreamConstants.CHARACTERS:
        state = "CHARACTERS";
        break;
    case XMLStreamConstants.CDATA:
        state = "CDATA";
        break;
    case XMLStreamConstants.END_ELEMENT:
        state = "END_ELEMENT";
        break;
    case XMLStreamConstants.END_DOCUMENT:
        state = "END_DOCUMENT";
        break;
    case XMLStreamConstants.SPACE:
        state = "SPACE";
        break;
    case XMLStreamConstants.COMMENT:
        state = "COMMENT";
        break;
    case XMLStreamConstants.DTD:
        state = "DTD";
        break;
    case XMLStreamConstants.PROCESSING_INSTRUCTION:
        state = "PROCESSING_INSTRUCTION";
        break;
    case XMLStreamConstants.ENTITY_REFERENCE:
        state = "ENTITY_REFERENCE";
        break;
    default:
        state = "UNKNOWN_STATE: " + event;
    }
    return state;
}

From source file:Main.java

private static void parseRestOfDocument(XMLStreamReader reader) throws XMLStreamException {

    while (reader.hasNext()) {
        int type = reader.next();
        switch (type) {
        case XMLStreamConstants.START_ELEMENT:
            System.out.println(reader.getLocalName());
            if (reader.getNamespaceURI() != null) {
                String prefix = reader.getPrefix();
                if (prefix == null) {
                    prefix = "[None]";
                }/*w w  w.  jav  a  2  s .  co  m*/
                System.out.println("prefix = '" + prefix + "', URI = '" + reader.getNamespaceURI() + "'");
            }

            if (reader.getAttributeCount() > 0) {
                for (int i = 0; i < reader.getAttributeCount(); i++) {
                    System.out.println("Attribute (name = '" + reader.getAttributeLocalName(i) + "', value = '"
                            + reader.getAttributeValue(i) + "')");
                    String attURI = reader.getAttributeNamespace(i);
                    if (attURI != null) {
                        String attPrefix = reader.getAttributePrefix(i);
                        if (attPrefix == null || attPrefix.equals("")) {
                            attPrefix = "[None]";
                        }
                        System.out.println("prefix=" + attPrefix + ",URI=" + attURI);
                    }
                }
            }

            break;
        case XMLStreamConstants.END_ELEMENT:
            System.out.println("XMLStreamConstants.END_ELEMENT");
            break;
        case XMLStreamConstants.CHARACTERS:
            if (!reader.isWhiteSpace()) {
                System.out.println("CD:" + reader.getText());
            }
            break;
        case XMLStreamConstants.DTD:
            System.out.println("DTD:" + reader.getText());
            break;
        case XMLStreamConstants.SPACE:
            System.out.println(" ");
            break;
        case XMLStreamConstants.COMMENT:
            System.out.println(reader.getText());
            break;
        default:
            System.out.println(type);
        }
    }
}

From source file:com.widowcrawler.exo.parse.Parser.java

private static String getEventTypeString(int eventType) {
    switch (eventType) {
    case XMLStreamConstants.START_ELEMENT:
        return "START_ELEMENT";
    case XMLStreamConstants.END_ELEMENT:
        return "END_ELEMENT";
    case XMLStreamConstants.PROCESSING_INSTRUCTION:
        return "PROCESSING_INSTRUCTION";
    case XMLStreamConstants.CHARACTERS:
        return "CHARACTERS";
    case XMLStreamConstants.COMMENT:
        return "COMMENT";
    case XMLStreamConstants.START_DOCUMENT:
        return "START_DOCUMENT";
    case XMLStreamConstants.END_DOCUMENT:
        return "END_DOCUMENT";
    case XMLStreamConstants.ENTITY_REFERENCE:
        return "ENTITY_REFERENCE";
    case XMLStreamConstants.ATTRIBUTE:
        return "ATTRIBUTE";
    case XMLStreamConstants.DTD:
        return "DTD";
    case XMLStreamConstants.CDATA:
        return "CDATA";
    case XMLStreamConstants.SPACE:
        return "SPACE";
    case XMLStreamConstants.ENTITY_DECLARATION:
        return "ENTITY_DECLARATION";
    case XMLStreamConstants.NAMESPACE:
        return "NAMESPACE";
    case XMLStreamConstants.NOTATION_DECLARATION:
        return "NOTATION_DECLARATION";
    }//w  ww  . j ava  2s  . com
    return "UNKNOWN_EVENT_TYPE: " + eventType;
}

From source file:StAXEventTreeViewer.java

public void buildTree(DefaultTreeModel treeModel, DefaultMutableTreeNode current, File file)
        throws XMLStreamException, FileNotFoundException {

    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    XMLEventReader reader = inputFactory.createXMLEventReader(new FileInputStream(file));
    while (reader.hasNext()) {
        XMLEvent event = reader.nextEvent();
        switch (event.getEventType()) {
        case XMLStreamConstants.START_DOCUMENT:
            StartDocument startDocument = (StartDocument) event;
            DefaultMutableTreeNode version = new DefaultMutableTreeNode(startDocument.getVersion());
            current.add(version);//  w w w . j  a va2  s.c o  m

            current.add(new DefaultMutableTreeNode(startDocument.isStandalone()));
            current.add(new DefaultMutableTreeNode(startDocument.standaloneSet()));
            current.add(new DefaultMutableTreeNode(startDocument.encodingSet()));
            current.add(new DefaultMutableTreeNode(startDocument.getCharacterEncodingScheme()));
            break;
        case XMLStreamConstants.START_ELEMENT:
            StartElement startElement = (StartElement) event;
            QName elementName = startElement.getName();

            DefaultMutableTreeNode element = new DefaultMutableTreeNode(elementName.getLocalPart());
            current.add(element);
            current = element;

            if (!elementName.getNamespaceURI().equals("")) {
                String prefix = elementName.getPrefix();
                if (prefix.equals("")) {
                    prefix = "[None]";
                }
                DefaultMutableTreeNode namespace = new DefaultMutableTreeNode(
                        "prefix=" + prefix + ",URI=" + elementName.getNamespaceURI());
                current.add(namespace);
            }

            for (Iterator it = startElement.getAttributes(); it.hasNext();) {
                Attribute attr = (Attribute) it.next();
                DefaultMutableTreeNode attribute = new DefaultMutableTreeNode("Attribute (name="
                        + attr.getName().getLocalPart() + ",value=" + attr.getValue() + "')");
                String attURI = attr.getName().getNamespaceURI();
                if (!attURI.equals("")) {
                    String attPrefix = attr.getName().getPrefix();
                    if (attPrefix.equals("")) {
                        attPrefix = "[None]";
                    }
                    attribute.add(new DefaultMutableTreeNode("prefix = " + attPrefix + ", URI = " + attURI));
                }
                current.add(attribute);
            }
            break;
        case XMLStreamConstants.END_ELEMENT:
            current = (DefaultMutableTreeNode) current.getParent();
            break;
        case XMLStreamConstants.CHARACTERS:
            Characters characters = (Characters) event;
            if (!characters.isIgnorableWhiteSpace() && !characters.isWhiteSpace()) {
                String data = characters.getData();
                if (data.length() != 0) {
                    current.add(new DefaultMutableTreeNode(characters.getData()));
                }
            }
            break;
        case XMLStreamConstants.DTD:
            DTD dtde = (DTD) event;
            current.add(new DefaultMutableTreeNode(dtde.getDocumentTypeDeclaration()));
        default:
            System.out.println(event.getClass().getName());
        }
    }
}

From source file:StAXStreamTreeViewer.java

private void parseRestOfDocument(XMLStreamReader reader, DefaultMutableTreeNode current)
        throws XMLStreamException {

    while (reader.hasNext()) {
        int type = reader.next();
        switch (type) {
        case XMLStreamConstants.START_ELEMENT:

            DefaultMutableTreeNode element = new DefaultMutableTreeNode(reader.getLocalName());
            current.add(element);//  ww w. ja v  a 2  s .co m
            current = element;

            if (reader.getNamespaceURI() != null) {
                String prefix = reader.getPrefix();
                if (prefix == null) {
                    prefix = "[None]";
                }
                DefaultMutableTreeNode namespace = new DefaultMutableTreeNode(
                        "prefix = '" + prefix + "', URI = '" + reader.getNamespaceURI() + "'");
                current.add(namespace);
            }

            if (reader.getAttributeCount() > 0) {
                for (int i = 0; i < reader.getAttributeCount(); i++) {
                    DefaultMutableTreeNode attribute = new DefaultMutableTreeNode(
                            "Attribute (name = '" + reader.getAttributeLocalName(i) + "', value = '"
                                    + reader.getAttributeValue(i) + "')");
                    String attURI = reader.getAttributeNamespace(i);
                    if (attURI != null) {
                        String attPrefix = reader.getAttributePrefix(i);
                        if (attPrefix == null || attPrefix.equals("")) {
                            attPrefix = "[None]";
                        }
                        DefaultMutableTreeNode attNamespace = new DefaultMutableTreeNode(
                                "prefix=" + attPrefix + ",URI=" + attURI);
                        attribute.add(attNamespace);
                    }
                    current.add(attribute);
                }
            }

            break;
        case XMLStreamConstants.END_ELEMENT:
            current = (DefaultMutableTreeNode) current.getParent();
            break;
        case XMLStreamConstants.CHARACTERS:
            if (!reader.isWhiteSpace()) {
                DefaultMutableTreeNode data = new DefaultMutableTreeNode("CD:" + reader.getText());
                current.add(data);
            }
            break;
        case XMLStreamConstants.DTD:
            DefaultMutableTreeNode dtd = new DefaultMutableTreeNode("DTD:" + reader.getText());
            current.add(dtd);
            break;
        case XMLStreamConstants.SPACE:
            break;
        case XMLStreamConstants.COMMENT:
            DefaultMutableTreeNode comment = new DefaultMutableTreeNode(reader.getText());
            current.add(comment);
            break;
        default:
            System.out.println(type);
        }
    }
}

From source file:org.javelin.sws.ext.bind.internal.model.ComplexTypePattern.java

@Override
public T consumeValue(XMLEventReader eventReader, UnmarshallingContext context) throws XMLStreamException {
    // first create an object to be filled (using PropertyAccessors - direct or bean) according to the content model
    T object = BeanUtils.instantiate(this.getJavaType());

    // the order is dictated by incoming events, not by the mode
    // TODO: create a property to enable strict unmarshalling - dictated by content model
    // only this (ContentModel) pattern iterates over XML Events
    XMLEvent event = null;//  w  w w  .java2s.c  o  m
    PropertyMetadataValue<T, ?> pmv = null;

    // this loop will only handle first level of start elements and only single end element
    // deeper levels will be handled by nested patterns
    while (true) {
        boolean end = false;
        event = eventReader.peek();
        pmv = null;

        switch (event.getEventType()) {
        case XMLStreamConstants.ATTRIBUTE:
            pmv = this.consumeNestedAttribute(eventReader, context);
            break;
        case XMLStreamConstants.CDATA:
        case XMLStreamConstants.CHARACTERS:
            // TODO: XMLEvent.ENTITY_REFERENCE?
            if (this.simpleContent != null) {
                pmv = this.consumeSimpleContent(eventReader, context);
                break;
            }
        case XMLStreamConstants.COMMENT:
        case XMLStreamConstants.DTD:
        case XMLStreamConstants.SPACE:
        case XMLStreamConstants.ENTITY_DECLARATION:
        case XMLStreamConstants.NOTATION_DECLARATION:
        case XMLStreamConstants.PROCESSING_INSTRUCTION:
            eventReader.nextEvent();
            break;
        case XMLStreamConstants.ENTITY_REFERENCE:
            // TODO: XMLEvent.ENTITY_REFERENCE?
            eventReader.nextEvent();
            break;
        case XMLStreamConstants.START_DOCUMENT:
            // strange
            break;
        case XMLStreamConstants.START_ELEMENT:
            pmv = this.consumeNestedElement(eventReader, context);
            break;
        case XMLStreamConstants.END_ELEMENT:
            // TODO: in mixed content there will be more than one end element it this content model's level
        case XMLStreamConstants.END_DOCUMENT:
            end = true;
            break;
        }

        if (end)
            break;

        if (pmv != null)
            pmv.getMetadata().setValue(object, pmv.getValue());
    }

    return (T) object;
}

From source file:com.marklogic.contentpump.AggregateXMLReader.java

@Override
public boolean nextKeyValue() throws IOException, InterruptedException {
    if (xmlSR == null) {
        hasNext = false;/*from w w  w  .  j a  v a  2  s. c o  m*/
        return false;
    }

    try {
        while (xmlSR.hasNext()) {
            int eventType;
            //getCharacterOffset() returns int; 
            //int will overflows if file is larger than 2GB
            if (!overflow && xmlSR.getLocation().getCharacterOffset() < -1) {
                overflow = true;
                LOG.info("In progress...");
            }
            //do not update pos if offset overflows
            if (!overflow) {
                pos = xmlSR.getLocation().getCharacterOffset();
            }
            eventType = xmlSR.next();
            switch (eventType) {
            case XMLStreamConstants.START_ELEMENT:
                if (startOfRecord) {
                    // this is the start of the root, only copy
                    // namespaces
                    copyNameSpaceDecl();
                    startOfRecord = false;
                    continue;
                }
                processStartElement();
                break;
            case XMLStreamConstants.CHARACTERS:
                write(StringEscapeUtils.escapeXml(xmlSR.getText()));
                break;
            case XMLStreamConstants.CDATA:
                write("<![CDATA[");
                write(xmlSR.getText());
                write("]]>");
                break;
            case XMLStreamConstants.SPACE:
                write(xmlSR.getText());
                break;
            case XMLStreamConstants.ENTITY_REFERENCE:
                write("&");
                write(xmlSR.getLocalName());
                write(";");
                break;
            case XMLStreamConstants.DTD:
                write("<!DOCTYPE");
                write(xmlSR.getText());
                write(">");
                break;
            case XMLStreamConstants.PROCESSING_INSTRUCTION:
                write("<?");
                write(xmlSR.getPIData());
                write("?>");
                break;
            case XMLStreamConstants.COMMENT:
                write("<!--");
                write(xmlSR.getText());
                write("-->");
                break;
            case XMLStreamConstants.END_ELEMENT:
                keepGoing = processEndElement();
                if (!keepGoing) {
                    keepGoing = true;
                    return true;
                }
                break;
            case XMLStreamConstants.START_DOCUMENT:
                throw new XMLStreamException("unexpected start of document within record!\n" + "recordName = "
                        + recordName + ", recordNamespace = " + recordNamespace + " at " + xmlSR.getLocation());
            case XMLStreamConstants.END_DOCUMENT:
                if (currentId != null) {
                    throw new XMLStreamException(
                            "end of document before end of current record!\n" + "recordName = " + recordName
                                    + ", recordNamespace = " + recordNamespace + " at " + xmlSR.getLocation());
                } else {
                    if (compressed) {
                        //this doc is done, refer to the zip for next doc
                        hasNext = false;
                        return false;
                    } else {
                        //get next file from FileIterator
                        if (iterator != null && iterator.hasNext()) {
                            close();
                            initStreamReader(iterator.next());
                            continue;
                        } else {
                            hasNext = false;
                            return false;
                        }
                    }
                }
            default:
                throw new XMLStreamException("UNIMPLEMENTED: " + eventType);
            }
        }
    } catch (XMLStreamException e) {
        LOG.error("Parsing error", e);
        throw new IOException("Parsing error", e);
    }

    return false;
}

From source file:ca.uhn.fhir.parser.XmlParser.java

private void encodeXhtml(XhtmlDt theDt, XMLStreamWriter theEventWriter) throws XMLStreamException {
    if (theDt == null || theDt.getValue() == null) {
        return;//from w  w w  .ja  v a2 s  .c o m
    }

    boolean firstElement = true;
    for (XMLEvent event : theDt.getValue()) {
        switch (event.getEventType()) {
        case XMLStreamConstants.ATTRIBUTE:
            Attribute attr = (Attribute) event;
            if (isBlank(attr.getName().getPrefix())) {
                if (isBlank(attr.getName().getNamespaceURI())) {
                    theEventWriter.writeAttribute(attr.getName().getLocalPart(), attr.getValue());
                } else {
                    theEventWriter.writeAttribute(attr.getName().getNamespaceURI(),
                            attr.getName().getLocalPart(), attr.getValue());
                }
            } else {
                theEventWriter.writeAttribute(attr.getName().getPrefix(), attr.getName().getNamespaceURI(),
                        attr.getName().getLocalPart(), attr.getValue());
            }

            break;
        case XMLStreamConstants.CDATA:
            theEventWriter.writeCData(((Characters) event).getData());
            break;
        case XMLStreamConstants.CHARACTERS:
        case XMLStreamConstants.SPACE:
            String data = ((Characters) event).getData();
            theEventWriter.writeCharacters(data);
            break;
        case XMLStreamConstants.COMMENT:
            theEventWriter.writeComment(((Comment) event).getText());
            break;
        case XMLStreamConstants.END_ELEMENT:
            theEventWriter.writeEndElement();
            break;
        case XMLStreamConstants.ENTITY_REFERENCE:
            EntityReference er = (EntityReference) event;
            theEventWriter.writeEntityRef(er.getName());
            break;
        case XMLStreamConstants.NAMESPACE:
            Namespace ns = (Namespace) event;
            theEventWriter.writeNamespace(ns.getPrefix(), ns.getNamespaceURI());
            break;
        case XMLStreamConstants.START_ELEMENT:
            StartElement se = event.asStartElement();
            if (firstElement) {
                if (StringUtils.isBlank(se.getName().getPrefix())) {
                    String namespaceURI = se.getName().getNamespaceURI();
                    if (StringUtils.isBlank(namespaceURI)) {
                        namespaceURI = "http://www.w3.org/1999/xhtml";
                    }
                    theEventWriter.writeStartElement(se.getName().getLocalPart());
                    theEventWriter.writeDefaultNamespace(namespaceURI);
                } else {
                    String prefix = se.getName().getPrefix();
                    String namespaceURI = se.getName().getNamespaceURI();
                    theEventWriter.writeStartElement(prefix, se.getName().getLocalPart(), namespaceURI);
                    theEventWriter.writeNamespace(prefix, namespaceURI);
                }
                firstElement = false;
            } else {
                if (isBlank(se.getName().getPrefix())) {
                    if (isBlank(se.getName().getNamespaceURI())) {
                        theEventWriter.writeStartElement(se.getName().getLocalPart());
                    } else {
                        if (StringUtils.isBlank(se.getName().getPrefix())) {
                            theEventWriter.writeStartElement(se.getName().getLocalPart());
                            // theEventWriter.writeDefaultNamespace(se.getName().getNamespaceURI());
                        } else {
                            theEventWriter.writeStartElement(se.getName().getNamespaceURI(),
                                    se.getName().getLocalPart());
                        }
                    }
                } else {
                    theEventWriter.writeStartElement(se.getName().getPrefix(), se.getName().getLocalPart(),
                            se.getName().getNamespaceURI());
                }
                for (Iterator<?> attrIter = se.getAttributes(); attrIter.hasNext();) {
                    Attribute next = (Attribute) attrIter.next();
                    theEventWriter.writeAttribute(next.getName().getLocalPart(), next.getValue());
                }
            }
            break;
        case XMLStreamConstants.DTD:
        case XMLStreamConstants.END_DOCUMENT:
        case XMLStreamConstants.ENTITY_DECLARATION:
        case XMLStreamConstants.NOTATION_DECLARATION:
        case XMLStreamConstants.PROCESSING_INSTRUCTION:
        case XMLStreamConstants.START_DOCUMENT:
            break;
        }

    }
}