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:de.betterform.xml.xforms.ui.state.UIElementStateUtil.java

/**
 * Creates or updates the specified state attribute. If the value is
 * <code>null</code>, the attribute will be removed.
 *
 * @param element the element./*  ww w.j av a2s  .  com*/
 * @param name    the attribute name.
 * @param value   the attribute value.
 */
public static void setStateAttribute(Element element, String name, String value) {
    if (value != null) {
        element.setAttribute(name, value);
    } else {
        element.removeAttribute(name);
    }
}

From source file:Main.java

/**
 * Renames the attribute with the new name
 * //from  ww  w  .  j a v  a 2  s .  co  m
 * @param elementObject
 *            the element object
 * @param oldName
 *            Old name of the attribute
 * @param newName
 *            New name of the attribute
 */
public static void renameAttribute(final Element elementObject, final String oldName, final String newName)
        throws IllegalArgumentException {
    // Validate element Object
    if (elementObject == null) {
        throw new IllegalArgumentException(
                "Element object cannot be null" + " in XmlUtils.renameAttribute method");
    }

    // Validate old name
    if (oldName == null) {
        throw new IllegalArgumentException("Old name cannot be null" + " in XmlUtils.renameAttribute method");
    }

    // Validate new name
    if (newName == null) {
        throw new IllegalArgumentException("New name cannot be null" + " in XmlUtils.renameAttribute method");
    }

    // the attribute value
    String attributeValue = null;

    // get the value of the attribute
    attributeValue = getAttribute(elementObject, oldName, null);

    // remove the old attribute
    elementObject.removeAttribute(oldName);

    // if the attribute value is not null then set the attribute
    if (attributeValue != null) {
        elementObject.setAttribute(newName, attributeValue);
    }
}

From source file:cz.cas.lib.proarc.common.export.mets.MetsUtils.java

/**
 * Removes the schemaLocation attribute//from  w w w  . j a  va 2  s. c o m
 *
 * @param elements
 * @return
 */
public static List<Element> removeSchemaLocation(List<Element> elements) {
    if (elements.size() > 0) {
        Element element = elements.get(0);
        element.removeAttribute("xsi:schemaLocation");
        element.removeAttribute("schemaLocation");
    }
    return elements;
}

From source file:com.icesoft.faces.renderkit.dom_html_basic.DomBasicRenderer.java

/**
 * <p/>//w ww  .j  a  va 2  s . c om
 * Sets a non-null, non-empty-string, UIComponent property to the
 * corresponding DOM Element
 * <p/>
 *
 * @param uiComponent         the source of the attribute value
 * @param targetElement       the DOM Element that will receive the
 *                            attribute
 * @param attrNameInComponent the property name in the UIComponent object
 * @param attrNameInDom       the attribute name in the DOM Element
 */
public static void renderAttribute(UIComponent uiComponent, Element targetElement, String attrNameInComponent,
        String attrNameInDom) {
    Object attrValue = uiComponent.getAttributes().get(attrNameInComponent);
    if (attrValue != null && !attrValue.equals("")) {
        if (attrValue.toString().equalsIgnoreCase("true") || attrValue.toString().equalsIgnoreCase("false")) {
            boolean trueValue = new Boolean(attrValue.toString()).booleanValue();
            if (!trueValue) {
                targetElement.removeAttribute(attrNameInDom.toString());
                return;
            }
        }
        targetElement.setAttribute(attrNameInDom.toString(), attrValue.toString());
    }
}

From source file:fi.csc.kapaVirtaAS.WSDLManipulator.java

public Element replaceAttribute(Element el, String attributeName, String newValue) {
    el.removeAttribute(attributeName);
    el.setAttribute(attributeName, newValue);
    return el;/*from   w w w .  j  av  a  2s .  c  o m*/
}

From source file:fi.csc.kapaVirtaAS.MessageTransformer.java

private Element removeAttribureFromElement(Element e, String attribute) {
    e.removeAttribute(attribute);
    return e;
}

From source file:com.idega.chiba.web.xml.xforms.ui.IdegaText.java

