Java Code Examples for org.dom4j.Element#detach()

The following examples show how to use org.dom4j.Element#detach() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: XmlText.java    From ats-framework with Apache License 2.0 6 votes vote down vote up
/**
 * Remove a XML element
 *
 * @param xpath XPath , pointing to a XML element
 * @return this instance
 * @throws XMLException
 */
@PublicAtsApi
public XmlText remove(
                       String xpath ) throws XMLException {

    if (StringUtils.isNullOrEmpty(xpath)) {
        throw new XMLException("Null/empty xpath is not allowed.");
    }

    Element element = findElement(xpath);

    if (element != null) {

        if (element.isRootElement()) {
            throw new XMLException("You cannot remove the root element of the XML document.");
        }

        element.detach();
    } else {
        throw new XMLException("'" + xpath + "' is not a valid path");
    }

    return this;
}
 
Example 2
Source File: XmlNode.java    From ats-framework with Apache License 2.0 6 votes vote down vote up
/**
 * If some node is to be skipped, we do it in this method
 * 
 * @param matchedNode
 */
public void removeChild( Element matchedNode ) {

    // remove the matched node
    matchedNode.detach();

    // now we have to regenerate info that might have been affected
    this.value = this.node.getTextTrim();

    this.children.clear();
    List<Element> childrenElements = this.node.elements();
    for (Element child : childrenElements) {
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            this.children.add(new XmlNode(this, child));
        }
    }
}
 
Example 3
Source File: DataMigrator.java    From onedev with MIT License 6 votes vote down vote up
private void migrate14(File dataDir, Stack<Integer> versions) {
	for (File file: dataDir.listFiles()) {
		if (file.getName().startsWith("Projects.xml")) {
			VersionedXmlDoc dom = VersionedXmlDoc.fromFile(file);
			for (Element projectElement: dom.getRootElement().elements()) {
				for (Element branchProtectionElement: projectElement.element("branchProtections").elements()) {
					Element submitterElement = branchProtectionElement.addElement("submitter");
					submitterElement.addAttribute("class", "com.turbodev.server.model.support.submitter.Anyone");
					branchProtectionElement.addElement("noCreation").setText("true");
				}
				for (Element tagProtectionElement: projectElement.element("tagProtections").elements()) {
					tagProtectionElement.detach();
				}
			}
			dom.writeToFile(file, false);
		} 
	}
}
 
Example 4
Source File: DataMigrator.java    From onedev with MIT License 6 votes vote down vote up
private void migrate17(File dataDir, Stack<Integer> versions) {
	for (File file: dataDir.listFiles()) {
		if (file.getName().startsWith("Issue")) {
			FileUtils.deleteFile(file);
		} else if (file.getName().startsWith("Projects.xml")) {
			VersionedXmlDoc dom = VersionedXmlDoc.fromFile(file);
			for (Element element: dom.getRootElement().elements()) {
				Element issueWorkflowElement = element.element("issueWorkflow");
				if (issueWorkflowElement != null)
					issueWorkflowElement.detach();
				Element savedIssueQueriesElement = element.element("savedIssueQueries");
				if (savedIssueQueriesElement != null)
					savedIssueQueriesElement.detach();
				Element issueListFieldsElement = element.element("issueListFields");
				if (issueListFieldsElement != null)
					issueListFieldsElement.detach();
				Element issueBoardsElement = element.element("issueBoards");
				if (issueBoardsElement != null)
					issueBoardsElement.detach();
			}
			dom.writeToFile(file, false);
		}
	}
}
 
Example 5
Source File: DataMigrator.java    From onedev with MIT License 6 votes vote down vote up
private void migrate19(File dataDir, Stack<Integer> versions) {
	for (File file: dataDir.listFiles()) {
		if (file.getName().startsWith("Projects.xml")) {
			VersionedXmlDoc dom = VersionedXmlDoc.fromFile(file);
			for (Element element: dom.getRootElement().elements()) {
				Element commitMessageTransformsElement = element.addElement("commitMessageTransforms");
				Element commitMessageTransformSettingElement = element.element("commitMessageTransformSetting");
				if (commitMessageTransformSettingElement != null) {
					commitMessageTransformSettingElement.detach();
					commitMessageTransformSettingElement.setName("io.onedev.server.model.support.CommitMessageTransform");
					commitMessageTransformsElement.add(commitMessageTransformSettingElement);
				}
			}
			dom.writeToFile(file, false);
		}
	}
}
 
Example 6
Source File: DOM4JSerializer.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static void exportToFile(ExportInteraction exportInteraction, ExtensionFileFilter fileFilter, DOM4JSettingsNode settingsNode) {
    File file = promptForFile(exportInteraction, fileFilter);
    if (file == null) {
        //the user canceled.
        return;
    }

    FileOutputStream fileOutputStream = null;
    try {
        fileOutputStream = new FileOutputStream(file);
    } catch (FileNotFoundException e) {
        LOGGER.error("Could not write to file: " + file.getAbsolutePath(), e);
        exportInteraction.reportError("Could not write to file: " + file.getAbsolutePath());
        return;
    }

    try {
        XMLWriter xmlWriter = new XMLWriter(fileOutputStream, OutputFormat.createPrettyPrint());
        Element rootElement = settingsNode.getElement();
        rootElement.detach();

        Document document = DocumentHelper.createDocument(rootElement);

        xmlWriter.write(document);
    } catch (Throwable t) {
        LOGGER.error("Internal error. Failed to save.", t);
        exportInteraction.reportError("Internal error. Failed to save.");
    } finally {
        closeQuietly(fileOutputStream);
    }
}
 
