List of usage examples for javax.xml.stream XMLStreamReader getAttributeValue
public String getAttributeValue(String namespaceURI, String localName);
From source file:org.wso2.carbon.registry.synchronization.Utils.java
/** * This method reads the xml stream up to the children and return the meta element. * * @param xmlReader the xml reader.//from www . ja va 2 s.c o m * * @return the meta element. * @throws SynchronizationException if the provided XML is invalid. * @throws XMLStreamException if XML parsing failed. */ public static OMElement readMetaElement(XMLStreamReader xmlReader) throws SynchronizationException, XMLStreamException { try { while (!xmlReader.isStartElement() && xmlReader.hasNext()) { xmlReader.next(); } if (!xmlReader.hasNext()) { // nothing to parse return null; } if (!xmlReader.getLocalName().equals(Utils.ELEM_RESOURCE)) { throw new SynchronizationException(MessageCode.INVALID_DUMP_CREATE_META_FILE); } // alerting non-backward compatibility... // String pathAttribute = xmlReader.getAttributeValue(null, ATTR_PATH); // if (pathAttribute != null) { // throw new SynchronizationException(MessageCode.CHECKOUT_OLD_VERSION); // } OMFactory factory = OMAbstractFactory.getOMFactory(); OMElement root = factory.createOMElement(new QName(Utils.ELEM_RESOURCE)); String resourceName = xmlReader.getAttributeValue(null, ATTR_NAME); String isCollectionString = xmlReader.getAttributeValue(null, ATTR_IS_COLLECTION); root.addAttribute(ATTR_NAME, resourceName, null); root.addAttribute(ATTR_IS_COLLECTION, isCollectionString, null); // traversing to the next element do { xmlReader.next(); } while (!xmlReader.isStartElement() && xmlReader.hasNext()); while (xmlReader.hasNext()) { String localName = xmlReader.getLocalName(); // version if (localName.equals(ELEM_VERSION)) { String text = xmlReader.getElementText(); OMElement versionElement = factory.createOMElement(new QName(ELEM_VERSION)); if (text != null) { versionElement.setText(text); } root.addChild(versionElement); // now go to the next element do { xmlReader.next(); } while (!xmlReader.isStartElement() && xmlReader.hasNext() && !(xmlReader.isEndElement() && xmlReader.getLocalName().equals(Utils.ELEM_RESOURCE))); } // setMediaType else if (localName.equals(ELEM_MEDIA_TYPE)) { String text = xmlReader.getElementText(); OMElement mediaTypeElement = factory.createOMElement(new QName(ELEM_MEDIA_TYPE)); if (text != null) { mediaTypeElement.setText(text); } root.addChild(mediaTypeElement); // now go to the next element do { xmlReader.next(); } while (!xmlReader.isStartElement() && xmlReader.hasNext() && !(xmlReader.isEndElement() && xmlReader.getLocalName().equals(Utils.ELEM_RESOURCE))); } // creator else if (localName.equals(ELEM_CREATOR)) { String text = xmlReader.getElementText(); OMElement creatorElement = factory.createOMElement(new QName(ELEM_CREATOR)); if (text != null) { creatorElement.setText(text); } root.addChild(creatorElement); // now go to the next element do { xmlReader.next(); } while (!xmlReader.isStartElement() && xmlReader.hasNext() && !(xmlReader.isEndElement() && xmlReader.getLocalName().equals(Utils.ELEM_RESOURCE))); } // createdTime else if (localName.equals(ELEM_CREATED_TIME)) { String text = xmlReader.getElementText(); OMElement createdTimeElement = factory.createOMElement(new QName(ELEM_CREATED_TIME)); if (text != null) { createdTimeElement.setText(text); } root.addChild(createdTimeElement); // now go to the next element do { xmlReader.next(); } while (!xmlReader.isStartElement() && xmlReader.hasNext() && !(xmlReader.isEndElement() && xmlReader.getLocalName().equals(Utils.ELEM_RESOURCE))); } // createdTime else if (localName.equals(ELEM_CONTENT)) { // currently we are keeping the content within the root element, and remove it // later. Before Carbon 3.0.0 the content was in the middle of the other // resource attributes String text = xmlReader.getElementText(); OMElement contentElement = factory.createOMElement(new QName(ELEM_CONTENT)); if (text != null) { contentElement.setText(text); } root.addChild(contentElement); // now go to the next element do { xmlReader.next(); } while (!xmlReader.isStartElement() && xmlReader.hasNext() && !(xmlReader.isEndElement() && xmlReader.getLocalName().equals(Utils.ELEM_RESOURCE))); } // setLastUpdater else if (localName.equals(ELEM_LAST_UPDATER)) { String text = xmlReader.getElementText(); OMElement lastUpdaterElement = factory.createOMElement(new QName(ELEM_LAST_UPDATER)); if (text != null) { lastUpdaterElement.setText(text); } root.addChild(lastUpdaterElement); // now go to the next element do { xmlReader.next(); } while (!xmlReader.isStartElement() && xmlReader.hasNext() && !(xmlReader.isEndElement() && xmlReader.getLocalName().equals(Utils.ELEM_RESOURCE))); } // LastModified else if (localName.equals(ELEM_LAST_MODIFIED)) { String text = xmlReader.getElementText(); OMElement lastModifiedElement = factory.createOMElement(new QName(ELEM_LAST_MODIFIED)); if (text != null) { lastModifiedElement.setText(text); } root.addChild(lastModifiedElement); // now go to the next element do { xmlReader.next(); } while (!xmlReader.isStartElement() && xmlReader.hasNext() && !(xmlReader.isEndElement() && xmlReader.getLocalName().equals(Utils.ELEM_RESOURCE))); } // get description else if (localName.equals(ELEM_DESCRIPTION)) { String text = xmlReader.getElementText(); OMElement description = factory.createOMElement(new QName(ELEM_DESCRIPTION)); if (text != null) { description.setText(text); } root.addChild(description); // now go to the next element do { xmlReader.next(); } while (!xmlReader.isStartElement() && xmlReader.hasNext() && !(xmlReader.isEndElement() && xmlReader.getLocalName().equals(Utils.ELEM_RESOURCE))); // now go to the next element while // (!xmlReader.isStartElement() && xmlReader.hasNext()); } // get uuid else if (localName.equals(ELEM_UUID)) { String text = xmlReader.getElementText(); OMElement description = factory.createOMElement(new QName(ELEM_UUID)); if (text != null) { description.setText(text); } root.addChild(description); // now go to the next element do { xmlReader.next(); } while (!xmlReader.isStartElement() && xmlReader.hasNext() && !(xmlReader.isEndElement() && xmlReader.getLocalName().equals(Utils.ELEM_RESOURCE))); // now go to the next element while // (!xmlReader.isStartElement() && xmlReader.hasNext()); } // get properties else if (localName.equals(ELEM_PROPERTIES)) { // iterating trying to find the children.. OMElement properties = factory.createOMElement(new QName(ELEM_PROPERTIES)); root.addChild(properties); do { xmlReader.next(); } while (!xmlReader.isStartElement() && xmlReader.hasNext() && !(xmlReader.isEndElement() && xmlReader.getLocalName().equals(Utils.ELEM_RESOURCE))); while (xmlReader.hasNext() && xmlReader.getLocalName().equals(ELEM_PROPERTY)) { String key = xmlReader.getAttributeValue(null, ATTR_KEY); String text = xmlReader.getElementText(); OMElement property = factory.createOMElement(new QName(ELEM_PROPERTY)); property.addAttribute(ATTR_KEY, key, null); properties.addChild(property); if (text.equals("")) { text = null; } if (text != null) { property.setText(text); } do { xmlReader.next(); } while (!xmlReader.isStartElement() && xmlReader.hasNext() && !(xmlReader.isEndElement() && xmlReader.getLocalName().equals(Utils.ELEM_RESOURCE))); } // getting comment information } else if (localName.equals(ELEM_COMMENTS)) { // iterating trying to find the children.. OMElement commentsElement = factory.createOMElement(new QName(ELEM_COMMENTS)); root.addChild(commentsElement); do { xmlReader.next(); } while (!xmlReader.isStartElement() && xmlReader.hasNext()); while (xmlReader.hasNext() && xmlReader.getLocalName().equals(ELEM_COMMENT)) { do { xmlReader.next(); } while (!xmlReader.isStartElement() && xmlReader.hasNext() && !(xmlReader.isEndElement() && xmlReader.getLocalName().equals(Utils.ELEM_RESOURCE))); localName = xmlReader.getLocalName(); OMElement commentElement = factory.createOMElement(new QName(ELEM_COMMENT)); commentsElement.addChild(commentElement); while (xmlReader.hasNext() && (localName.equals(ELEM_USER) || localName.equals(ELEM_TEXT))) { if (localName.equals(ELEM_USER)) { String text = xmlReader.getElementText(); if (text != null) { OMElement userElement = factory.createOMElement(new QName(ELEM_USER)); userElement.setText(text); commentElement.addChild(userElement); } } else if (localName.equals(ELEM_TEXT)) { String text = xmlReader.getElementText(); if (text != null) { OMElement textElement = factory.createOMElement(new QName(ELEM_TEXT)); textElement.setText(text); commentElement.addChild(textElement); } } do { xmlReader.next(); } while (!xmlReader.isStartElement() && xmlReader.hasNext() && !(xmlReader.isEndElement() && xmlReader.getLocalName().equals(Utils.ELEM_RESOURCE))); if (xmlReader.hasNext()) { localName = xmlReader.getLocalName(); } } } } // getting tagging information else if (localName.equals(ELEM_TAGGINGS)) { // iterating trying to find the children.. OMElement taggingsElement = factory.createOMElement(new QName(ELEM_TAGGINGS)); root.addChild(taggingsElement); do { xmlReader.next(); } while (!xmlReader.isStartElement() && xmlReader.hasNext() && !(xmlReader.isEndElement() && xmlReader.getLocalName().equals(Utils.ELEM_RESOURCE))); while (xmlReader.hasNext() && xmlReader.getLocalName().equals(ELEM_TAGGING)) { do { xmlReader.next(); } while (!xmlReader.isStartElement() && xmlReader.hasNext() && !(xmlReader.isEndElement() && xmlReader.getLocalName().equals(Utils.ELEM_RESOURCE))); localName = xmlReader.getLocalName(); OMElement taggingElement = factory.createOMElement(new QName(ELEM_TAGGING)); taggingsElement.addChild(taggingElement); while (xmlReader.hasNext() && (localName.equals(ELEM_USER) || localName.equals(ELEM_DATE) || localName.equals(ELEM_TAG_NAME))) { if (localName.equals(ELEM_USER)) { String text = xmlReader.getElementText(); if (text != null) { OMElement userElement = factory.createOMElement(new QName(ELEM_USER)); userElement.setText(text); taggingElement.addChild(userElement); } } else if (localName.equals(ELEM_DATE)) { String text = xmlReader.getElementText(); if (text != null) { OMElement dateElement = factory.createOMElement(new QName(ELEM_DATE)); dateElement.setText(text); taggingElement.addChild(dateElement); } } else if (localName.equals(ELEM_TAG_NAME)) { String text = xmlReader.getElementText(); if (text != null) { OMElement tagNameElement = factory.createOMElement(new QName(ELEM_TAG_NAME)); tagNameElement.setText(text); taggingElement.addChild(tagNameElement); } } do { xmlReader.next(); } while (!xmlReader.isStartElement() && xmlReader.hasNext() && !(xmlReader.isEndElement() && xmlReader.getLocalName().equals(Utils.ELEM_RESOURCE))); if (xmlReader.hasNext()) { localName = xmlReader.getLocalName(); } } } } // getting rating information else if (localName.equals(ELEM_RATINGS)) { // iterating trying to find the children.. OMElement ratingsElement = factory.createOMElement(new QName(ELEM_RATINGS)); root.addChild(ratingsElement); do { xmlReader.next(); } while (!xmlReader.isStartElement() && xmlReader.hasNext() && !(xmlReader.isEndElement() && xmlReader.getLocalName().equals(Utils.ELEM_RESOURCE))); while (xmlReader.hasNext() && xmlReader.getLocalName().equals(ELEM_RATING)) { do { xmlReader.next(); } while (!xmlReader.isStartElement() && xmlReader.hasNext()); localName = xmlReader.getLocalName(); OMElement ratingElement = factory.createOMElement(new QName(ELEM_RATING)); ratingsElement.addChild(ratingElement); while (xmlReader.hasNext() && (localName.equals(ELEM_USER) || localName.equals(ELEM_DATE) || localName.equals(ELEM_RATE))) { if (localName.equals(ELEM_USER)) { String text = xmlReader.getElementText(); if (text != null) { OMElement userElement = factory.createOMElement(new QName(ELEM_USER)); userElement.setText(text); ratingElement.addChild(userElement); } } else if (localName.equals(ELEM_DATE)) { String text = xmlReader.getElementText(); if (text != null) { OMElement dateElement = factory.createOMElement(new QName(ELEM_DATE)); dateElement.setText(text); ratingElement.addChild(dateElement); } } else if (localName.equals(ELEM_RATE)) { String text = xmlReader.getElementText(); if (text != null) { OMElement rateElement = factory.createOMElement(new QName(ELEM_RATE)); rateElement.setText(text); ratingElement.addChild(rateElement); } } do { xmlReader.next(); } while (!xmlReader.isStartElement() && xmlReader.hasNext() && !(xmlReader.isEndElement() && xmlReader.getLocalName().equals(Utils.ELEM_RESOURCE))); if (xmlReader.hasNext()) { localName = xmlReader.getLocalName(); } } } } // getting rating information else if (localName.equals(ELEM_ASSOCIATIONS)) { // iterating trying to find the children.. OMElement associationsElement = factory.createOMElement(new QName(ELEM_ASSOCIATIONS)); root.addChild(associationsElement); do { xmlReader.next(); } while (!xmlReader.isStartElement() && xmlReader.hasNext() && !(xmlReader.isEndElement() && xmlReader.getLocalName().equals(Utils.ELEM_RESOURCE))); while (xmlReader.hasNext() && xmlReader.getLocalName().equals(ELEM_ASSOCIATION)) { do { xmlReader.next(); } while (!xmlReader.isStartElement() && xmlReader.hasNext() && !(xmlReader.isEndElement() && xmlReader.getLocalName().equals(Utils.ELEM_RESOURCE))); localName = xmlReader.getLocalName(); OMElement associationElement = factory.createOMElement(new QName(ELEM_ASSOCIATION)); associationsElement.addChild(associationElement); while (xmlReader.hasNext() && (localName.equals(ELEM_SOURCE) || localName.equals(ELEM_DESTINATION) || localName.equals(ELEM_TYPE))) { if (localName.equals(ELEM_SOURCE)) { String text = xmlReader.getElementText(); if (text != null) { OMElement sourceElement = factory.createOMElement(new QName(ELEM_SOURCE)); sourceElement.setText(text); associationElement.addChild(sourceElement); } } else if (localName.equals(ELEM_DESTINATION)) { String text = xmlReader.getElementText(); if (text != null) { OMElement destinationElement = factory .createOMElement(new QName(ELEM_DESTINATION)); destinationElement.setText(text); associationElement.addChild(destinationElement); } } else if (localName.equals(ELEM_TYPE)) { String text = xmlReader.getElementText(); if (text != null) { OMElement typeElement = factory.createOMElement(new QName(ELEM_TYPE)); typeElement.setText(text); associationElement.addChild(typeElement); } } do { xmlReader.next(); } while (!xmlReader.isStartElement() && xmlReader.hasNext() && !(xmlReader.isEndElement() && xmlReader.getLocalName().equals(Utils.ELEM_RESOURCE))); if (xmlReader.hasNext()) { localName = xmlReader.getLocalName(); } } } } else if (localName.equals(ELEM_CHILDREN) || localName.equals(Utils.ELEM_RESOURCE)) { // checking the children or content element to terminate the check. break; } else { // we do mind having unwanted elements, now go to the next element break; } if ((xmlReader.isEndElement() && xmlReader.getLocalName().equals(Utils.ELEM_RESOURCE))) { // here we come the end of the resource tag break; } } return root; } catch (XMLStreamException e) { throw new SynchronizationException(MessageCode.ERROR_IN_READING_STREAM_TO_CREATE_META_FILE, e); } }
From source file:org.wso2.carbon.repository.core.ResourceStorer.java
private void restoreRecursively(String path, XMLStreamReader xmlReader, DumpReader dumpReader, long currentVersion, boolean resourceExists) throws RepositoryException, XMLStreamException { while (!xmlReader.isStartElement() && xmlReader.hasNext()) { xmlReader.next();/*from w ww . j a v a 2 s . c om*/ } if (!xmlReader.hasNext()) { return; } if (!xmlReader.getLocalName().equals(DumpConstants.RESOURCE)) { String msg = "Invalid dump to restore at " + path; log.error(msg); throw new RepositoryException(msg); } String incomingParentPath = xmlReader.getAttributeValue(null, DumpConstants.RESOURCE_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(RepositoryConstants.ROOT_PATH) && !isCollection) { String msg = "Illegal to restore a non-collection in place of root collection."; log.error(msg); throw new RepositoryException(msg); } String status = xmlReader.getAttributeValue(null, DumpConstants.RESOURCE_STATUS); 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(); } boolean isCreatorExisting = false; boolean isCreatedTimeExisting = false; boolean isUpdaterExisting = false; boolean isUpdatedTimeExisting = false; long dumpingResourceVersion = -1; 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()); } 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 = DatatypeConverter.parseBase64Binary(text); } do { xmlReader.next(); } while ((!xmlReader.isStartElement() && xmlReader.hasNext()) && !(xmlReader.isEndElement() && xmlReader.getLocalName().equals(DumpConstants.RESOURCE))); } // 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 = RepositoryUtils.getAbsoluteAssociationPath(source, path); if (destination.startsWith(DumpConstants.EXTERNAL_ASSOCIATION_DESTINATION_PREFIX)) { destination = destination .substring(DumpConstants.EXTERNAL_ASSOCIATION_DESTINATION_PREFIX.length()); } else { destination = RepositoryUtils.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 RepositoryException(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 RepositoryException(msg); } } } // completing the empty fields if (!isCreatorExisting) { String creator = CurrentContext.getUser(); resourceImpl.setAuthorUserName(creator); } if (!isCreatedTimeExisting) { long now = System.currentTimeMillis(); resourceImpl.setCreatedTime(new Date(now)); } if (!isUpdaterExisting) { String updater = CurrentContext.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.getPropertyValue(InternalConstants.REGISTRY_LINK_RESTORATION); if (linkRestoration != null) { String[] parts = linkRestoration.split(RepositoryConstants.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) { InternalUtils.registerHandlerForRemoteLinks(RepositoryContext.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 = RepositoryUtils.getRelativeAssociationPath(parts[1], parts[0]); // then we derive the absolute path with reference to the current path. String absoluteTargetPath = RepositoryUtils.getAbsoluteAssociationPath(relativeTargetPath, path); InternalUtils.registerHandlerForSymbolicLinks(RepositoryContext.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 = RepositoryUtils.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.setName(resourceID.getName()); resourceImpl.setPath(path); } // 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); } } 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).getChildPaths(); 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(RepositoryConstants.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(RepositoryConstants.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(RepositoryConstants.ROOT_PATH) ? "" : RepositoryConstants.PATH_SEPARATOR) + childName; } else { String msg = "Error in deriving the child paths for collection. path: " + path + "."; log.error(msg); throw new RepositoryException(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 RepositoryException(msg + e.getMessage(), e); } // by this time i ++ child resource should exist i++; } Collection parent = (Collection) get(path); String[] existingChildren = parent.getChildPaths(); for (String existingChild : existingChildren) { if (!childPathSet.contains(existingChild)) { delete(existingChild); } } } }
From source file:org.xwiki.contrib.confluence.filter.internal.ConfluenceXMLPackage.java
private void readObject(XMLStreamReader xmlReader) throws XMLStreamException, ConfigurationException, FilterException { String type = xmlReader.getAttributeValue(null, "class"); if (type != null) { if (type.equals("Page")) { readPageObject(xmlReader);/*from ww w . j a v a2 s . c o m*/ } else if (type.equals("Space")) { readSpaceObject(xmlReader); } else if (type.equals("InternalUser")) { readUserObject(xmlReader); } else if (type.equals("InternalGroup")) { readGroupObject(xmlReader); } else if (type.equals("HibernateMembership")) { readMembershipObject(xmlReader); } else if (type.equals("BodyContent")) { readBodyContentObject(xmlReader); } else if (type.equals("SpaceDescription")) { readSpaceDescriptionObject(xmlReader); } else if (type.equals("SpacePermission")) { readSpacePermissionObject(xmlReader); } else if (type.equals("Attachment")) { readAttachmentObject(xmlReader); } else { PropertiesConfiguration properties = newProperties(); long id = readObjectProperties(xmlReader, properties); // Save page saveObjectProperties(properties, id); } } }
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); }/* ww w . j a v a 2 s . com*/ } 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 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") || propertyClass.equals("ContentProperty")) { return readObjectReference(xmlReader); } else {//from w w w . ja va 2s.c o m StAXUtils.skipElement(xmlReader); } return null; }
From source file:org.xwiki.filter.confluence.xml.internal.ConfluenceXMLPackage.java
private void readObject(XMLStreamReader xmlReader) throws XMLStreamException, NumberFormatException, IOException, ConfigurationException, FilterException { String type = xmlReader.getAttributeValue(null, "class"); if (type != null) { if (type.equals("Page")) { readPageObject(xmlReader);/*w w w . ja v a 2s . c o m*/ } else if (type.equals("Space")) { readSpaceObject(xmlReader); } else if (type.equals("InternalUser")) { readUserObject(xmlReader); } else if (type.equals("InternalGroup")) { readGroupObject(xmlReader); } else if (type.equals("HibernateMembership")) { readMembershipObject(xmlReader); } else if (type.equals("BodyContent")) { readBodyContentObject(xmlReader); } else if (type.equals("SpaceDescription")) { readSpaceDescriptionObject(xmlReader); } else if (type.equals("SpacePermission")) { readSpacePermissionObject(xmlReader); } else if (type.equals("Attachment")) { readAttachmentObject(xmlReader); } else if (type.equals("ReferralLink")) { // TODO: any idea how to convert that ? StAXUtils.skipElement(xmlReader); } else if (type.equals("Label")) { // TODO: any idea how to convert that ? StAXUtils.skipElement(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 . jav a 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.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 {//from w ww . j av a 2 s. c o m StAXUtils.skipElement(xmlReader); } return null; }
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 ww . j ava 2 s. c om 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.wikistream.confluence.xml.internal.ConfluenceXMLPackage.java
private void readObject(XMLStreamReader xmlReader) throws XMLStreamException, NumberFormatException, IOException, ConfigurationException, WikiStreamException { String type = xmlReader.getAttributeValue(null, "class"); if (type != null) { if (type.equals("Page")) { readPageObject(xmlReader);/* w w w. j ava2s. c o m*/ } else if (type.equals("Space")) { readSpaceObject(xmlReader); } else if (type.equals("InternalUser")) { readUserObject(xmlReader); } else if (type.equals("InternalGroup")) { readGroupObject(xmlReader); } else if (type.equals("HibernateMembership")) { readMembershipObject(xmlReader); } else if (type.equals("BodyContent")) { readBodyContentObject(xmlReader); } else if (type.equals("SpaceDescription")) { readSpaceDescriptionObject(xmlReader); } else if (type.equals("SpacePermission")) { readSpacePermissionObject(xmlReader); } else if (type.equals("Attachment")) { readAttachmentObject(xmlReader); } else if (type.equals("ReferralLink")) { // TODO: any idea how to convert that ? StAXUtils.skipElement(xmlReader); } else if (type.equals("Label")) { // TODO: any idea how to convert that ? StAXUtils.skipElement(xmlReader); } else { StAXUtils.skipElement(xmlReader); } } }