public IdegaText(Element element, Model model) {
    super(element, model);

    if (element == null) {
        return;/*from   www. java  2s  .c  om*/
    }

    if (execute(element)) {
        element.removeAttribute("style");
        return;
    }

    element.setAttribute("style", "display: none;");
}

From source file:name.vysoky.epub.Part.java

private void removeUnsupportedAttributes(Element element) {
    for (String attr : UNSUPPORTED_ATTRIBUTES)
        element.removeAttribute(attr);
    // recursion//  ww  w.j  a  v  a  2 s.c o  m
    for (int i = 0; i < element.getChildNodes().getLength(); i++) {
        Node childNode = element.getChildNodes().item(i);
        if (childNode.getNodeType() == Node.ELEMENT_NODE)
            removeUnsupportedAttributes((Element) childNode);
    }
}

From source file:org.mitre.stix.STIXSchema.java

/**
 * Validate an XML text String against the STIX schema
 * //  w w w .ja va 2 s . c  om
 * @param xmlText
 *            A string of XML text to be validated
 * @return boolean True If the xmlText validates against the schema
 * @throws SAXException
 *             If the a validation ErrorHandler has not been set, and
 *             validation throws a SAXException
 */
public boolean validate(String xmlText) throws SAXException {

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);

    // This section removes the schema hint as we have the schema docs
    // otherwise exceptions may be thrown

    try {
        DocumentBuilder b = factory.newDocumentBuilder();
        Document document = b.parse(new ByteArrayInputStream(xmlText.getBytes()));

        Element root = document.getDocumentElement();

        root.removeAttribute("xsi:schemaLocation");

        TransformerFactory transFactory = TransformerFactory.newInstance();
        Transformer transformer = transFactory.newTransformer();
        StringWriter buffer = new StringWriter();
        transformer.transform(new DOMSource(root), new StreamResult(buffer));
        xmlText = buffer.toString();

    } catch (ParserConfigurationException e) {
        throw new RuntimeException(e);
    } catch (TransformerConfigurationException e) {
        throw new RuntimeException(e);
    } catch (TransformerException e) {
        throw new RuntimeException(e);
    } catch (SAXException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

    try {
        // synchronized to avoid org.xml.sax.SAXException: FWK005 parse may
        // not be called while parsing.
        synchronized (this) {
            validator.validate(
                    new StreamSource(new ByteArrayInputStream(xmlText.getBytes(StandardCharsets.UTF_8))));
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    } catch (SAXException e) {
        if (this.validator.getErrorHandler() != null) {
            return false;
        } else {
            // re-throw the SAXException
            throw e;
        }
    }

    return true;
}

From source file:no.met.jtimeseries.meteogram.SvgChartSaver.java

@Override
public void save(File file, JFreeChart chart, int width, int height) throws IOException {

    if (file == null || chart == null)
        throw new IllegalArgumentException("Null 'file' or 'chart' argument.");
    //get the genric dom imp
    DOMImplementation dom = GenericDOMImplementation.getDOMImplementation();
    //create document
    org.w3c.dom.Document document = dom.createDocument(null, "svg", null);
    //create svg 2d graphics
    SVGGraphics2D svgGenerator = new SVGGraphics2D(document);
    svgGenerator.getGeneratorContext().setPrecision(6);
    svgGenerator.setSVGCanvasSize(new Dimension(width, height));
    //render chart with svg 2D graphics        
    chart.draw(svgGenerator, new Rectangle2D.Double(0, 0, width, height));
    Element svgRoot = svgGenerator.getRoot();
    /// set SVG Canvas size (For auto resizing)
    svgRoot.setAttributeNS(null, SVGGraphics2D.SVG_VIEW_BOX_ATTRIBUTE,
            String.format("0 0 %d %d", width, height));
    svgRoot.setAttributeNS(null, "preserveAspectRatio", "xMidYMid meet");
    svgRoot.removeAttribute("width");
    svgRoot.removeAttribute("height");
    OutputStream out = new BufferedOutputStream(new FileOutputStream(file));
    Writer writer = new OutputStreamWriter(out, "UTF-8");
    svgGenerator.stream(svgRoot, writer, false, true);
}