List of usage examples for org.jdom2 Attribute clone
@Override
public Attribute clone()
From source file:com.c4om.autoconf.ulysses.extra.svinchangesetgenerator.SVINChangesetGenerator.java
License:Apache License
/** * Recursive method that mixes two XML trees in the following way: * <ul>//from w ww .j ava 2s . c o m * <li>If a child exists at the source leaf but not at destination, the * element is copied as a child to the destination leaf</li> * <li>If a child exists at both the source and the destination leafs and * the source child has children, this method is recursively called to mix * both children</li> * </ul> * Some important remarks: * <ul> * <li>Equality comparison is not made via common methods but via * {@link JDOMUtils#elementsEqualAtCNameAndAttributes(Element, Element)} * .</li> * <li>Results of this method are returned as changes to the * destinationLeaf.</li> * <li>An attribute may be appended to all the elements added to the * destination leaf by this method.</li> * <li>Elements of a concrete namespace can be ignored by this method, if desired.</li> * </ul> * * @param sourceLeaf * The source leaf to mix into the destination leaf. It remains * unchanged. * @param destinationLeaf * The destination leaf, where the source leaf will be mixed * into. Results will be returned as changes at this element, so * IT WILL NOT REMAIN UNCHANGED AFTER THIS METHOD (normally). You * should consider using {@link Element#clone()} if necessary. * @param metadataAttributeToAppend * an attribute to be appended to each element added by this * method to the destinationLeaf. If a node with descendants is * added, the attribute will be added only to the top-level * element (not to all the descendants). If null, no attribute * will be added. * @param ignoreNamespaceURIIfTextPresent any element whose namespace URI matches this one * will be ignored if the source element has text content. * If null, no element is ignored. */ private void mixTreesRecursive(Element sourceLeaf, Element destinationLeaf, Attribute metadataAttributeToAppend, String ignoreNamespaceURIIfTextPresent) { List<Content> sourceLeafContent = sourceLeaf.getContent(); //j is the index for "only-element" content for (int i = 0, j = 0; i < sourceLeafContent.size(); i++) { Content currentContent = sourceLeafContent.get(i); if (!(currentContent instanceof Element)) { continue; } Element currentSourceChild = (Element) currentContent; Element currentDestinationChild = searchElementEqualAtCNameAndAttributes(destinationLeaf.getChildren(), currentSourceChild); if (currentDestinationChild == null) { if (ignoreNamespaceURIIfTextPresent != null && !destinationLeaf.getTextNormalize().equals("") && ignoreNamespaceURIIfTextPresent.equals(currentSourceChild.getNamespaceURI())) { continue; } // There is not equivalent node at destination, so we copy the // whole currentSourceChild. Element elementToAdd = currentSourceChild.clone(); if (metadataAttributeToAppend != null) { elementToAdd.setAttribute(metadataAttributeToAppend.clone()); } destinationLeaf.addContent(j, elementToAdd); } else { // Element exists at destination. If it has children, we recurse // to fill them (they might be not completely filled). if (currentSourceChild.getChildren().size() > 0) { mixTreesRecursive(currentSourceChild, currentDestinationChild, metadataAttributeToAppend, ignoreNamespaceURIIfTextPresent); } } j++; } }
From source file:com.facebook.buck.ide.intellij.projectview.ProjectView.java
License:Apache License
private void writeRootDotIml(List<String> sourceFiles, Set<String> roots, List<String> libraries) { String buckOut = fileJoin(viewPath, BUCK_OUT); symlink(fileJoin(repository, BUCK_OUT), buckOut); String apkPath = null;/*from ww w . ja v a 2 s.co m*/ Map<BuildTarget, String> outputs = getOutputs(); // Find the 1st target that has output for (BuildTarget target : buildTargets) { String output = outputs.get(target); if (output != null && output.endsWith(".apk")) { apkPath = File.separator + output; break; } } String manifestPath = fileJoin(File.separator, RES, ANDROID_MANIFEST); symlink(fileJoin(repository, ANDROID_RES, ANDROID_MANIFEST), fileJoin(viewPath, manifestPath)); Element module = newElement("module", attribute(TYPE, "JAVA_MODULE"), attribute(VERSION, 4)); Element facetManager = addElement(module, COMPONENT, attribute(NAME, "FacetManager")); Element facet = addElement(facetManager, "facet", attribute(TYPE, "android"), attribute(NAME, "Android")); Element configuration = addElement(facet, "configuration"); String genFolder = fileJoin(File.separator, BUCK_OUT, "gen"); addElement(configuration, OPTION, attribute(NAME, "GEN_FOLDER_RELATIVE_PATH_APT"), attribute(VALUE, genFolder)); addElement(configuration, OPTION, attribute(NAME, "GEN_FOLDER_RELATIVE_PATH_AIDL"), attribute(VALUE, fileJoin(genFolder, "aidl"))); addElement(configuration, OPTION, attribute(NAME, "MANIFEST_FILE_RELATIVE_PATH"), attribute(VALUE, manifestPath)); addElement(configuration, OPTION, attribute(NAME, "RES_FOLDERS_RELATIVE_PATH"), attribute(VALUE, "/res")); if (apkPath != null) { addElement(configuration, OPTION, attribute(NAME, "APK_PATH"), attribute(VALUE, apkPath)); } addElement(configuration, OPTION, attribute(NAME, "ENABLE_SOURCES_AUTOGENERATION"), attribute(VALUE, true)); addElement(configuration, "includeAssetsFromLibraries").addContent("true"); Element rootManager = addElement(module, COMPONENT, attribute(NAME, "NewModuleRootManager"), attribute("inherit-compiler-output", true)); addElement(rootManager, "exclude-output"); Element folders = addElement(rootManager, CONTENT, attribute(URL, FILE_MODULE_DIR)); Set<String> sourceFolders = sourceFiles.stream().map((folder) -> dirname(folder)) .collect(Collectors.toSet()); sourceFolders.remove(null); for (String source : sortSourceFolders(sourceFolders)) { List<Attribute> attributes = new ArrayList<>(3); attributes.add(attribute(URL, fileJoin(FILE_MODULE_DIR, source))); attributes.add(attribute(IS_TEST_SOURCE, false)); String packagePrefix = getPackage(fileJoin(repository, source)); if (packagePrefix != null) { attributes.add(attribute("packagePrefix", packagePrefix)); } addElement(folders, SOURCE_FOLDER, attributes); } for (String excluded : getExcludedFolders(sourceFolders, roots)) { addElement(folders, EXCLUDE_FOLDER, attribute(URL, fileJoin(FILE_MODULE_DIR, excluded))); } addElement(rootManager, ORDER_ENTRY, attribute(TYPE, "inheritedJdk")); addElement(rootManager, ORDER_ENTRY, attribute(TYPE, SOURCE_FOLDER), attribute("forTests", false)); for (String library : libraries) { addElement(rootManager, ORDER_ENTRY, attribute(TYPE, LIBRARY), attribute(NAME, library), attribute("level", "project")); } for (String relativeFolder : getAnnotationAndGeneratedFolders()) { String folder = fileJoin(FILE_MODULE_DIR, relativeFolder); Attribute url = attribute(URL, folder); Element content = addElement(rootManager, CONTENT, url); addElement(content, SOURCE_FOLDER, url.clone(), attribute(IS_TEST_SOURCE, false), attribute("generated", true)); } saveDocument(viewPath, ROOT_IML, XML.DECLARATION, module); }