Example usage for javax.xml.stream XMLStreamReader getLocalName

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

Introduction

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

Prototype

public String getLocalName();

Source Link

Document

Returns the (local) name of the current event.

Usage

From source file:org.springmodules.remoting.xmlrpc.stax.AbstractStaxXmlRpcParser.java

/**
 * Creates a new Object from the current element being read in the specified
 * <code>StreamReader</code>.
 * /*from w  w w  .  j  a  v a 2s .c o  m*/
 * @param reader
 *          the <code>StreamReader</code>.
 * @return the created Object.
 * @throws XmlRpcInvalidPayloadException
 *           if the element contains an unknown child.
 * @see #parseArrayElement(XMLStreamReader)
 * @see #parseStructElement(XMLStreamReader)
 */
protected final XmlRpcElement parseValueElement(XMLStreamReader reader) throws XMLStreamException {

    while (reader.hasNext()) {
        int event = reader.next();
        String localName = null;

        switch (event) {
        case XMLStreamConstants.START_ELEMENT:
            localName = reader.getLocalName();

            if (XmlRpcElementNames.ARRAY.equals(localName)) {
                return parseArrayElement(reader);

            } else if (XmlRpcElementNames.BASE_64.equals(localName)) {
                String source = reader.getElementText();
                return new XmlRpcBase64(source);

            } else if (XmlRpcElementNames.BOOLEAN.equals(localName)) {
                String source = reader.getElementText();
                return new XmlRpcBoolean(source);

            } else if (XmlRpcElementNames.DATE_TIME.equals(localName)) {
                String source = reader.getElementText();
                return new XmlRpcDateTime(source);

            } else if (XmlRpcElementNames.DOUBLE.equals(localName)) {
                String source = reader.getElementText();
                return new XmlRpcDouble(source);

            } else if (XmlRpcElementNames.I4.equals(localName) || XmlRpcElementNames.INT.equals(localName)) {
                String source = reader.getElementText();
                return new XmlRpcInteger(source);

            } else if (XmlRpcElementNames.STRING.equals(localName)
                    || XmlRpcElementNames.INT.equals(localName)) {
                String source = reader.getElementText();
                return new XmlRpcString(source);

            } else if (XmlRpcElementNames.STRUCT.equals(localName)) {
                return parseStructElement(reader);

            } else {
                XmlRpcParsingUtils.handleUnexpectedElementFound(localName);
            }

        case XMLStreamConstants.CHARACTERS:
            String source = reader.getText();
            return new XmlRpcString(source);
        }
    }

    // we should not reach this point.
    return null;
}

From source file:org.talend.dataprep.schema.xls.XlsUtils.java

/**
 * read workbook xml spec to get non hidden sheets
 *
 * @param inputStream/*from www  .  java2s.  c  o m*/
 * @return
 */
public static List<String> getActiveSheetsFromWorkbookSpec(InputStream inputStream) throws XMLStreamException {
    // If doesn't support mark, wrap up
    if (!inputStream.markSupported()) {
        inputStream = new PushbackInputStream(inputStream, 8);
    }
    XMLStreamReader streamReader = XML_INPUT_FACTORY.createXMLStreamReader(inputStream);
    try {
        /*
         *
         * <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <workbook
         * xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"
         * xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"> <fileVersion appName="xl"
         * lastEdited="5" lowestEdited="5" rupBuild="9303" codeName="{8C4F1C90-05EB-6A55-5F09-09C24B55AC0B}"/>
         * <workbookPr codeName="ThisWorkbook" defaultThemeVersion="124226"/> <bookViews> <workbookView xWindow="0"
         * yWindow="732" windowWidth="22980" windowHeight="8868" firstSheet="1" activeTab="8"/> </bookViews>
         * <sheets> <sheet name="formdata" sheetId="4" state="hidden" r:id="rId1"/> <sheet name="MONDAY" sheetId="1"
         * r:id="rId2"/> <sheet name="TUESDAY" sheetId="8" r:id="rId3"/> <sheet name="WEDNESDAY" sheetId="10"
         * r:id="rId4"/> <sheet name="THURSDAY" sheetId="11" r:id="rId5"/> <sheet name="FRIDAY" sheetId="12"
         * r:id="rId6"/> <sheet name="SATURDAY" sheetId="13" r:id="rId7"/> <sheet name="SUNDAY" sheetId="14"
         * r:id="rId8"/> <sheet name="WEEK SUMMARY" sheetId="15" r:id="rId9"/> </sheets>
         *
         */
        // we only want sheets not with state=hidden

        List<String> names = new ArrayList<>();

        while (streamReader.hasNext()) {
            switch (streamReader.next()) {
            case START_ELEMENT:
                if (StringUtils.equals(streamReader.getLocalName(), "sheet")) {
                    Map<String, String> attributesValues = getAttributesNameValue(streamReader);
                    if (!attributesValues.isEmpty()) {
                        String sheetState = attributesValues.get("state");
                        if (!StringUtils.equals(sheetState, "hidden")) {
                            String sheetName = attributesValues.get("name");
                            names.add(sheetName);
                        }
                    }
                }
                break;
            case XMLStreamConstants.END_ELEMENT:
                if (StringUtils.equals(streamReader.getLocalName(), "sheets")) {
                    // shortcut to stop parsing
                    return names;
                }
                break;
            default:
                // no op
            }
        }
        return names;
    } finally {
        if (streamReader != null) {
            streamReader.close();
        }
    }
}

