Example usage for org.w3c.dom Element hasAttribute

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

Introduction

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

Prototype

public boolean hasAttribute(String name);

Source Link

Document

Returns true when an attribute with a given name is specified on this element or has a default value, false otherwise.

Usage

From source file:org.exoplatform.utils.ExoDocumentUtils.java

private static ExoFile getFileFromXMLElement(Element element, boolean isFolder) throws NullPointerException {
    if (element == null)
        throw new NullPointerException("Given element is null");

    ExoFile file = new ExoFile();
    if (element.hasAttribute("title")) {
        file.name = Html.fromHtml(element.getAttribute("title")).toString();
    } else {/* w  ww .  ja  v  a 2 s.  c om*/
        file.name = element.getAttribute("name");
    }
    file.workspaceName = element.getAttribute("workspaceName");
    file.path = fullURLofFile(file.workspaceName, element.getAttribute("path"));
    if (element.hasAttribute("driveName"))
        file.driveName = element.getAttribute("driveName");
    else
        file.driveName = file.name;
    file.currentFolder = element.getAttribute("currentFolder");
    if (file.currentFolder == null)
        file.currentFolder = "";
    file.isFolder = isFolder;
    if (element.hasAttribute("nodeType"))
        file.nodeType = element.getAttribute("nodeType");

    String canRemove = element.getAttribute("canRemove");
    file.canRemove = Boolean.parseBoolean(canRemove.trim());

    return file;
}

From source file:org.gephi.desktop.preview.PresetUtils.java

private PreviewPreset readXML(Document document) {
    DefaultPreset defaultPreset = new DefaultPreset();//For retrieving property class if it is not in the xml (old serialization)

    Element presetE = document.getDocumentElement();
    Map<String, Object> propertiesMap = new HashMap<>();
    String presetName = presetE.getAttribute("name");

    NodeList propertyList = presetE.getElementsByTagName("previewproperty");
    for (int i = 0; i < propertyList.getLength(); i++) {
        Node n = propertyList.item(i);
        if (n.getNodeType() == Node.ELEMENT_NODE) {
            Element propertyE = (Element) n;
            String name = propertyE.getAttribute("name");
            String valueClassName = propertyE.hasAttribute(name) ? propertyE.getAttribute("class") : null;
            String stringValue = propertyE.getTextContent();
            Class valueClass = null;
            if (valueClassName != null) {
                try {
                    valueClass = Class.forName(valueClassName);
                } catch (ClassNotFoundException ex) {
                    Exceptions.printStackTrace(ex);
                }/*from  w w w  . j  a  va 2  s .  c  o m*/
            } else {
                Object defaultValue = defaultPreset.getProperties().get(name);
                if (defaultValue != null) {
                    valueClass = defaultValue.getClass();
                }
            }
            if (valueClass != null) {
                Object value = PreviewProperties.readValueFromText(stringValue, valueClass);
                if (value != null) {
                    propertiesMap.put(name, value);
                }
            }
        }
    }
    return new PreviewPreset(presetName, propertiesMap);
}

From source file:org.gvnix.support.WebProjectUtils.java

/**
 * Replaces all namespaces occurrences contained in {@code oldUriMap} in
 * given {@relativePath} file with namespaces occurrences contained in
 * {@code newUriMap}.//from www . ja v  a 2  s .com
 * <p/>
 * A namespace in given {@relativePath} file will be replaced by a namespace
 * in {@code newUriMap} when one of the conditions below is true:
 * <p/>
 * <strong>A.</strong> Namespace name is in {@code oldUriMap}, as is in
 * {@code newUriMap} and as is in {@relativePath} file and old namespace
 * (value in {@code oldUriMap}) match with namespace in jspx.
 * <p/>
 * <strong>B.</strong> Namespace name is not in {@code oldUriMap} and
 * namespace name is in {@relativePath} file
 * 
 * @param relativePath XML file to update. The path must be relative to
 *        {@code src/main/webapp} (cannot be null, but may be empty if
 *        referring to the path itself)
 * @param oldUriMap (optional) Keys are namespace names (ex: "xmlns:page")
 *        and values are the old namespace URI (ex:
 *        "urn:jsptagdir:/WEB-INF/tags/form") that must match with the
 *        namespace URI in the XML
 * @param newUriMap Keys are namespace names (ex: "xmlns:page") and values
 *        are the new namespace URI (ex:
 *        "urn:jsptagdir:/WEB-INF/tags/datatables")
 * @param projectOperations
 * @param fileManager
 */
