Example usage for javax.xml.stream XMLStreamReader isStartElement

List of usage examples for javax.xml.stream XMLStreamReader isStartElement

Introduction

In this page you can find the example usage for javax.xml.stream XMLStreamReader isStartElement.

Prototype

public boolean isStartElement();

Source Link

Document

Returns true if the cursor points to a start tag (otherwise false)

Usage

From source file:org.xwiki.contrib.confluence.filter.internal.ConfluenceXMLPackage.java

private void createTree() throws XMLStreamException, FactoryConfigurationError, IOException,
        ConfigurationException, FilterException {
    if (this.temporaryDirectory) {
        this.tree = new File(this.directory, "tree");
    } else {/*from w  w  w .  j av  a  2  s .co m*/
        this.tree = File.createTempFile("confluencexml-tree", "");
        this.tree.delete();
    }
    this.tree.mkdir();

    try (InputStream stream = new FileInputStream(getEntities())) {
        XMLStreamReader xmlReader = XML_INPUT_FACTORY.createXMLStreamReader(stream);

        xmlReader.nextTag();

        for (xmlReader.nextTag(); xmlReader.isStartElement(); xmlReader.nextTag()) {
            String elementName = xmlReader.getLocalName();

            if (elementName.equals("object")) {
                readObject(xmlReader);
            } else {
                StAXUtils.skipElement(xmlReader);
            }
        }
    }
}

From source file:org.xwiki.contrib.confluence.filter.internal.ConfluenceXMLPackage.java

private long readObjectProperties(XMLStreamReader xmlReader, PropertiesConfiguration properties)
        throws XMLStreamException, FilterException {
    long id = -1;

    for (xmlReader.nextTag(); xmlReader.isStartElement(); xmlReader.nextTag()) {
        String elementName = xmlReader.getLocalName();

        if (elementName.equals("id")) {
            String idName = xmlReader.getAttributeValue(null, "name");

            if (idName != null && idName.equals("id")) {
                id = Long.valueOf(xmlReader.getElementText());

                properties.setProperty("id", id);
            } else {
                StAXUtils.skipElement(xmlReader);
            }//  w ww. j a va2 s  . c  om
        } else if (elementName.equals("property") || elementName.equals("collection")) {
            String propertyName = xmlReader.getAttributeValue(null, "name");

            properties.setProperty(propertyName, readProperty(xmlReader));
        } else {
            StAXUtils.skipElement(xmlReader);
        }
    }

    return id;
}

From source file:org.xwiki.contrib.confluence.filter.internal.ConfluenceXMLPackage.java

private List<Object> readListProperty(XMLStreamReader xmlReader) throws XMLStreamException, FilterException {
    List<Object> list = new ArrayList<>();

    for (xmlReader.nextTag(); xmlReader.isStartElement(); xmlReader.nextTag()) {
        list.add(readProperty(xmlReader));
    }/*ww w.  j av  a  2s  .co  m*/

    return list;
}

From source file:org.xwiki.contrib.confluence.filter.internal.ConfluenceXMLPackage.java

private Set<Object> readSetProperty(XMLStreamReader xmlReader) throws XMLStreamException, FilterException {
    Set<Object> set = new LinkedHashSet<>();

    for (xmlReader.nextTag(); xmlReader.isStartElement(); xmlReader.nextTag()) {
        set.add(readProperty(xmlReader));
    }/*from www .j  av  a2s  . c  om*/

    return set;
}

From source file:org.xwiki.filter.confluence.xml.internal.ConfluenceXMLPackage.java

private void createTree() throws XMLStreamException, FactoryConfigurationError, NumberFormatException,
        IOException, ConfigurationException, FilterException {
    this.tree = new File(this.directory, "tree");
    this.tree.mkdir();

    InputStream stream = new FileInputStream(getEntities());

    XMLStreamReader xmlReader = XMLInputFactory.newInstance().createXMLStreamReader(stream);

    xmlReader.nextTag();//from www  . j  ava  2  s. co m

    for (xmlReader.nextTag(); xmlReader.isStartElement(); xmlReader.nextTag()) {
        String elementName = xmlReader.getLocalName();

        if (elementName.equals("object")) {
            readObject(xmlReader);
        } else {
            StAXUtils.skipElement(xmlReader);
        }
    }
}

From source file:org.xwiki.filter.confluence.xml.internal.ConfluenceXMLPackage.java