From source file:org.talend.dataprep.schema.xls.XlsUtils.java

/**
 *
 * @param inputStream xls sheet inputStream
 * @return the column number from reading sheet metadata or -1 if unknown
 * @throws XMLStreamException//from  ww w . ja  va2s . c o  m
 * @throws IOException
 */
public static int getColumnsNumber(InputStream inputStream) throws XMLStreamException, IOException {
    // If doesn't support mark, wrap up
    if (!inputStream.markSupported()) {
        inputStream = new PushbackInputStream(inputStream, 8);
    }

    int colNumber = 0;

    // TDP-1781 xlsx files may not containing dimension so we fallback to col element number

    XMLStreamReader streamReader = XML_INPUT_FACTORY.createXMLStreamReader(inputStream);
    try {
        while (streamReader.hasNext()) {
            switch (streamReader.next()) {
            case START_ELEMENT:
                if (StringUtils.equals(streamReader.getLocalName(), "dimension")) {
                    Map<String, String> attributesValues = getAttributesNameValue(streamReader);
                    if (!attributesValues.isEmpty()) {
                        return getColumnsNumberFromDimension(attributesValues.get("ref"));
                    }
                }
                if (StringUtils.equals(streamReader.getLocalName(), "col")) {
                    colNumber++;
                }
                break;
            case END_ELEMENT:
                if (StringUtils.equals(streamReader.getLocalName(), "cols")) {
                    return colNumber;
                }
            default:
                // no op
            }
        }
    } finally {
        if (streamReader != null) {
            streamReader.close();
        }
    }
    return -1;
}

From source file:org.tobarsegais.webapp.data.Index.java

public static Index read(String bundle, XMLStreamReader reader) throws XMLStreamException {
    while (reader.hasNext() && !reader.isStartElement()) {
        reader.next();/*from  w w  w  .j a va 2s  . com*/
    }
    if (reader.getEventType() != XMLStreamConstants.START_ELEMENT) {
        throw new IllegalStateException("Expecting a start element");
    }
    if (!"index".equals(reader.getLocalName())) {
        throw new IllegalStateException("Expecting a <index> element, found a <" + reader.getLocalName() + ">");
    }
    List<IndexEntry> entries = new ArrayList<IndexEntry>();
    int depth = 0;
    while (reader.hasNext() && depth >= 0) {
        switch (reader.next()) {
        case XMLStreamConstants.START_ELEMENT:
            if (depth == 0 && "entry".equals(reader.getLocalName())) {
                entries.add(IndexEntry.read(bundle, Collections.<String>emptyList(), reader));
            } else {
                depth++;
            }
            break;
        case XMLStreamConstants.END_ELEMENT:
            depth--;
            break;
        }
    }
    return new Index(entries);
}

From source file:org.tobarsegais.webapp.data.Plugin.java

public static Plugin read(XMLStreamReader reader) throws XMLStreamException {
    while (reader.hasNext() && !reader.isStartElement()) {
        reader.next();//from  w w w.j a v a2 s . co m
    }
    if (reader.getEventType() != XMLStreamConstants.START_ELEMENT) {
        throw new IllegalStateException("Expecting a start element");
    }
    if (!"plugin".equals(reader.getLocalName())) {
        throw new IllegalStateException("Expecting a <plugin> element");
    }
    String name = reader.getAttributeValue(null, "name");
    String id = reader.getAttributeValue(null, "id");
    String version = reader.getAttributeValue(null, "version");
    String providerName = reader.getAttributeValue(null, "provider-name");
    List<Extension> extensions = new ArrayList<Extension>();
    int depth = 0;
    while (reader.hasNext() && depth >= 0) {
        switch (reader.next()) {
        case XMLStreamConstants.START_ELEMENT:
            if (depth == 0 && "extension".equals(reader.getLocalName())) {
                extensions.add(Extension.read(reader));
            } else {
                depth++;
            }
            break;
        case XMLStreamConstants.END_ELEMENT:
            depth--;
            break;
        }
    }
    return new Plugin(name, id, version, providerName, extensions);
}

From source file:org.tobarsegais.webapp.data.Toc.java