Example 7
Source File: DOM4JSettingsNode.java    From pushfish-android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void removeAllChildren() {
    List list = element.elements();
    Iterator iterator = list.iterator();

    while (iterator.hasNext()) {
        Element child = (Element) iterator.next();
        child.detach();
    }
}
 
Example 8
Source File: DefaultPersistManager.java    From onedev with MIT License 5 votes vote down vote up
protected void validateData(Metadata metadata, File dataDir) {
	List<Class<?>> entityTypes = getEntityTypes(sessionFactory);
	Collections.reverse(entityTypes);
	for (Class<?> entityType: entityTypes) {
		File[] dataFiles = dataDir.listFiles(new FilenameFilter() {

			@Override
			public boolean accept(File dir, String name) {
				return name.startsWith(entityType.getSimpleName() + "s.xml");
			}
			
		});
		for (File file: dataFiles) {
			try {
				logger.info("Validating data file '" + file.getName() + "'...");
				VersionedXmlDoc dom = VersionedXmlDoc.fromFile(file);
				
				for (Element element: dom.getRootElement().elements()) {
					element.detach();
					AbstractEntity entity = (AbstractEntity) new VersionedXmlDoc(DocumentHelper.createDocument(element)).toBean();
					validator.validate(entity);
				}
			} catch (Exception e) {
				throw ExceptionUtils.unchecked(e);
			}
		}
	}	
}
 
Example 9
Source File: AbstractType.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
protected static void replaceNode(Node container, Element value) {
	if ( container!=value ) { //not really necessary, I guess...
		Element parent = container.getParent();
		container.detach();
		value.setName( container.getName() );
		value.detach();
		parent.add(value);
	}
}
 
Example 10
Source File: DataMigrator.java    From onedev with MIT License 5 votes vote down vote up
private void migrate37(File dataDir, Stack<Integer> versions) {
	for (File file: dataDir.listFiles()) {
		if (file.getName().startsWith("Settings.xml")) {
			VersionedXmlDoc dom = VersionedXmlDoc.fromFile(file);
			for (Element element: dom.getRootElement().elements()) {
				if (element.elementTextTrim("key").equals("LICENSE"))
					element.detach();
			}
			dom.writeToFile(file, false);
		}
	}
}
 
Example 11
Source File: DataMigrator.java    From onedev with MIT License 5 votes vote down vote up
private void migrate32(File dataDir, Stack<Integer> versions) {
	for (File file: dataDir.listFiles()) {
		if (file.getName().startsWith("Settings.xml")) {
			VersionedXmlDoc dom = VersionedXmlDoc.fromFile(file);
			for (Element element: dom.getRootElement().elements()) {
				String key = element.elementTextTrim("key"); 
				if (key.equals("ISSUE"))
					element.detach();
			}
			dom.writeToFile(file, false);
		} else if (file.getName().startsWith("IssueChanges.xml")) { 
			FileUtils.deleteFile(file);
		}
	}
}
 
Example 12
Source File: DataMigrator.java    From onedev with MIT License 5 votes vote down vote up
private void migrate31(File dataDir, Stack<Integer> versions) {
	for (File file: dataDir.listFiles()) {
		if (file.getName().startsWith("Roles.xml")) {
			VersionedXmlDoc dom = VersionedXmlDoc.fromFile(file);
			for (Element element: dom.getRootElement().elements()) {
				Element editableIssueFieldsElement = element.element("editableIssueFields");
				editableIssueFieldsElement.detach();
				element.addElement("editableIssueFields").addAttribute(
						"class", "io.onedev.server.model.support.role.AllIssueFields");
			}
			dom.writeToFile(file, false);
		}
	}
}
 
Example 13
Source File: DataMigrator.java    From onedev with MIT License 5 votes vote down vote up
private void migrate30(File dataDir, Stack<Integer> versions) {
	for (File file: dataDir.listFiles()) {
		if (file.getName().startsWith("Settings.xml")) {
			VersionedXmlDoc dom = VersionedXmlDoc.fromFile(file);
			for (Element element: dom.getRootElement().elements()) {
				String key = element.elementTextTrim("key"); 
				if (key.equals("JOB_EXECUTORS")) 
					element.detach();
			}
			dom.writeToFile(file, false);
		}
	}
}
 
