Example usage for org.w3c.dom Element removeAttribute

List of usage examples for org.w3c.dom Element removeAttribute

Introduction

In this page you can find the example usage for org.w3c.dom Element removeAttribute.

Prototype

public void removeAttribute(String name) throws DOMException;

Source Link

Document

Removes an attribute by name.

Usage

From source file:org.apache.openaz.xacml.std.dom.DOMIdReference.java

public static boolean repair(Node nodeIdReference) throws DOMStructureException {
    Element elementIdReference = DOMUtil.getElement(nodeIdReference);
    boolean result = false;

    result = DOMUtil.repairIdentifierContent(elementIdReference, logger) || result;

    String versionString = DOMUtil.getStringAttribute(elementIdReference, XACML3.ATTRIBUTE_VERSION);
    if (versionString != null) {
        try {/*from w  w w  . j a v a2  s .c  o  m*/
            StdVersion.newInstance(versionString);
        } catch (ParseException ex) {
            logger.warn("Deleting invalid Version string " + versionString, ex);
            elementIdReference.removeAttribute(XACML3.ATTRIBUTE_VERSION);
            result = true;
        }
    }

    return result;
}

From source file:org.apache.openaz.xacml.std.dom.DOMUtil.java

public static boolean repairVersionMatchAttribute(Element element, String attributeName, Log logger) {
    String versionString = getStringAttribute(element, attributeName);
    if (versionString == null) {
        return false;
    }//from w  ww.java2s.co m

    try {
        StdVersionMatch.newInstance(versionString);
    } catch (ParseException ex) {
        logger.warn("Deleting invalid " + attributeName + " string " + versionString, ex);
        element.removeAttribute(attributeName);
        return true;
    }

    return false;
}

From source file:org.apache.sling.stanbol.ui.StanbolResourceViewer.java

private Element parseBody(String string) throws SAXException, IOException, ParserConfigurationException {
    InputSource inputSource = new InputSource(new StringReader(string));
    Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(inputSource);
    Element docElem = doc.getDocumentElement();
    docElem.removeAttribute("id");
    doc.renameNode(docElem, null, "body");
    return docElem;
}

From source file:org.apereo.portal.layout.dlm.EditManager.java

/**
   Evaluate whether attribute changes exist in the ilfChild and if so
   apply them. Returns true if some changes existed. If changes existed
   but matched those in the original node then they are not applicable,
   are removed from the editSet, and false is returned.
*///  w  ww .j  av  a2  s  .c o  m
public static boolean applyEditSet(Element plfChild, Element original) {
    // first get edit set if it exists
    Element editSet = null;
    try {
        editSet = getEditSet(plfChild, null, null, false);
    } catch (Exception e) {
        // should never occur unless problem during create in getEditSet
        // and we are telling it not to create.
        return false;
    }

    if (editSet == null || editSet.getChildNodes().getLength() == 0)
        return false;

    if (original.getAttribute(Constants.ATT_EDIT_ALLOWED).equals("false")) {
        // can't change anymore so discard changes
        plfChild.removeChild(editSet);
        return false;
    }

    Document ilf = original.getOwnerDocument();
    boolean attributeChanged = false;
    Element edit = (Element) editSet.getFirstChild();

    while (edit != null) {
        String attribName = edit.getAttribute(Constants.ATT_NAME);
        Attr attr = plfChild.getAttributeNode(attribName);

        // preferences are only updated at preference storage time so
        // if a preference change exists in the edit set assume it is
        // still valid so that the node being edited will persist in
        // the PLF.
        if (edit.getNodeName().equals(Constants.ELM_PREF))
            attributeChanged = true;
        else if (attr == null) {
            // attribute removed. See if needs removing in original.
            attr = original.getAttributeNode(attribName);
            if (attr == null) // edit irrelevant,
                editSet.removeChild(edit);
            else {
                // edit differs, apply to original
                original.removeAttribute(attribName);
                attributeChanged = true;
            }
        } else {
            // attribute there, see if original is also there
            Attr origAttr = original.getAttributeNode(attribName);
            if (origAttr == null) {
                // original attribute isn't defined so need to add
                origAttr = (Attr) ilf.importNode(attr, true);
                original.setAttributeNode(origAttr);
                attributeChanged = true;
            } else {
                // original attrib found, see if different
                if (attr.getValue().equals(origAttr.getValue())) {
                    // they are the same, edit irrelevant
                    editSet.removeChild(edit);
                } else {
                    // edit differs, apply to original
                    origAttr.setValue(attr.getValue());
                    attributeChanged = true;
                }
            }
        }
        edit = (Element) edit.getNextSibling();
    }
    return attributeChanged;
}