private int readObjectProperties(XMLStreamReader xmlReader, PropertiesConfiguration properties)
        throws XMLStreamException, FilterException, ConfigurationException, IOException {
    int id = -1;// w  ww  . j a v a  2s .c  om

    for (xmlReader.nextTag(); xmlReader.isStartElement(); xmlReader.nextTag()) {
        String elementName = xmlReader.getLocalName();

        if (elementName.equals("id")) {
            id = Integer.valueOf(xmlReader.getElementText());

            properties.setProperty("id", id);
        } else if (elementName.equals("property")) {
            String propertyName = xmlReader.getAttributeValue(null, "name");

            properties.setProperty(propertyName, readProperty(xmlReader));
        } else if (elementName.equals("collection")) {
            String propertyName = xmlReader.getAttributeValue(null, "name");

            properties.setProperty(propertyName, readProperty(xmlReader));
        } else {
            StAXUtils.skipElement(xmlReader);
        }
    }

    return id;
}

From source file:org.xwiki.filter.confluence.xml.internal.ConfluenceXMLPackage.java

private List<Object> readListProperty(XMLStreamReader xmlReader) throws XMLStreamException, FilterException {
    List<Object> list = new ArrayList<Object>();

    for (xmlReader.nextTag(); xmlReader.isStartElement(); xmlReader.nextTag()) {
        list.add(readProperty(xmlReader));
    }/*from ww w. j a va  2 s .c  om*/

    return list;
}

From source file:org.xwiki.filter.confluence.xml.internal.ConfluenceXMLPackage.java

private Set<Object> readSetProperty(XMLStreamReader xmlReader) throws XMLStreamException, FilterException {
    Set<Object> set = new LinkedHashSet<Object>();

    for (xmlReader.nextTag(); xmlReader.isStartElement(); xmlReader.nextTag()) {
        set.add(readProperty(xmlReader));
    }//from ww w . j  a  v  a  2s .  com

    return set;
}

From source file:org.xwiki.filter.xar.internal.input.AttachmentReader.java

@Override
public WikiAttachment read(XMLStreamReader xmlReader, XARInputProperties properties)
        throws XMLStreamException, FilterException {
    WikiAttachment wikiAttachment = new WikiAttachment();

    for (xmlReader.nextTag(); xmlReader.isStartElement(); xmlReader.nextTag()) {
        String elementName = xmlReader.getLocalName();

        EventParameter parameter = XARAttachmentModel.ATTACHMENT_PARAMETERS.get(elementName);

        if (parameter != null) {
            Object wsValue = convert(parameter.type, xmlReader.getElementText());
            if (wsValue != null) {
                wikiAttachment.parameters.put(parameter.name, wsValue);
            }//www.j  a v  a 2  s.  c o m
        } else {
            if (XARAttachmentModel.ELEMENT_NAME.equals(elementName)) {
                wikiAttachment.name = xmlReader.getElementText();
            } else if (XARAttachmentModel.ELEMENT_CONTENT_SIZE.equals(elementName)) {
                wikiAttachment.size = Long.valueOf(xmlReader.getElementText());
            } else if (XARAttachmentModel.ELEMENT_CONTENT.equals(elementName)) {
                // We copy the attachment content to use it later. We can't directly send it as a stream because XAR
                // specification does not force any order for the attachment properties and we need to be sure we
                // have everything when sending the event.

                // Allocate a temporary file in case the attachment content is big
                File temporaryFile;
                try {
                    temporaryFile = File.createTempFile("xar/attachments/attachment", ".bin");
                    temporaryFile.deleteOnExit();
                } catch (IOException e) {
                    throw new FilterException(e);
                }

                // Create a deferred file based content (if the content is bigger than 10000 bytes it will end up in
                // a file)
                wikiAttachment.content = new DeferredFileOutputStream(100000, temporaryFile);

                // Copy the content to byte array or file depending on its size
                for (xmlReader.next(); xmlReader.isCharacters(); xmlReader.next()) {
                    try {
                        wikiAttachment.content.write(xmlReader.getText().getBytes(StandardCharsets.UTF_8));
                    } catch (IOException e) {
                        throw new FilterException(e);
                    }
                }
            }
        }
    }

    return wikiAttachment;
}

From source file:org.xwiki.filter.xar.internal.input.DocumentLocaleReader.java