public static Toc read(XMLStreamReader reader) throws XMLStreamException {
    while (reader.hasNext() && !reader.isStartElement()) {
        reader.next();/*from  w  w w. jav  a2 s  .c o  m*/
    }
    if (reader.getEventType() != XMLStreamConstants.START_ELEMENT) {
        throw new IllegalStateException("Expecting a start element");
    }
    if (!"toc".equals(reader.getLocalName())) {
        throw new IllegalStateException("Expecting a <toc> element");
    }
    String label = reader.getAttributeValue(null, "label");
    String topic = reader.getAttributeValue(null, "topic");
    List<Topic> topics = new ArrayList<Topic>();
    int depth = 0;
    while (reader.hasNext() && depth >= 0) {
        switch (reader.next()) {
        case XMLStreamConstants.START_ELEMENT:
            if (depth == 0 && "topic".equals(reader.getLocalName())) {
                topics.add(Topic.read(reader));
            } else {
                depth++;
            }
            break;
        case XMLStreamConstants.END_ELEMENT:
            depth--;
            break;
        }
    }
    return new Toc(label, topic, topics);
}

From source file:org.unitedinternet.cosmo.util.DomReader.java

private static Element readElement(Document d, XMLStreamReader reader) throws XMLStreamException {
    Element e = null;//w  w  w  . ja  v a 2s  . c  o m

    String local = reader.getLocalName();
    String ns = reader.getNamespaceURI();
    if (ns != null && !ns.equals("")) {
        String prefix = reader.getPrefix();
        String qualified = prefix != null && !prefix.isEmpty() ? prefix + ":" + local : local;
        e = d.createElementNS(ns, qualified);
    } else {
        e = d.createElement(local);
    }

    //if (log.isDebugEnabled())
    //log.debug("Reading element " + e.getTagName());

    for (int i = 0; i < reader.getAttributeCount(); i++) {
        Attr a = readAttribute(i, d, reader);
        if (a.getNamespaceURI() != null) {
            e.setAttributeNodeNS(a);
        } else {
            e.setAttributeNode(a);
        }
    }

    return e;
}

From source file:org.ut.biolab.medsavant.client.plugin.AppController.java

private static AppDescriptor.PluginXMLElement readElement(XMLStreamReader reader) {
    try {/*  w  w w .j a va 2 s  . c  om*/
        String elemName = reader.getLocalName().toUpperCase();
        return Enum.valueOf(AppDescriptor.PluginXMLElement.class, elemName);
    } catch (IllegalArgumentException ignored) {
        // Any elements not in our enum will just be ignored.
        return AppDescriptor.PluginXMLElement.IGNORED;
    }
}

From source file:org.ut.biolab.medsavant.client.plugin.PluginIndex.java

public PluginIndex(URL url) throws IOException {
    urls = new HashMap<String, URL>();
    try {/*  w  ww  . j  ava 2 s  .co  m*/

        XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(
                ClientNetworkUtils.openStream(url, ClientNetworkUtils.NONCRITICAL_CONNECT_TIMEOUT,
                        ClientNetworkUtils.NONCRITICAL_READ_TIMEOUT));
        if (reader.getVersion() == null) {
            throw new XMLStreamException("Invalid XML at URL " + url);
        }
        boolean done = false;
        String id = null;
        do {
            if (reader.hasNext()) {
                int t = reader.next();
                switch (t) {
                case XMLStreamConstants.START_ELEMENT:
                    String elemName = reader.getLocalName();
                    if (elemName.equals("leaf")) {
                        id = reader.getAttributeValue(null, "id");
                    } else if (elemName.equals("url")) {
                        if (id != null) {
                            try {
                                urls.put(id, new URL(reader.getElementText()));
                            } catch (MalformedURLException x) {
                                LOG.warn(String.format("Unable to parse \"%s\" as a plugin URL.",
                                        reader.getElementText()));
                            }
                            id = null;
                        }
                    }
                    break;
                case XMLStreamConstants.END_DOCUMENT:
                    reader.close();
                    done = true;
                    break;
                }
            } else {
                throw new XMLStreamException("Malformed XML at " + url);
            }
        } while (!done);
    } catch (XMLStreamException x) {
        throw new IOException("Unable to get version number from web-site.", x);
    }
}

From source file:org.wso2.carbon.registry.core.jdbc.Repository.java