public static void updateTagxUriInJspx(String relativePath, Map<String, String> oldUriMap,
        Map<String, String> newUriMap, ProjectOperations projectOperations, FileManager fileManager) {

    // If null, create default oldUriMap causing jspx will be updated with
    // all URIs in newUriMap
    if (oldUriMap == null) {
        oldUriMap = new HashMap<String, String>();
    }

    // Get jspx file path
    PathResolver pathResolver = projectOperations.getPathResolver();
    String docJspx = pathResolver.getIdentifier(getWebappPath(projectOperations), relativePath);

    // Parse XML document
    Document docJspXml = loadXmlDocument(docJspx, fileManager);
    if (docJspXml == null) {
        // file not found: do nothing
        return;
    }

    // Get main div
    Element docRoot = docJspXml.getDocumentElement();
    Element divMain = XmlUtils.findFirstElement("/div", docRoot);
    boolean modified = false;

    // Update namespace URIs
    for (Entry<String, String> newUriEntry : newUriMap.entrySet()) {
        String nsName = newUriEntry.getKey();
        String nsUri = newUriEntry.getValue();

        // Namespace name is in oldUriMap, as is in and as is in given file
        // and old namespace (value in oldUriMap) match with namespace in
        // jspx
        if (oldUriMap.containsKey(nsName) && divMain.hasAttribute(nsName)) {
            String oldNsUri = oldUriMap.get(nsName);
            String currentUri = divMain.getAttribute(nsName);

            if (StringUtils.isEmpty(oldNsUri) || oldNsUri.equalsIgnoreCase(currentUri)) {
                // Compares new value with current before change
                if (!StringUtils.equalsIgnoreCase(currentUri, nsUri)) {
                    divMain.setAttribute(nsName, nsUri);
                    modified = true;
                }
            }
        }

        // Namespace name is not in oldUriMap and namespace name is in
        // given file
        if (!oldUriMap.containsKey(nsName) && divMain.hasAttribute(nsName)) {
            // Compares new value with current before change
            if (!StringUtils.equalsIgnoreCase(divMain.getAttribute(nsName), nsUri)) {
                divMain.setAttribute(nsName, nsUri);
                modified = true;
            }
        }
    }

    // If modified, update the jspx file
    if (modified) {
        DomUtils.removeTextNodes(docJspXml);
        fileManager.createOrUpdateTextFileIfRequired(docJspx, XmlUtils.nodeToString(docJspXml), true);
    }
}

From source file:org.hammurapi.HammurapiTask.java

/**
 * @param config//  w w w  .j a  va 2 s.c  om
 * @throws ParseException
 */
protected void setAttributes(Element config) {
    super.setAttributes(config);
    if (config.hasAttribute("host-id")) {
        setHostId(config.getAttribute("host-id"));
    }
    if (config.hasAttribute("baseline")) {
        try {
            setBaseLine(
                    new SimpleDateFormat(HammurapiArchiver.DATE_FORMAT).parse(config.getAttribute("baseline")));
        } catch (java.text.ParseException e) {
            throw new BuildException("Cannot parse baseline date", e);
        }
    }
}

From source file:org.hammurapi.TaskBase.java

/**
* @param config// w  ww  . j a v  a  2  s .  c  o m
* @throws ParseException
*/
protected void setAttributes(Element config) {
    if (config.hasAttribute("title")) {
        setTitle(config.getAttribute("title"));
    }

    if (config.hasAttribute("dpmo-threshold")) {
        setDpmoThreshold(Integer.parseInt(config.getAttribute("dpmo-threshold")));
    }

    if (config.hasAttribute("sigma-threshold")) {
        setSigmaThreshold(Double.parseDouble(config.getAttribute("sigma-threshold")));
    }

    if (config.hasAttribute("severity-threshold")) {
        setSeverityThreshold(Integer.parseInt(config.getAttribute("severity-threshold")));
    }

    if (config.hasAttribute("force")) {
        setForce("yes".equals(config.getAttribute("force")));
    }

    if (config.hasAttribute("force-on-warnings")) {
        setForceOnWarnings("yes".equals(config.getAttribute("force-on-warnings")));
    }

    if (config.hasAttribute("review-description")) {
        setReviewDescription(config.getAttribute("review-description"));
    }

    //Anu : 20050701 for baselining
    if (config.hasAttribute("baselining")) {
        setBaselining(config.getAttribute("baselining"));
    }
}

From source file:org.hippoecm.frontend.plugins.gallery.imageutil.ScaleImageOperation.java

private void scaleSvg(final File tmpFile) throws ParserConfigurationException, SAXException, IOException {
    final Document svgDocument = readSvgDocument(tmpFile);
    final Element svg = svgDocument.getDocumentElement();

    if (svg.hasAttribute("width") && svg.hasAttribute("height")) {
        final String svgWidth = svg.getAttribute("width");
        final String svgHeight = svg.getAttribute("height");

        log.info("SVG size: {} x {}", svgWidth, svgHeight);

        final double originalWidth = readDoubleFromStart(svgWidth);
        final double originalHeight = readDoubleFromStart(svgHeight);

        final double resizeRatio = calculateResizeRatio(originalWidth, originalHeight, width, height);

        scaledWidth = (int) Math.max(originalWidth * resizeRatio, 1);
        scaledHeight = (int) Math.max(originalHeight * resizeRatio, 1);

        // save variant with scaled dimensions
        svg.setAttribute("width", Integer.toString(scaledWidth));
        svg.setAttribute("height", Integer.toString(scaledHeight));

        // add a viewbox when not present, so scaled variants still show the full image
        if (!svg.hasAttribute("viewBox")) {
            svg.setAttribute("viewBox", "0 0 " + originalWidth + " " + originalHeight);
        }/*  www  .  j  a  va  2s  .  com*/

        writeSvgDocument(tmpFile, svgDocument);
    }
}