private void readDocument(XMLStreamReader xmlReader, Object filter, XARInputFilter proxyFilter)
        throws XMLStreamException, FilterException {
    this.currentSourceType = SourceType.DOCUMENT;

    // Initialize with a few defaults (thing that don't exist in old XAR format)
    this.currentDocumentRevisionParameters.put(XWikiWikiDocumentFilter.PARAMETER_SYNTAX, Syntax.XWIKI_1_0);
    this.currentDocumentRevisionParameters.put(XWikiWikiDocumentFilter.PARAMETER_HIDDEN, false);

    // Reference//from  w  w  w. j  a v a 2  s  .c  o  m
    String referenceString = xmlReader.getAttributeValue(null, XARDocumentModel.ATTRIBUTE_DOCUMENT_REFERENCE);
    if (StringUtils.isNotEmpty(referenceString)) {
        this.currentDocumentReference = this.relativeResolver.resolve(referenceString, EntityType.DOCUMENT);
        this.currentSpaceReference = this.currentDocumentReference.getParent();

        // Send needed wiki spaces event if possible
        switchWikiSpace(proxyFilter, false);
    }

    // Locale
    String localeString = xmlReader.getAttributeValue(null, XARDocumentModel.ATTRIBUTE_DOCUMENT_LOCALE);
    if (localeString != null) {
        this.currentDocumentLocale = toLocale(localeString);
        this.localeFromLegacy = false;
    }

    for (xmlReader.nextTag(); xmlReader.isStartElement(); xmlReader.nextTag()) {
        String elementName = xmlReader.getLocalName();

        if (elementName.equals(XARAttachmentModel.ELEMENT_ATTACHMENT)) {
            readAttachment(xmlReader, filter, proxyFilter);
        } else if (elementName.equals(XARObjectModel.ELEMENT_OBJECT)) {
            readObject(xmlReader, filter, proxyFilter);
        } else if (elementName.equals(XARClassModel.ELEMENT_CLASS)) {
            readClass(xmlReader, filter, proxyFilter);
        } else {
            String value = xmlReader.getElementText();

            if (XarDocumentModel.ELEMENT_SPACE.equals(elementName)) {
                this.currentLegacySpace = value;

                if (this.currentDocumentReference == null) {
                    // Its an old thing
                    if (this.currentLegacyDocument == null) {
                        this.currentSpaceReference = new EntityReference(value, EntityType.SPACE);
                    } else {
                        this.currentDocumentReference = new LocalDocumentReference(this.currentLegacySpace,
                                this.currentLegacyDocument);
                        this.currentSpaceReference = this.currentDocumentReference.getParent();
                    }

                    // Send needed wiki spaces event if possible
                    switchWikiSpace(proxyFilter, false);
                }
            } else if (XarDocumentModel.ELEMENT_NAME.equals(elementName)) {
                this.currentLegacyDocument = value;

                if (this.currentDocumentReference == null) {
                    // Its an old thing
                    if (this.currentLegacySpace != null) {
                        this.currentDocumentReference = new LocalDocumentReference(this.currentLegacySpace,
                                this.currentLegacyDocument);
                        this.currentSpaceReference = this.currentDocumentReference.getParent();
                    }
                }
            } else if (XarDocumentModel.ELEMENT_LOCALE.equals(elementName)) {
                if (this.localeFromLegacy) {
                    this.currentDocumentLocale = toLocale(value);
                }
            } else if (XarDocumentModel.ELEMENT_REVISION.equals(elementName)) {
                this.currentDocumentRevision = value;
            } else {
                EventParameter parameter = XARDocumentModel.DOCUMENT_PARAMETERS.get(elementName);

                if (parameter != null) {
                    Object wsValue = convert(parameter.type, value);
                    if (wsValue != null) {
                        this.currentDocumentParameters.put(parameter.name, wsValue);
                    }
                } else {
                    parameter = XARDocumentModel.DOCUMENTLOCALE_PARAMETERS.get(elementName);

                    if (parameter != null) {
                        Object wsValue = convert(parameter.type, value);
                        if (wsValue != null) {
                            this.currentDocumentLocaleParameters.put(parameter.name, wsValue);
                        }
                    } else {
                        parameter = XARDocumentModel.DOCUMENTREVISION_PARAMETERS.get(elementName);

                        if (parameter != null) {
                            Object objectValue;
                            if (parameter.type == EntityReference.class) {
                                objectValue = this.relativeResolver.resolve(value, EntityType.DOCUMENT);
                            } else {
                                objectValue = convert(parameter.type, value);
                            }

                            if (objectValue != null) {
                                this.currentDocumentRevisionParameters.put(parameter.name, objectValue);
                            }
                        } else {
                            // Unknown property
                            // TODO: log something ?
                        }
                    }
                }
            }
        }
    }

    sendBeginWikiDocumentRevision(proxyFilter, true);
    sendWikiAttachments(proxyFilter);
    sendWikiClass(proxyFilter);
    sendWikiObjects(proxyFilter);
    sendEndWikiDocument(proxyFilter);
}