Example usage for javax.xml.stream XMLStreamReader getElementText

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

Introduction

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

Prototype

public String getElementText() throws XMLStreamException;

Source Link

Document

Reads the content of a text-only element, an exception is thrown if this is not a text-only element.

Usage

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

private Object readProperty(XMLStreamReader xmlReader) throws XMLStreamException, FilterException {
    String propertyClass = xmlReader.getAttributeValue(null, "class");

    if (propertyClass == null) {
        return fixCData(xmlReader.getElementText());
    } else if (propertyClass.equals("java.util.List") || propertyClass.equals("java.util.Collection")) {
        return readListProperty(xmlReader);
    } else if (propertyClass.equals("java.util.Set")) {
        return readSetProperty(xmlReader);
    } else if (propertyClass.equals("Page") || propertyClass.equals("Space")
            || propertyClass.equals("BodyContent") || propertyClass.equals("Attachment")
            || propertyClass.equals("SpaceDescription") || propertyClass.equals("Labelling")
            || propertyClass.equals("SpacePermission") || propertyClass.equals("InternalGroup")
            || propertyClass.equals("InternalUser") || propertyClass.equals("Comment")) {
        return readIdProperty(xmlReader);
    } else {// www.j  a  v a 2s.  c o  m
        StAXUtils.skipElement(xmlReader);
    }

    return null;
}

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

private Integer readIdProperty(XMLStreamReader xmlReader) throws FilterException, XMLStreamException {
    xmlReader.nextTag();//from  w  w  w .  j  a v a 2s  .c om

    if (!xmlReader.getLocalName().equals("id")) {
        throw new FilterException(
                String.format("Was expecting id element but found [%s]", xmlReader.getLocalName()));
    }

    Integer value = Integer.valueOf(xmlReader.getElementText());

    xmlReader.nextTag();

    return value;
}

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);
            }/*ww w  . jav 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   www.j a va  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);
}

From source file:org.xwiki.store.filesystem.internal.migration.R910100XWIKI14871DataMigration.java

private void migrateMetadatas(Session session) throws IOException, XMLStreamException,
        FactoryConfigurationError, ParserConfigurationException, SAXException {
    this.logger.info("Migrating filesystem attachment metadatas storded in [{}]",
            this.fstools.getStorageLocationFile());

    File pathByIdStore = this.fstools.getGlobalFile("DELETED_ATTACHMENT_ID_MAPPINGS.xml");
    if (pathByIdStore.exists()) {
        try (FileInputStream stream = new FileInputStream(pathByIdStore)) {
            XMLStreamReader xmlReader = XMLInputFactory.newInstance().createXMLStreamReader(stream);

            // <deletedattachmentids>
            xmlReader.nextTag();// ww w.  j  a va2s  . co m

            for (xmlReader.nextTag(); xmlReader.isStartElement(); xmlReader.nextTag()) {
                // <entry>
                xmlReader.nextTag();

                String value1 = xmlReader.getElementText();
                xmlReader.nextTag();
                String value2 = xmlReader.getElementText();

                long id;
                String path;
                if (xmlReader.getLocalName().equals("path")) {
                    id = Long.valueOf(value1);
                    path = value2;
                } else {
                    id = Long.valueOf(value2);
                    path = value1;
                }

                // </entry>
                xmlReader.nextTag();

                File directory = new File(path);
                if (!directory.exists()) {
                    this.logger.warn("[{}] does not exist", directory);

                    continue;
                }

                if (!directory.isDirectory()) {
                    this.logger.warn("[{}] is not a directory", directory);

                    continue;
                }

                storeDeletedAttachment(directory, id, session);
            }
        }
    }
}

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

private int readObjectProperties(XMLStreamReader xmlReader, PropertiesConfiguration properties)
        throws XMLStreamException, WikiStreamException, ConfigurationException, IOException {
    int id = -1;/*from  w  w w  .j  ava  2  s .  c o m*/

    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.wikistream.confluence.xml.internal.ConfluenceXMLPackage.java

private Object readProperty(XMLStreamReader xmlReader) throws XMLStreamException, WikiStreamException {
    String propertyClass = xmlReader.getAttributeValue(null, "class");

    if (propertyClass == null) {
        return fixCData(xmlReader.getElementText());
    } else if (propertyClass.equals("java.util.List") || propertyClass.equals("java.util.Collection")) {
        return readListProperty(xmlReader);
    } else if (propertyClass.equals("java.util.Set")) {
        return readSetProperty(xmlReader);
    } else if (propertyClass.equals("Page") || propertyClass.equals("Space")
            || propertyClass.equals("BodyContent") || propertyClass.equals("Attachment")
            || propertyClass.equals("SpaceDescription") || propertyClass.equals("Labelling")
            || propertyClass.equals("SpacePermission") || propertyClass.equals("InternalGroup")
            || propertyClass.equals("InternalUser") || propertyClass.equals("Comment")) {
        return readIdProperty(xmlReader);
    } else {//from w  w w  .  j  a  v a  2 s  . co  m
        StAXUtils.skipElement(xmlReader);
    }

    return null;
}

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

private Integer readIdProperty(XMLStreamReader xmlReader) throws WikiStreamException, XMLStreamException {
    xmlReader.nextTag();/*w w w.java  2  s .  c o  m*/

    if (!xmlReader.getLocalName().equals("id")) {
        throw new WikiStreamException(
                String.format("Was expecting id element but found [%s]", xmlReader.getLocalName()));
    }

    Integer value = Integer.valueOf(xmlReader.getElementText());

    xmlReader.nextTag();

    return value;
}

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

public WikiAttachment read(XMLStreamReader xmlReader) throws XMLStreamException, WikiStreamException {
    WikiAttachment wikiAttachment = new WikiAttachment();

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

        String value = xmlReader.getElementText();

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

        if (parameter != null) {
            Object wsValue = convert(parameter.type, value);
            if (wsValue != null) {
                wikiAttachment.parameters.put(parameter.name, wsValue);
            }// w  ww . j a  v a 2s .c o  m
        } else {
            if (XARAttachmentModel.ELEMENT_NAME.equals(elementName)) {
                wikiAttachment.name = value;
            } else if (XARAttachmentModel.ELEMENT_CONTENT.equals(elementName)) {
                wikiAttachment.content = Base64.decodeBase64(value.getBytes());
            }
        }
    }

    return wikiAttachment;
}

From source file:org.xwiki.xar.internal.property.DateXarObjectPropertySerializer.java

@Override
public Object read(XMLStreamReader reader) throws XMLStreamException {
    String value = reader.getElementText();

    if (StringUtils.isEmpty(value)) {
        return null;
    }// w w  w. j a  v  a 2 s  . c  o  m

    // FIXME: The value of a date property should be serialized using the date timestamp or the date format
    // specified in the XClass the date property belongs to.
    SimpleDateFormat sdf = DEFAULT_FORMAT;
    try {
        return sdf.parse(value);
    } catch (ParseException e) {
        // I suppose this is a date format used a long time ago. DateProperty is using the above date format now.
        SimpleDateFormat sdfOld = new SimpleDateFormat("EEE MMM d HH:mm:ss z yyyy", Locale.US);
        this.logger.warn("Failed to parse date [{}] using format [{}]. Trying again with format [{}].", value,
                sdf.toPattern(), sdfOld.toPattern());
        try {
            return sdfOld.parse(value);
        } catch (ParseException exception) {
            this.logger.warn("Failed to parse date [{}] using format [{}]. Defaulting to the current date.",
                    value, sdfOld.toPattern());
            return new Date();
        }
    }
}