Example 14
Source File: DataMigrator.java    From onedev with MIT License 5 votes vote down vote up
private void migrate4(File dataDir, Stack<Integer> versions) {
	for (File file: dataDir.listFiles()) {
		if (file.getName().startsWith("Accounts.xml")) {
			VersionedXmlDoc dom = VersionedXmlDoc.fromFile(file);
			for (Element element: dom.getRootElement().elements()) {
				Element avatarUploadDateElement = element.element("avatarUploadDate");
				if (avatarUploadDateElement != null)
					avatarUploadDateElement.detach();
			}
			dom.writeToFile(file, false);
		}
	}	
}
 
Example 15
Source File: DataMigrator.java    From onedev with MIT License 5 votes vote down vote up
private void migrate2(File dataDir, Stack<Integer> versions) {
	for (File file: dataDir.listFiles()) {
		if (file.getName().startsWith("Depots.xml")) {
			VersionedXmlDoc dom = VersionedXmlDoc.fromFile(file);
			for (Element element: dom.getRootElement().elements()) {
				Element gateKeeperElement = element.element("gateKeeper");
				gateKeeperElement.detach();
				element.addElement("gateKeepers");
			}
			dom.writeToFile(file, false);
		}
	}	
}
 
Example 16
Source File: DataMigrator.java    From onedev with MIT License 5 votes vote down vote up
private void migrate1(File dataDir, Stack<Integer> versions) {
	for (File file: dataDir.listFiles()) {
		if (file.getName().startsWith("CodeComments.xml")) {
			VersionedXmlDoc dom = VersionedXmlDoc.fromFile(file);
			for (Element element: dom.getRootElement().elements()) {
				Element branchRefElement = element.element("branchRef");
				if (branchRefElement != null)
					branchRefElement.detach();
			}
			dom.writeToFile(file, false);
		}
	}	
}
 
Example 17
Source File: DOM4JSerializer.java    From pushfish-android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static void exportToFile(ExportInteraction exportInteraction, ExtensionFileFilter fileFilter, DOM4JSettingsNode settingsNode) {
    File file = promptForFile(exportInteraction, fileFilter);
    if (file == null) {
        //the user canceled.
        return;
    }

    FileOutputStream fileOutputStream = null;
    try {
        fileOutputStream = new FileOutputStream(file);
    } catch (FileNotFoundException e) {
        LOGGER.error("Could not write to file: " + file.getAbsolutePath(), e);
        exportInteraction.reportError("Could not write to file: " + file.getAbsolutePath());
        return;
    }

    try {
        XMLWriter xmlWriter = new XMLWriter(fileOutputStream, OutputFormat.createPrettyPrint());
        Element rootElement = settingsNode.getElement();
        rootElement.detach();

        Document document = DocumentHelper.createDocument(rootElement);

        xmlWriter.write(document);
    } catch (Throwable t) {
        LOGGER.error("Internal error. Failed to save.", t);
        exportInteraction.reportError("Internal error. Failed to save.");
    } finally {
        closeQuietly(fileOutputStream);
    }
}
 
Example 18
Source File: DOM4JSettingsNode.java    From pushfish-android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void removeAllChildren() {
    List list = element.elements();
    Iterator iterator = list.iterator();

    while (iterator.hasNext()) {
        Element child = (Element) iterator.next();
        child.detach();
    }
}
 
Example 19
Source File: DOM4JSettingsNode.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void removeAllChildren() {
    List list = element.elements();
    Iterator iterator = list.iterator();

    while (iterator.hasNext()) {
        Element child = (Element) iterator.next();
        child.detach();
    }
}
 
Example 20
Source File: PrivateStorage.java    From Openfire with Apache License 2.0 4 votes vote down vote up
/**
 * Stores private data. If the name and namespace of the element matches another
 * stored private data XML document, then replace it with the new one.
 *
 * @param data the data to store (XML element)
 * @param username the username of the account where private data is being stored
 */
public void add(String username, Element data) {
    if (!enabled)
    {
        return;
    }

    final JID owner = XMPPServer.getInstance().createJID( username, null );
    final PEPServiceManager serviceMgr = XMPPServer.getInstance().getIQPEPHandler().getServiceManager();
    PEPService pepService = serviceMgr.getPEPService( owner );
    if ( pepService == null )
    {
        pepService = serviceMgr.create( owner );
    }

    Node node = pepService.getNode( data.getNamespaceURI() );
    if ( node == null )
    {
        PubSubEngine.CreateNodeResponse response = PubSubEngine.createNodeHelper( pepService, owner, pepService.getDefaultNodeConfiguration( true ).getConfigurationForm().getElement(), data.getNamespaceURI(), PRIVATE_DATA_PUBLISHING_OPTIONS );
        node = response.newNode;

        if ( node == null )
        {
            Log.error( "Unable to create new PEP node, to be used to store private data. Error condition: {}", response.creationStatus.toXMPP() );
            return;
        }
    }

    if (!(node instanceof LeafNode))
    {
        Log.error( "Unable to store private data into a PEP node. The node that is available is not a leaf node." );
        return;
    }

    data.detach();
    final Element item = DocumentHelper.createElement( "item" );
    item.addAttribute( "id", "current" );
    item.add( data );

    ((LeafNode) node).publishItems( owner, Collections.singletonList( item ) );
}