From source file:org.impalaframework.web.spring.config.WebMappingBeanDefinitionParser.java

private void handlePrefixes(Element element, ParserContext parserContext) {
    List<Element> toModules = DomUtils.getChildElementsByTagName(element, TO_MDOULE_ELEMENT);

    Map<String, ContextAndServletPath> toModulesMap = new LinkedHashMap<String, ContextAndServletPath>();

    for (Element toModulesElement : toModules) {
        String pathAttribute = toModulesElement.getAttribute(PREFIX_ATTRIBUTE);

        String servletPath = getPathAttributeValue(toModulesElement, pathAttribute, SET_SERVLET_PATH_ATTRIBUTE,
                SERVLET_PATH_ATTRIBUTE);
        String contextPath = getPathAttributeValue(toModulesElement, pathAttribute, SET_CONTEXT_PATH_ATTRIBUTE,
                CONTEXT_PATH_ATTRIBUTE);

        //check that not relying on both setServletPath and setContextPath only
        if (toModulesElement.hasAttribute(SET_SERVLET_PATH_ATTRIBUTE)
                && toModulesElement.hasAttribute(SET_CONTEXT_PATH_ATTRIBUTE)
                && !toModulesElement.hasAttribute(SERVLET_PATH_ATTRIBUTE)
                && !toModulesElement.hasAttribute(CONTEXT_PATH_ATTRIBUTE)) {
            logger.warn(/*from ww  w  .  ja  va 2 s.c o m*/
                    "Both 'setServletPath' and 'setContextPath' are used for to-handler attribute. This is ordinarily not required, and is not a recommended usage.");
        }

        ContextAndServletPath paths = new ContextAndServletPath(contextPath, servletPath);
        toModulesMap.put(pathAttribute.trim(), paths);
    }

    RootBeanDefinition definition = newContributorDefinition(toModulesMap);
    registerDefinition(parserContext, definition);
}

From source file:org.impalaframework.web.spring.config.WebMappingBeanDefinitionParser.java

private String getPathAttributeValue(Element toModulesElement, String pathAttribute,
        final String setPathAttributeName, final String pathAttributeName) {
    String setServletPathAttribute = toModulesElement.getAttribute(setPathAttributeName);
    String servletPathAttribute = toModulesElement.getAttribute(pathAttributeName);
    if (!StringUtils.hasText(servletPathAttribute)) {
        servletPathAttribute = toModulesElement.hasAttribute(pathAttributeName) ? "" : null;
    }//  w ww  .ja va  2  s  . c  om

    String servletPath = getServletPath(pathAttribute, setServletPathAttribute, servletPathAttribute);
    return servletPath;
}

From source file:org.infoscoop.request.filter.GadgetViewFilter.java

private byte[] processViewTransform(Document module, String viewType) throws Exception {
    NodeList nodeList = module.getElementsByTagName("Maximize");
    while (nodeList.getLength() > 0) {
        nodeList.item(0).getParentNode().removeChild(nodeList.item(0));
        nodeList = module.getElementsByTagName("Maximize");
    }//from w w w . j a v a2 s  .  c  o m

    nodeList = module.getElementsByTagName("Content");
    Collection contents = new ArrayList();
    for (int i = 0; i < nodeList.getLength(); i++)
        contents.add(nodeList.item(i));

    Element matches = null;
    for (Iterator ite = contents.iterator(); ite.hasNext();) {
        Element content = (Element) ite.next();
        if ("Maximize".equals(((Element) content.getParentNode()).getTagName()))
            continue;

        if (matches == null)
            matches = content;

        if (content.hasAttribute("view") && !"".equals(content.getAttribute("view"))) {
            if (content.getAttribute("view").toLowerCase().indexOf(viewType) >= 0) {
                matches = content;
                break;
            } else if ("home".equalsIgnoreCase(content.getAttribute("view"))) {
                matches = content;
            }
        } else {
            matches = content;
        }
    }

    contents.remove(matches);
    for (Iterator ite = contents.iterator(); ite.hasNext();) {
        Element content = (Element) ite.next();

        content.getParentNode().removeChild(content);
    }

    ByteArrayOutputStream respOut = new ByteArrayOutputStream();
    try {
        Transformer trans = TransformerFactory.newInstance().newTransformer();
        trans.transform(new DOMSource(module), new StreamResult(respOut));
    } catch (Exception ex) {
        throw ex;
    } finally {
        respOut.close();
    }

    return respOut.toByteArray();
}

From source file:org.infoscoop.request.filter.GadgetViewFilter.java

private static boolean hasContent(Document module, String viewType) {
    NodeList nodeList = module.getElementsByTagName("Content");
    for (int i = 0; i < nodeList.getLength(); i++) {
        Element node = (Element) nodeList.item(i);
        if (node.hasAttribute("view") && node.getAttribute("view").toLowerCase().indexOf(viewType) >= 0)
            return true;
    }/*from w w  w  . j a  v a  2  s. c  o m*/

    return false;
}