@SuppressWarnings("deprecation")
private void restoreRecursively(String path, XMLStreamReader xmlReader, DumpReader dumpReader,
        long currentVersion, boolean resourceExists) throws RegistryException, XMLStreamException {
    // we need to check the authorization with the current resource path at this point
    if (!AuthorizationUtils.authorize(path, ActionConstants.PUT)) {
        String msg = "Failed to check-in resource " + path + ". User " + CurrentSession.getUser()
                + " is not authorized to update " + "the current collection path " + path + ".";
        log.warn(msg);/*from w  ww .j a v  a  2  s . c o  m*/
        throw new AuthorizationFailedException(msg);
    }

    while (!xmlReader.isStartElement() && xmlReader.hasNext()) {
        xmlReader.next();
    }

    if (!xmlReader.hasNext()) {
        // nothing to parse
        return;
    }

    if (!xmlReader.getLocalName().equals(DumpConstants.RESOURCE)) {
        String msg = "Invalid dump to restore at " + path;
        log.error(msg);
        throw new RegistryException(msg);
    }

    String incomingParentPath = xmlReader.getAttributeValue(null, DumpConstants.RESOURCE_PATH);
    // the name of the resource is used instead of the path
    String resourceName = xmlReader.getAttributeValue(null, DumpConstants.RESOURCE_NAME);
    String ignoreConflictsStrValue = xmlReader.getAttributeValue(null, DumpConstants.IGNORE_CONFLICTS);

    boolean ignoreConflicts = true;
    if (ignoreConflictsStrValue != null && Boolean.toString(false).equals(ignoreConflictsStrValue)) {
        ignoreConflicts = false;
    }

    String isCollectionString = xmlReader.getAttributeValue(null, DumpConstants.RESOURCE_IS_COLLECTION);
    boolean isCollection = isCollectionString.equals(DumpConstants.RESOURCE_IS_COLLECTION_TRUE);

    if (path.equals(RegistryConstants.ROOT_PATH) && !isCollection) {
        // you can not put non collection to the root path
        String msg = "Illegal to restore a non-collection in place of root collection.";
        log.error(msg);
        throw new RegistryException(msg);
    }

    String status = xmlReader.getAttributeValue(null, DumpConstants.RESOURCE_STATUS);

    //if the status is deleted we delete that resource/collection
    if (DumpConstants.RESOURCE_DELETED.equals(status) && resourceExists) {
        delete(path);
        return;
    }

    ResourceImpl resourceImpl;
    byte[] contentBytes = new byte[0];
    if (isCollection) {
        resourceImpl = new CollectionImpl();
    } else {
        resourceImpl = new ResourceImpl();
    }

    List<CommentDO> commentDOList = new ArrayList<CommentDO>();
    List<TaggingDO> taggingDOList = new ArrayList<TaggingDO>();
    List<RatingDO> ratingDOList = new ArrayList<RatingDO>();
    List<Association> associationList = new ArrayList<Association>();

    boolean isCreatorExisting = false;
    boolean isCreatedTimeExisting = false;
    boolean isUpdaterExisting = false;
    boolean isUpdatedTimeExisting = false;
    long dumpingResourceVersion = -1;

    // traversing to the next element
    do {
        xmlReader.next();
    } while (!xmlReader.isStartElement() && xmlReader.hasNext());

    while (xmlReader.hasNext()) {
        String localName = xmlReader.getLocalName();

        // setMediaType
        if (localName.equals(DumpConstants.MEDIA_TYPE)) {
            String text = xmlReader.getElementText();
            if (text.indexOf('/') < 0) {
                text = MediaTypesUtils.getMediaType("dummy." + text);
            }
            if (text != null) {
                resourceImpl.setMediaType(text);
            }
            // now go to the next element
            do {
                xmlReader.next();
            } while (!xmlReader.isStartElement() && xmlReader.hasNext());
        }
        // creator
        else if (localName.equals(DumpConstants.CREATOR)) {
            String text = xmlReader.getElementText();
            if (text != null) {
                resourceImpl.setAuthorUserName(text);
                isCreatorExisting = true;
            }
            // now go to the next element
            do {
                xmlReader.next();
            } while (!xmlReader.isStartElement() && xmlReader.hasNext());
        }
        // version: just to keep track of the server changes
        else if (localName.equals(DumpConstants.VERSION)) {
            String text = xmlReader.getElementText();
            if (text != null) {
                dumpingResourceVersion = Long.parseLong(text);
            }
            // now go to the next element
            do {
                xmlReader.next();
            } while (!xmlReader.isStartElement() && xmlReader.hasNext());
        }
        // uuid: just to keep track of the server changes
        else if (localName.equals(DumpConstants.UUID)) {
            String text = xmlReader.getElementText();
            if (text != null) {
                resourceImpl.setUUID(text);
            }
            // now go to the next element
            do {
                xmlReader.next();
            } while (!xmlReader.isStartElement() && xmlReader.hasNext());
        }
        // createdTime
        else if (localName.equals(DumpConstants.CREATED_TIME)) {
            String text = xmlReader.getElementText();
            if (text != null) {
                long date = Long.parseLong(text);
                resourceImpl.setCreatedTime(new Date(date));
                isCreatedTimeExisting = true;
            }
            // now go to the next element
            do {
                xmlReader.next();
            } while (!xmlReader.isStartElement() && xmlReader.hasNext());
        }
        // setLastUpdater
        else if (localName.equals(DumpConstants.LAST_UPDATER)) {
            String text = xmlReader.getElementText();
            if (text != null) {
                resourceImpl.setLastUpdaterUserName(text);
                isUpdaterExisting = true;
            }
            // now go to the next element
            do {
                xmlReader.next();
            } while (!xmlReader.isStartElement() && xmlReader.hasNext());
        }
        // LastModified
        else if (localName.equals(DumpConstants.LAST_MODIFIED)) {
            String text = xmlReader.getElementText();
            if (text != null) {
                long date = Long.parseLong(text);
                resourceImpl.setLastModified(new Date(date));
                isUpdatedTimeExisting = true;
            }
            // now go to the next element
            do {
                xmlReader.next();
            } while (!xmlReader.isStartElement() && xmlReader.hasNext());
        }
        // get description
        else if (localName.equals(DumpConstants.DESCRIPTION)) {
            String text = xmlReader.getElementText();
            if (text != null) {
                resourceImpl.setDescription(text);
            }
            // now go to the next element
            do {
                xmlReader.next();
            } while (!xmlReader.isStartElement() && xmlReader.hasNext());
        }
        // get properties
        else if (localName.equals(DumpConstants.PROPERTIES)) {
            // iterating trying to find the children..
            do {
                xmlReader.next();
            } while (!xmlReader.isStartElement() && xmlReader.hasNext());
            while (xmlReader.hasNext() && xmlReader.getLocalName().equals(DumpConstants.PROPERTY_ENTRY)) {
                String key = xmlReader.getAttributeValue(null, DumpConstants.PROPERTY_ENTRY_KEY);
                String text = xmlReader.getElementText();
                if (text.equals("")) {
                    text = null;
                }
                if (text != null) {
                    resourceImpl.addPropertyWithNoUpdate(key, text);
                }
                do {
                    xmlReader.next();
                } while (!xmlReader.isStartElement() && xmlReader.hasNext());
            }
        }
        // get content
        else if (localName.equals(DumpConstants.CONTENT)) {
            String text = xmlReader.getElementText();
            // we keep content as base64 encoded
            if (text != null) {
                contentBytes = Base64.decode(text);
            }
            do {
                xmlReader.next();
            } while ((!xmlReader.isStartElement() && xmlReader.hasNext())
                    && !(xmlReader.isEndElement() && xmlReader.getLocalName().equals(DumpConstants.RESOURCE)));
        }
        // getting comment information
        else if (localName.equals(DumpConstants.COMMENTS)) {
            // iterating trying to find the children..
            do {
                xmlReader.next();
            } while (!xmlReader.isStartElement() && xmlReader.hasNext());
            while (xmlReader.hasNext() && xmlReader.getLocalName().equals(DumpConstants.COMMENT_ENTRY)) {
                CommentDO commentDO = new CommentDO();

                do {
                    xmlReader.next();
                } while (!xmlReader.isStartElement() && xmlReader.hasNext());

                localName = xmlReader.getLocalName();
                while (xmlReader.hasNext() && (localName.equals(DumpConstants.COMMENT_ENTRY_USER)
                        || localName.equals(DumpConstants.COMMENT_ENTRY_TEXT))) {
                    if (localName.equals(DumpConstants.COMMENT_ENTRY_USER)) {
                        String text = xmlReader.getElementText();
                        if (text != null) {
                            commentDO.setCommentedUser(text);
                        }
                    } else if (localName.equals(DumpConstants.COMMENT_ENTRY_TEXT)) {
                        String text = xmlReader.getElementText();
                        if (text != null) {
                            commentDO.setCommentText(text);
                        }
                    }

                    do {
                        xmlReader.next();
                    } while (!xmlReader.isStartElement() && xmlReader.hasNext());
                    if (xmlReader.hasNext()) {
                        localName = xmlReader.getLocalName();
                    }
                }
                commentDOList.add(commentDO);
            }
        }
        // getting tagging information
        else if (localName.equals(DumpConstants.TAGGINGS)) {
            // iterating trying to find the children..
            do {
                xmlReader.next();
            } while (!xmlReader.isStartElement() && xmlReader.hasNext());
            while (xmlReader.hasNext() && xmlReader.getLocalName().equals(DumpConstants.TAGGING_ENTRY)) {

                TaggingDO taggingDO = new TaggingDO();

                do {
                    xmlReader.next();
                } while (!xmlReader.isStartElement() && xmlReader.hasNext());

                localName = xmlReader.getLocalName();
                while (xmlReader.hasNext() && (localName.equals(DumpConstants.TAGGING_ENTRY_USER)
                        || localName.equals(DumpConstants.TAGGING_ENTRY_DATE)
                        || localName.equals(DumpConstants.TAGGING_ENTRY_TAG_NAME))) {
                    if (localName.equals(DumpConstants.TAGGING_ENTRY_USER)) {
                        String text = xmlReader.getElementText();
                        if (text != null) {
                            taggingDO.setTaggedUserName(text);
                        }
                    } else if (localName.equals(DumpConstants.TAGGING_ENTRY_DATE)) {
                        String text = xmlReader.getElementText();
                        if (text != null) {
                            long date = Long.parseLong(text);
                            taggingDO.setTaggedTime(new Date(date));
                        }
                    } else if (localName.equals(DumpConstants.TAGGING_ENTRY_TAG_NAME)) {
                        String text = xmlReader.getElementText();
                        if (text != null) {
                            taggingDO.setTagName(text);
                        }
                    }
                    do {
                        xmlReader.next();
                    } while (!xmlReader.isStartElement() && xmlReader.hasNext());
                    if (xmlReader.hasNext()) {
                        localName = xmlReader.getLocalName();
                    }
                }
                taggingDOList.add(taggingDO);
            }
        }
        // getting rating information
        else if (localName.equals(DumpConstants.RATINGS)) {
            // iterating trying to find the children..
            do {
                xmlReader.next();
            } while (!xmlReader.isStartElement() && xmlReader.hasNext());
            while (xmlReader.hasNext() && xmlReader.getLocalName().equals(DumpConstants.RATING_ENTRY)) {
                RatingDO ratingDO = new RatingDO();

                do {
                    xmlReader.next();
                } while (!xmlReader.isStartElement() && xmlReader.hasNext());

                localName = xmlReader.getLocalName();
                while (xmlReader.hasNext() && (localName.equals(DumpConstants.RATING_ENTRY_USER)
                        || localName.equals(DumpConstants.RATING_ENTRY_DATE)
                        || localName.equals(DumpConstants.RATING_ENTRY_RATE))) {
                    if (localName.equals(DumpConstants.RATING_ENTRY_USER)) {
                        String text = xmlReader.getElementText();
                        if (text != null) {
                            ratingDO.setRatedUserName(text);
                        }
                    } else if (localName.equals(DumpConstants.RATING_ENTRY_DATE)) {
                        String text = xmlReader.getElementText();
                        if (text != null) {
                            long date = Long.parseLong(text);
                            ratingDO.setRatedTime(new Date(date));
                        }
                    } else if (localName.equals(DumpConstants.RATING_ENTRY_RATE)) {
                        String text = xmlReader.getElementText();
                        if (text != null) {
                            int ratingValue = Integer.parseInt(text);
                            ratingDO.setRating(ratingValue);
                        }
                    }
                    do {
                        xmlReader.next();
                    } while (!xmlReader.isStartElement() && xmlReader.hasNext());
                    if (xmlReader.hasNext()) {
                        localName = xmlReader.getLocalName();
                    }
                }
                ratingDOList.add(ratingDO);
            }
        }

        // getting rating information
        else if (localName.equals(DumpConstants.ASSOCIATIONS)) {
            // iterating trying to find the children..
            do {
                xmlReader.next();
            } while (!xmlReader.isStartElement() && xmlReader.hasNext());
            while (xmlReader.hasNext() && xmlReader.getLocalName().equals(DumpConstants.ASSOCIATION_ENTRY)) {
                String source = null;
                String destination = null;
                String type = null;

                do {
                    xmlReader.next();
                } while (!xmlReader.isStartElement() && xmlReader.hasNext());

                localName = xmlReader.getLocalName();
                while (xmlReader.hasNext() && (localName.equals(DumpConstants.ASSOCIATION_ENTRY_SOURCE)
                        || localName.equals(DumpConstants.ASSOCIATION_ENTRY_DESTINATION)
                        || localName.equals(DumpConstants.ASSOCIATION_ENTRY_TYPE))) {
                    if (localName.equals(DumpConstants.ASSOCIATION_ENTRY_SOURCE)) {
                        String text = xmlReader.getElementText();
                        if (text != null) {
                            source = text;
                        }
                    } else if (localName.equals(DumpConstants.ASSOCIATION_ENTRY_DESTINATION)) {
                        String text = xmlReader.getElementText();
                        if (text != null) {
                            destination = text;
                        }
                    } else if (localName.equals(DumpConstants.ASSOCIATION_ENTRY_TYPE)) {
                        String text = xmlReader.getElementText();
                        if (text != null) {
                            type = text;
                        }
                    }
                    do {
                        xmlReader.next();
                    } while (!xmlReader.isStartElement() && xmlReader.hasNext());
                    if (xmlReader.hasNext()) {
                        localName = xmlReader.getLocalName();
                    }
                }
                // get the source and destination as absolute paths
                source = RegistryUtils.getAbsoluteAssociationPath(source, path);
                if (destination.startsWith(DumpConstants.EXTERNAL_ASSOCIATION_DESTINATION_PREFIX)) {
                    destination = destination
                            .substring(DumpConstants.EXTERNAL_ASSOCIATION_DESTINATION_PREFIX.length());
                } else {
                    destination = RegistryUtils.getAbsoluteAssociationPath(destination, path);
                }
                associationList.add(new Association(source, destination, type));
            }
        }
        // getting children, just storing in array list now, will used at the end
        // we are keeping old name to keep backward compatibility.
        else if (localName.equals(DumpConstants.CHILDREN) || localName.equals(DumpConstants.CHILDS)) {
            // we keep the stream to call this function recursively
            break;
        } else if (localName.equals(DumpConstants.RESOURCE)) {
            // we keep the stream to call this function recursively
            break;
        } else {
            // we don't mind having unwanted elements, now go to the next element
            do {
                xmlReader.next();
            } while (!xmlReader.isStartElement() && xmlReader.hasNext());
        }
    }
    if (!ignoreConflicts) {
        // so we handling the conflicts.
        if (dumpingResourceVersion > 0) {
            if (currentVersion == -1) {
                // the current version == -1 means the resource is deleted in the server
                // but since the client is sending a version number, it has a previously checkout
                // resource
                String msg = "Resource is deleted in the server, resource path: " + path + ".";
                log.error(msg);
                throw new RegistryException(msg);
            }
            // we should check whether our dump is up-to-date
            if (currentVersion > dumpingResourceVersion) {
                // that mean the current resource is updated before the current version
                // so we have to notify user to get an update
                String msg = "Resource is in a newer version than the restoring version. " + "resource path: "
                        + path + ".";
                log.error(msg);
                throw new RegistryException(msg);
            }
        }
    }

    // completing the empty fields
    if (!isCreatorExisting) {
        String creator = CurrentSession.getUser();
        resourceImpl.setAuthorUserName(creator);
    }
    if (!isCreatedTimeExisting) {
        long now = System.currentTimeMillis();
        resourceImpl.setCreatedTime(new Date(now));
    }
    if (!isUpdaterExisting) {
        String updater = CurrentSession.getUser();
        resourceImpl.setLastUpdaterUserName(updater);
    }
    if (!isUpdatedTimeExisting) {
        long now = System.currentTimeMillis();
        resourceImpl.setLastModified(new Date(now));
    }

    if (resourceImpl.getUUID() == null) {
        setUUIDForResource(resourceImpl);
    }

    // create sym links
    String linkRestoration = resourceImpl.getProperty(RegistryConstants.REGISTRY_LINK_RESTORATION);
    if (linkRestoration != null) {
        String[] parts = linkRestoration.split(RegistryConstants.URL_SEPARATOR);
        if (parts.length == 4) {
            if (parts[2] != null && parts[2].length() == 0) {
                parts[2] = null;
            }
            if (parts[0] != null && parts[1] != null && parts[3] != null) {
                RegistryUtils.registerHandlerForRemoteLinks(RegistryContext.getBaseInstance(), parts[0],
                        parts[1], parts[2], parts[3]);
            }
        } else if (parts.length == 3) {
            // here parts[0] the current path, path[1] is the target path.
            if (parts[0] != null && parts[1] != null) {
                // first we are calculating the relative path of path[1] to path[0]
                String relativeTargetPath = RegistryUtils.getRelativeAssociationPath(parts[1], parts[0]);
                // then we derive the absolute path with reference to the current path.
                String absoluteTargetPath = RegistryUtils.getAbsoluteAssociationPath(relativeTargetPath, path);
                RegistryUtils.registerHandlerForSymbolicLinks(RegistryContext.getBaseInstance(), path,
                        absoluteTargetPath, parts[2]);
            }
        }
    }

    synchronized (this) {
        ResourceIDImpl resourceID = null;
        ResourceDO resourceDO = null;
        if (resourceDAO.resourceExists(path)) {
            resourceID = resourceDAO.getResourceID(path);
            resourceDO = resourceDAO.getResourceDO(resourceID);
            if (resourceDO == null) {
                if (isCollection) {
                    resourceID = resourceDAO.getResourceID(path, isCollection);
                    if (resourceID != null) {
                        resourceDO = resourceDAO.getResourceDO(resourceID);
                    }
                }
                if (resourceDO == null) {
                    return;
                }
            }
        }

        if (DumpConstants.RESOURCE_UPDATED.equals(status) || DumpConstants.RESOURCE_ADDED.equals(status)
                || DumpConstants.RESOURCE_DUMP.equals(status)) {
            if (resourceDAO.resourceExists(path)) {
                if (DumpConstants.RESOURCE_DUMP.equals(status)) {
                    delete(path);
                } else {
                    deleteNode(resourceID, resourceDO, true);
                }
            }
            if (resourceID == null) {
                // need to create a resourceID
                String parentPath = RegistryUtils.getParentPath(path);

                ResourceIDImpl parentResourceID = resourceDAO.getResourceID(parentPath, true);
                if (parentResourceID == null || !resourceDAO.resourceExists(parentResourceID)) {
                    addEmptyCollection(parentPath);
                    if (parentResourceID == null) {
                        parentResourceID = resourceDAO.getResourceID(parentPath, true);
                    }
                }
                resourceDAO.createAndApplyResourceID(path, parentResourceID, resourceImpl);
            } else {
                resourceImpl.setPathID(resourceID.getPathID());
                resourceImpl.setPath(path);
                resourceImpl.setName(resourceID.getName());
            }

            // adding resource followed by content (for nonCollection)
            if (!isCollection) {
                int contentId = 0;
                if (contentBytes.length > 0) {
                    contentId = resourceDAO.addContentBytes(new ByteArrayInputStream(contentBytes));
                }
                resourceImpl.setDbBasedContentID(contentId);
            }

            resourceDO = resourceImpl.getResourceDO();
            resourceDAO.addResourceDO(resourceDO);
            resourceImpl.setVersionNumber(resourceDO.getVersion());

            // adding the properties.
            resourceDAO.addProperties(resourceImpl);

            // adding comments
            commentsDAO.addComments(resourceImpl, commentDOList.toArray(new CommentDO[commentDOList.size()]));

            // adding tags
            tagsDAO.addTaggings(resourceImpl, taggingDOList.toArray(new TaggingDO[taggingDOList.size()]));

            // adding ratings
            ratingsDAO.addRatings(resourceImpl, ratingDOList.toArray(new RatingDO[ratingDOList.size()]));

            for (Association association : associationList) {
                associationDAO.addAssociation(association.getSourcePath(), association.getDestinationPath(),
                        association.getAssociationType());
            }
        }
    }

    if (!xmlReader.hasNext() || !(xmlReader.getLocalName().equals(DumpConstants.CHILDREN)
            || xmlReader.getLocalName().equals(DumpConstants.CHILDS))) {
        // finished the recursion
        return;
    }

    do {
        xmlReader.next();
        if (xmlReader.isEndElement() && (xmlReader.getLocalName().equals(DumpConstants.CHILDREN)
                || xmlReader.getLocalName().equals(DumpConstants.CHILDS))) {
            // this means empty children, just quit from here
            // before that we have to set the cursor to the start of the next element
            if (xmlReader.hasNext()) {
                do {
                    xmlReader.next();
                } while ((!xmlReader.isStartElement() && xmlReader.hasNext()) && !(xmlReader.isEndElement()
                        && xmlReader.getLocalName().equals(DumpConstants.RESOURCE)));
            }
            Resource resource = get(path);
            if (resource instanceof Collection) {
                String[] existingChildren = ((Collection) resource).getChildren();
                for (String existingChild : existingChildren) {
                    delete(existingChild);
                }
            }
            return;
        }
    } while (!xmlReader.isStartElement() && xmlReader.hasNext());

    int i = 0;
    if (xmlReader.hasNext() && xmlReader.getLocalName().equals(DumpConstants.RESOURCE)) {
        Set<String> childPathSet = new HashSet<String>();
        while (true) {
            if (i != 0) {
                dumpReader.setReadingChildResourceIndex(i);

                // otherwise we will set the stuff for the next resource
                // get an xlm reader in the checking child by parent mode.
                xmlReader = XMLInputFactory.newInstance().createXMLStreamReader(dumpReader);

                while (!xmlReader.isStartElement() && xmlReader.hasNext()) {
                    xmlReader.next();
                }
            }

            String absoluteChildPath;
            if (incomingParentPath != null) {
                // the code to support backward compatibility.
                // prepare the children absolute path
                String incomingChildPath = xmlReader.getAttributeValue(null, DumpConstants.RESOURCE_PATH);
                /*                    if (!incomingChildPath.startsWith(incomingParentPath)) {
                //break;
                                    }*/

                String relativeChildPath;
                if (incomingParentPath.equals(RegistryConstants.ROOT_PATH)) {
                    relativeChildPath = incomingChildPath;
                } else {
                    if (incomingParentPath.contains(incomingChildPath)) {
                        relativeChildPath = incomingChildPath.substring(incomingParentPath.length());
                    } else {
                        // this happens only at some custom editing of dump.xml
                        relativeChildPath = null;
                    }
                }
                if (relativeChildPath != null) {
                    if (path.equals(RegistryConstants.ROOT_PATH)) {
                        absoluteChildPath = relativeChildPath;
                    } else {
                        absoluteChildPath = path + relativeChildPath;
                    }
                } else {
                    String checkoutRoot = path.substring(0, path.length() - incomingParentPath.length());
                    absoluteChildPath = checkoutRoot + incomingChildPath;
                }
            } else if (resourceName != null) {
                String childName = xmlReader.getAttributeValue(null, DumpConstants.RESOURCE_NAME);
                absoluteChildPath = path
                        + (path.equals(RegistryConstants.ROOT_PATH) ? "" : RegistryConstants.PATH_SEPARATOR)
                        + childName;
            } else {
                String msg = "Error in deriving the child paths for collection. path: " + path + ".";
                log.error(msg);
                throw new RegistryException(msg);
            }

            // we give the control back to the child.
            dumpReader.setCheckingChildByParent(false);

            dumpReader.setReadingChildResourceIndex(i);
            // call the check in method recursively

            recursionRepository.restoreRecursively(absoluteChildPath, dumpReader);
            childPathSet.add(absoluteChildPath);

            dumpReader.setCheckingChildByParent(true);
            try {
                if (dumpReader.isLastResource(i)) {
                    dumpReader.setCheckingChildByParent(false);
                    break;
                }
            } catch (IOException e) {
                String msg = "Error in checking the last resource exists.";
                log.error(msg, e);
                throw new RegistryException(msg + e.getMessage(), e);
            }
            // by this time i ++ child resource should exist
            i++;
        }
        Collection parent = (Collection) get(path);
        String[] existingChildren = parent.getChildren();
        for (String existingChild : existingChildren) {
            if (!childPathSet.contains(existingChild)) {
                delete(existingChild);
            }
        }
    }
}