From source file:org.dhatim.delivery.dom.serialize.Serializer.java

/**
 * Serialise the document to the supplied output writer instance.
 * <p/>/*from w w w.ja v  a2s.  co  m*/
 * Adds the DOCTYPE decl if one defined in the Content Delivery Configuration.
 * <p/>
 * If the node is a Document (or DocumentFragment) node the whole node is serialised.  
 * Otherwise, only the node child elements are serialised i.e. the node itself is skipped.
 * @param writer Output writer.
 * @throws ResourceConfigurationNotFoundException DOM Serialiser exception.
 * @throws IOException Unable to write to output writer.
 */
public void serailize(Writer writer) throws ResourceConfigurationNotFoundException, IOException {
    List docTypeUDs;

    if (writer == null) {
        throw new IllegalArgumentException("null 'writer' arg passed in method call.");
    }

    // Register the DOM phase events...
    if (eventListener != null) {
        eventListener.onEvent(
                new DOMFilterLifecycleEvent(DOMFilterLifecycleEvent.DOMEventType.SERIALIZATION_STARTED));
    }

    if (node instanceof Document) {
        Document doc = (Document) node;
        Element rootElement = doc.getDocumentElement();

        DocType.DocumentTypeData docTypeData = DocType.getDocType(executionContext);
        if (docTypeData != null) {
            DocType.serializeDoctype(docTypeData, writer);
            if (docTypeData.getXmlns() != null) {
                rootElement.setAttribute("xmlns", docTypeData.getXmlns());
            } else {
                rootElement.removeAttribute("xmlns");
            }
        }

        recursiveDOMWrite(rootElement, writer, true);
    } else {
        // Write the DOM, the child elements of the node
        NodeList deliveryNodes = node.getChildNodes();
        int nodeCount = deliveryNodes.getLength();
        boolean isRoot = (node == node.getOwnerDocument().getDocumentElement());
        for (int i = 0; i < nodeCount; i++) {
            Node childNode = deliveryNodes.item(i);
            if (childNode.getNodeType() == Node.ELEMENT_NODE) {
                recursiveDOMWrite((Element) childNode, writer, isRoot);
            }
        }
    }
}

From source file:org.dita.dost.AbstractIntegrationTest.java

private Document parseXml(final File f) throws SAXException, IOException {
    final Document d = db.parse(f);
    final NodeList elems = d.getElementsByTagName("*");
    for (int i = 0; i < elems.getLength(); i++) {
        final Element e = (Element) elems.item(i);
        // remove debug attributes
        e.removeAttribute(ATTRIBUTE_NAME_XTRF);
        e.removeAttribute(ATTRIBUTE_NAME_XTRC);
        // remove DITA version and domains attributes
        e.removeAttributeNS(DITA_NAMESPACE, ATTRIBUTE_NAME_DITAARCHVERSION);
        e.removeAttribute(ATTRIBUTE_NAME_DOMAINS);
        // remove workdir processing instructions
        removeWorkdirProcessingInstruction(e);
    }//from w ww .  j  ava 2 s .com
    // rewrite IDs
    return rewriteIds(d, ditaIdPattern);
}

From source file:org.dita.dost.AbstractIntegrationTest.java

private Document removeCopyright(final Document doc) {
    final NodeList ns = doc.getElementsByTagName("meta");
    for (int i = 0; i < ns.getLength(); i++) {
        final Element e = (Element) ns.item(i);
        final String name = e.getAttribute("name");
        if (name.equals("copyright") || name.equals("DC.rights.owner")) {
            e.removeAttribute("content");
        }// ww  w.  j  av  a 2  s. c o m
    }
    return doc;
}

From source file:org.dita.dost.module.BranchFilterModule.java

/** Copy and filter topics for branches. These topics have a new name and will be added to job configuration. */
private void generateCopies(final Element topicref, final List<FilterUtils> filters) {
    final List<FilterUtils> fs = combineFilterUtils(topicref, filters);

    final String copyTo = topicref.getAttribute(BRANCH_COPY_TO);
    if (!copyTo.isEmpty()) {
        final URI dstUri = map.resolve(copyTo);
        final URI dstAbsUri = job.tempDirURI.resolve(dstUri);
        final String href = topicref.getAttribute(ATTRIBUTE_NAME_HREF);
        final URI srcUri = map.resolve(href);
        final URI srcAbsUri = job.tempDirURI.resolve(srcUri);
        final FileInfo srcFileInfo = job.getFileInfo(srcUri);
        if (srcFileInfo != null) {
            //                final FileInfo fi = new FileInfo.Builder(srcFileInfo).uri(dstUri).build();
            //                 TODO: Maybe Job should be updated earlier?
            //                job.add(fi);
            logger.info("Filtering " + srcAbsUri + " to " + dstAbsUri);
            final ProfilingFilter writer = new ProfilingFilter();
            writer.setLogger(logger);// w  w w .ja  va2 s  .  com
            writer.setJob(job);
            writer.setFilterUtils(fs);
            writer.setCurrentFile(dstAbsUri);
            final List<XMLFilter> pipe = singletonList(writer);

            final File dstDirUri = new File(dstAbsUri.resolve("."));
            if (!dstDirUri.exists() && !dstDirUri.mkdirs()) {
                logger.error("Failed to create directory " + dstDirUri);
            }
            try {
                xmlUtils.transform(srcAbsUri, dstAbsUri, pipe);
            } catch (final DITAOTException e) {
                logger.error("Failed to filter " + srcAbsUri + " to " + dstAbsUri + ": " + e.getMessage(), e);
            }
            topicref.setAttribute(ATTRIBUTE_NAME_HREF, copyTo);
            topicref.removeAttribute(BRANCH_COPY_TO);
            // disable filtering again
            topicref.setAttribute(SKIP_FILTER, Boolean.TRUE.toString());
        }
    }
    for (final Element child : getChildElements(topicref, MAP_TOPICREF)) {
        if (DITAVAREF_D_DITAVALREF.matches(child)) {
            continue;
        }
        generateCopies(child, fs);
    }
}

From source file:org.dita.dost.module.BranchFilterModule.java

private void processAttributes(final Element elem, final Branch filter) {
    if (filter.resourcePrefix.isPresent() || filter.resourceSuffix.isPresent()) {
        final String href = elem.getAttribute(ATTRIBUTE_NAME_HREF);
        final String copyTo = elem.getAttribute(ATTRIBUTE_NAME_COPY_TO);
        final String scope = elem.getAttribute(ATTRIBUTE_NAME_SCOPE);
        if ((!href.isEmpty() || !copyTo.isEmpty()) && !scope.equals(ATTR_SCOPE_VALUE_EXTERNAL)) {
            final FileInfo hrefFileInfo = job.getFileInfo(currentFile.resolve(href));
            final FileInfo copyToFileInfo = !copyTo.isEmpty() ? job.getFileInfo(currentFile.resolve(copyTo))
                    : null;//from   w  w w.ja v  a  2s .  c  o m
            final URI dstSource;
            dstSource = generateCopyTo((copyToFileInfo != null ? copyToFileInfo : hrefFileInfo).result, filter);
            final URI dstTemp = tempFileNameScheme.generateTempFileName(dstSource);
            final String dstPathFromMap = !copyTo.isEmpty() ? FilenameUtils.getPath(copyTo)
                    : FilenameUtils.getPath(href);
            final FileInfo.Builder dstBuilder = new FileInfo.Builder(hrefFileInfo).result(dstSource)
                    .uri(dstTemp);
            if (dstBuilder.build().format == null) {
                dstBuilder.format(ATTR_FORMAT_VALUE_DITA);
            }
            if (hrefFileInfo.src == null && href != null) {
                if (copyToFileInfo != null) {
                    dstBuilder.src(copyToFileInfo.src);
                }
            }
            final FileInfo dstFileInfo = dstBuilder.build();

            elem.setAttribute(BRANCH_COPY_TO, dstPathFromMap + FilenameUtils.getName(dstTemp.toString()));
            if (!copyTo.isEmpty()) {
                elem.removeAttribute(ATTRIBUTE_NAME_COPY_TO);
            }

            job.add(dstFileInfo);
        }
    }

    if (filter.keyscopePrefix.isPresent() || filter.keyscopeSuffix.isPresent()) {
        final StringBuilder buf = new StringBuilder();
        final String keyscope = elem.getAttribute(ATTRIBUTE_NAME_KEYSCOPE);
        if (!keyscope.isEmpty()) {
            for (final String key : keyscope.trim().split("\\s+")) {
                filter.keyscopePrefix.ifPresent(buf::append);
                buf.append(key);
                filter.keyscopeSuffix.ifPresent(buf::append);
                buf.append(' ');
            }
        } else {
            filter.keyscopePrefix.ifPresent(buf::append);
            filter.keyscopeSuffix.ifPresent(buf::append);
        }
        elem.setAttribute(ATTRIBUTE_NAME_KEYSCOPE, buf.toString().trim());
    }
}

From source file:org.dita.dost.reader.ChunkMapReader.java

/**
 * Process map when "to-content" is specified on map element.
 *
 * TODO: Instead of reclassing map element to be a topicref, add a topicref
 * into the map root and move all map content into that topicref.
 *//*from  ww w  .  j a v a2 s  .co m*/
private void chunkMap(final Element root) {
    // create the reference to the new file on root element.
    String newFilename = replaceExtension(inputFile.getName(), FILE_EXTENSION_DITA);
    File newFile = new File(inputFile.getParentFile().getAbsolutePath(), newFilename);
    if (newFile.exists()) {
        newFilename = chunkFilenameGenerator.generateFilename("Chunk", FILE_EXTENSION_DITA);
        final String oldpath = newFile.getAbsolutePath();
        newFile = resolve(inputFile.getParentFile().getAbsolutePath(), newFilename);
        // Mark up the possible name changing, in case that references might be updated.
        conflictTable.put(newFile.getAbsolutePath(), normalize(oldpath));
    }
    changeTable.put(newFile.getAbsolutePath(), newFile.getAbsolutePath());

    // change the class attribute to "topicref"
    final String originClassValue = root.getAttribute(ATTRIBUTE_NAME_CLASS);
    root.setAttribute(ATTRIBUTE_NAME_CLASS, originClassValue + MAP_TOPICREF.matcher);
    root.setAttribute(ATTRIBUTE_NAME_HREF, toURI(newFilename).toString());

    createTopicStump(newFile);

    // process chunk
    processTopicref(root);

    // restore original root element
    if (originClassValue != null) {
        root.setAttribute(ATTRIBUTE_NAME_CLASS, originClassValue);
    }
    root.removeAttribute(ATTRIBUTE_NAME_HREF);
}