Example usage for org.w3c.dom Element getAttribute

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

Introduction

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

Prototype

public String getAttribute(String name);

Source Link

Document

Retrieves an attribute value by name.

Usage

From source file:com.omertron.yamjtrakttv.tools.CompleteMoviesTools.java

/**
 * Parse the video element and extract the information from it.
 *
 * @param eVideo/*from  w  ww  . ja  va 2s. c o  m*/
 * @return
 */
public static Video parseVideo(Element eVideo) {
    Video v = new Video();
    v.setTitle(DOMHelper.getValueFromElement(eVideo, "title"));
    v.setYear(DOMHelper.getValueFromElement(eVideo, "year"));
    v.setType(DOMHelper.getValueFromElement(eVideo, "movieType"));
    v.setWatched(Boolean.parseBoolean(DOMHelper.getValueFromElement(eVideo, "watched")));

    String stringDate = DOMHelper.getValueFromElement(eVideo, "watchedDate");
    if (StringUtils.isNumeric(stringDate) && !"0".equals(stringDate)) {
        v.setWatchedDate(new Date(Long.parseLong(stringDate)));
    } else {
        LOG.debug("Invalid watched date '" + stringDate + "' using current date");
        v.setWatchedDate(new Date());
        // Because the date was set by us, let's add a small (1 second) delay to ensure that we don't get identical watched dates
        try {
            TimeUnit.SECONDS.sleep(DEFAULT_DELAY);
        } catch (InterruptedException ex) {
            // Don't care if we are interrupted or not.
        }
    }

    NodeList nlID = eVideo.getElementsByTagName("id");
    if (nlID.getLength() > 0) {
        Node nID;
        Element eID;
        for (int loop = 0; loop < nlID.getLength(); loop++) {
            nID = nlID.item(loop);
            if (nID.getNodeType() == Node.ELEMENT_NODE) {
                eID = (Element) nID;
                String moviedb = eID.getAttribute("movieDatabase");
                if (StringUtils.isNotBlank(moviedb)) {
                    v.addId(moviedb, eID.getTextContent());
                }
            }
        }
    }

    // TV specific processing
    if (v.isTvshow()) {
        v.addEpisodes(parseTvFiles(eVideo));
    }

    return v;
}

From source file:com.concursive.connect.web.portal.ProjectPortalUtils.java

public static DashboardPage retrieveDashboardPage(ProjectPortalBean portalBean) throws Exception {

    // Use the project-portal-config.xml to determine the dashboard page to use for the current url
    URL resource = ProjectPortalUtils.class.getResource("/portal/project-portal-config.xml");
    LOG.debug("portal config file: " + resource.toString());
    XMLUtils library = new XMLUtils(resource);

    // The nodes are listed under the <navigation> tag
    Element portal = XMLUtils.getFirstChild(library.getDocumentElement(), "portal", "type", "navigation");

    String moduleLayout = null;//from www .jav a 2s  . c  o m
    String page = null;
    String modulePermission = null;

    // Look through the modules
    NodeList moduleNodeList = portal.getElementsByTagName("module");
    for (int i = 0; i < moduleNodeList.getLength(); i++) {
        Node thisModule = moduleNodeList.item(i);

        NodeList urlNodeList = ((Element) thisModule).getElementsByTagName("url");
        for (int j = 0; j < urlNodeList.getLength(); j++) {
            Element urlElement = (Element) urlNodeList.item(j);

            // Construct a ProjectPortalURL and add to the list for retrieving
            String name = urlElement.getAttribute("name");

            // <url name="/show" object="/" page="project profile"/>
            if (name.equals("/" + portalBean.getAction())) {
                String object = urlElement.getAttribute("object");
                if (object.equals("/" + portalBean.getDomainObject())) {
                    // Set the module for tab highlighting
                    portalBean.setModule(((Element) thisModule).getAttribute("name"));

                    // Set the page for looking up
                    page = urlElement.getAttribute("page");

                    // Set the layout filename to find the referenced page
                    moduleLayout = ((Element) thisModule).getAttribute("layout");

                    // Set the permission required for viewing this page
                    modulePermission = ((Element) thisModule).getAttribute("permission");
                    break;
                }
            }
        }
    }

    URL moduleResource = ProjectPortalUtils.class.getResource("/portal/" + moduleLayout);
    LOG.debug("module config file: " + moduleResource.toString());
    XMLUtils module = new XMLUtils(moduleResource);

    Element moduleElement = XMLUtils.getFirstChild(module.getDocumentElement(), "navigation");

    NodeList pageNodeList = moduleElement.getElementsByTagName("page");
    if (pageNodeList.getLength() == 0) {
        LOG.warn("No pages found in moduleLayout: " + moduleLayout);
    }

    for (int j = 0; j < pageNodeList.getLength(); j++) {
        Node thisPage = pageNodeList.item(j);

        String pageName = ((Element) thisPage).getAttribute("name");

        if (pageName.equals(page)) {
            LOG.debug("Found page " + pageName);

            // Find a portal template to use
            Project thisProject = ProjectUtils.loadProject(portalBean.getProjectId());
            DashboardPage dashboardPage = null;
            if (thisProject.getCategoryId() > -1) {
                // Use a category specific portal
                ProjectCategory category = ProjectUtils.loadProjectCategory(thisProject.getCategoryId());
                LOG.debug("Trying a category specific portal: " + page + ": "
                        + category.getDescription().toLowerCase());
                dashboardPage = DashboardUtils.loadDashboardPage(DashboardTemplateList.TYPE_NAVIGATION,
                        (portalBean.isPopup() ? page + "-popup" : page) + ": "
                                + category.getDescription().toLowerCase(),
                        moduleLayout);
            }
            if (dashboardPage == null) {
                LOG.debug("Trying a specific portal: " + page);
                dashboardPage = DashboardUtils.loadDashboardPage(DashboardTemplateList.TYPE_NAVIGATION,
                        (portalBean.isPopup() ? page + "-popup" : page), moduleLayout);
            }
            if (dashboardPage == null) {
                LOG.warn("Page not found: " + page);
                System.out.println("ProjectPortalUtils-> * PAGE NOT FOUND *");
                return null;
            }
            dashboardPage.setProjectId(thisProject.getId());
            // Set the dashboard page's module permission if it doesn't have a page permission
            if (dashboardPage.getPermission() == null) {
                dashboardPage.setPermission(modulePermission);
            }
            return dashboardPage;
        }
    }
    return null;
}

From source file:de.codesourcery.utils.xml.XmlHelper.java

public static String getAttribute(Element root, String attrName, boolean isRequired) throws ParseException {

    String sValue = root.getAttribute(attrName);

    if (!StringUtils.isBlank(sValue)) {
        return sValue;
    } else {/*  w  ww.j  av  a  2  s .c o  m*/

        if (isRequired) {
            final String msg = "Tag <" + root.getNodeName() + "> is lacking required attribute " + attrName;
            log.error("getAttribute(): " + msg);
            throw new ParseException(msg, -1);
        }

        return null;
    }
}

From source file:fll.xml.XMLUtils.java

/**
 * @see #getDoubleAttributeValue(Element, String)
 *//*from w  w w  .j a  v  a2 s  . co m*/
@SuppressFBWarnings(value = {
        "NP_BOOLEAN_RETURN_NULL" }, justification = "Need to return Null so that we can determine when there is no score")
public static Boolean getBooleanAttributeValue(final Element element, final String attributeName) {
    if (null == element) {
        return null;
    }
    final String str = element.getAttribute(attributeName);
    if (str.isEmpty()) {
        return null;
    } else {
        return Boolean.valueOf(str);
    }
}

From source file:it.polimi.diceH2020.plugin.control.JSonReader.java

private static Node searchElement(NodeList nodes, String deviceId) {
    Node n = null;//w w w  .jav a  2s .  c o  m
    String baseDevice;
    Element nElement = null;
    for (int i = 0; i < nodes.getLength(); i++) {
        n = nodes.item(i);
        nElement = (Element) n;
        baseDevice = nElement.getAttribute("base_Device");
        if (deviceId.equals(baseDevice)) {
            return n;
        }
    }
    return null;
}

From source file:com.hdsfed.cometapi.XMLHelper.java

public static List<Coordinates> ExtractCoordinates(Document doc) {
    List<Coordinates> coordinateList = new ArrayList<Coordinates>();
    NodeList nList = doc.getElementsByTagName("Corner");
    for (int temp = 0; temp < nList.getLength(); temp++) {
        Node nNode = nList.item(temp);
        if (nNode.getNodeType() == Node.ELEMENT_NODE) {
            Element eElement = (Element) nNode;
            if (eElement != null) {
                coordinateList.add(new Coordinates(eElement.getAttribute("name"), getTagValue("X", eElement),
                        getTagValue("Y", eElement)));
            } // end if eelement
        } // end if nnode.getnodetype()
    } //end for int temp
    return coordinateList;
}

From source file:de.codesourcery.utils.xml.XmlHelper.java

public static int getIntAttribute(Element root, String attrName, boolean isRequired) throws ParseException {

    String sValue = root.getAttribute(attrName);

    if (!StringUtils.isBlank(sValue)) {
        try {/*from  w  w w  . j  a  va 2 s.  com*/
            return Integer.parseInt(sValue);
        } catch (NumberFormatException ex) {
            final String msg = "Invalid attribute value (not a number) for tag <" + root.getNodeName() + ">"
                    + " , attribute " + attrName + " , value '" + sValue + "'";
            log.error("getIntAttribute(): " + msg);
            throw new ParseException(msg, -1);
        }
    } else {

        if (isRequired) {
            final String msg = "Tag <" + root.getNodeName() + "> is lacking required integer attribute "
                    + attrName;
            log.error("getIntAttribute(): " + msg);
            throw new ParseException(msg, -1);
        }

        return 0;
    }
}

From source file:Main.java

public static ArrayList<String> getNodeListAttValAsStringCols(String Xpath, Node node, String[] attrNames,
        String sep) throws Exception {
    ArrayList<String> retV = new ArrayList<String>();

    if (sep == null) {
        sep = " ";
    }//from   w ww  .jav a2s .  c o m
    int aNamesL = attrNames.length;
    if (aNamesL > 0) {

        NodeList nl = getNodesListXpathNode(Xpath, node);
        int l = nl.getLength();
        Element e = null;
        String val = "";

        for (int i = 0; i < l; i++) {
            e = (Element) nl.item(i);
            if (e.getNodeType() == Node.ELEMENT_NODE) {
                StringBuilder sb = new StringBuilder();
                for (int y = 0; y < aNamesL; y++) {
                    sb.append(e.getAttribute(attrNames[y]));
                    if (y < aNamesL - 1) {
                        sb.append(sep);
                    }
                }
                val = sb.toString();
                if (val != null && val.length() > 0) {
                    retV.add(val);
                }
            }
        }
    }
    return retV;
}

From source file:eu.eidas.auth.engine.configuration.dom.DOMConfigurationParser.java

/**
 * Generate parameters.//w ww.jav a 2s.  c o  m
 *
 * @param configurationTag the configuration node
 * @return the map< string, string>
 */
@Nonnull
private static ImmutableMap<String, String> parseParameters(@Nonnull String configurationFileName,
        @Nonnull String instanceName, @Nonnull String configurationName, @Nonnull Element configurationTag)
        throws SamlEngineConfigurationException {
    Map<String, String> parameters = new LinkedHashMap<String, String>();

    NodeList parameterTags = configurationTag
            .getElementsByTagName(InstanceTag.ConfigurationTag.ParameterTag.TAG_NAME);

    for (int i = 0, n = parameterTags.getLength(); i < n; i++) {
        // for every parameter find, process.
        Element parameterTag = (Element) parameterTags.item(i);
        String parameterName = parameterTag
                .getAttribute(InstanceTag.ConfigurationTag.ParameterTag.Attribute.NAME);
        String parameterValue = parameterTag
                .getAttribute(InstanceTag.ConfigurationTag.ParameterTag.Attribute.VALUE);

        // verify the content.
        if (StringUtils.isBlank(parameterName)) {
            String message = "SAML engine configuration file \"" + configurationFileName
                    + "\" contains a blank parameter name for configuration name \"" + configurationName
                    + "\" in instance name \"" + instanceName + "\"";
            LOG.error(message);
            throw new SamlEngineConfigurationException(message);
        }
        if (StringUtils.isBlank(parameterValue)) {
            String message = "SAML engine configuration file \"" + configurationFileName
                    + "\" contains parameter name \"" + parameterName
                    + "\" with a blank value for configuration name \"" + configurationName
                    + "\" in instance name \"" + instanceName + "\"";
            LOG.error(message);
            throw new SamlEngineConfigurationException(message);
        }
        String previous = parameters.put(parameterName.trim(), parameterValue.trim());
        if (null != previous) {
            String message = "Duplicate parameter entry names \"" + parameterName
                    + "\" in SAML engine configuration file \"" + configurationFileName
                    + "\" for configuration name \"" + configurationName + "\" in instance name \""
                    + instanceName + "\"";
            LOG.error(message);
            throw new SamlEngineConfigurationException(message);
        }
    }
    return ImmutableMap.copyOf(parameters);
}

From source file:com.moadbus.banking.iso.core.protocol.ConfigParser.java

/**
 * Reads the XML from the stream and configures the message factory with its
 * values.//from   www. jav  a  2 s.co  m
 *
 * @param mfact
 *            The message factory to be configured with the values read from
 *            the XML.
 * @param stream
 *            The InputStream containing the XML configuration.
 */
protected static void parse(MessageFactory mfact, InputStream stream) throws IOException {
    final DocumentBuilderFactory docfact = DocumentBuilderFactory.newInstance();
    DocumentBuilder docb = null;
    Document doc = null;
    try {
        docb = docfact.newDocumentBuilder();
        doc = docb.parse(stream);
    } catch (ParserConfigurationException ex) {
        log.error("parse: Cannot parse XML configuration", ex);
        return;
    } catch (SAXException ex) {
        log.error("parse: Parsing XML configuration", ex);
        return;
    }
    final Element root = doc.getDocumentElement();

    // Read the ISO headers
    NodeList nodes = root.getElementsByTagName("header");
    Element elem = null;
    for (int i = 0; i < nodes.getLength(); i++) {
        elem = (Element) nodes.item(i);
        int type = parseType(elem.getAttribute("type"));
        if (type == -1) {
            throw new IOException("Invalid type for header: " + elem.getAttribute("type"));
        }
        if (elem.getChildNodes() == null || elem.getChildNodes().getLength() == 0) {
            throw new IOException("Invalid header element");
        }
        String header = elem.getChildNodes().item(0).getNodeValue();
        if (log.isTraceEnabled()) {
            log.trace("Adding ISO header for type " + elem.getAttribute("type") + ": " + header);
        }
        mfact.setIsoHeader(type, header);
    }

    // Read the message templates
    nodes = root.getElementsByTagName("template");
    for (int i = 0; i < nodes.getLength(); i++) {
        elem = (Element) nodes.item(i);
        int type = parseType(elem.getAttribute("type"));
        if (type == -1) {
            throw new IOException("Invalid type for template: " + elem.getAttribute("type"));
        }
        NodeList fields = elem.getElementsByTagName("field");
        IsoMessage m = new IsoMessage();
        m.setType(type);
        for (int j = 0; j < fields.getLength(); j++) {
            Element f = (Element) fields.item(j);
            int num = Integer.parseInt(f.getAttribute("num"));
            IsoType itype = IsoType.valueOf(f.getAttribute("type"));
            int length = 0;
            if (f.getAttribute("length").length() > 0) {
                length = Integer.parseInt(f.getAttribute("length"));
            }
            String v = f.getChildNodes().item(0).getNodeValue();
            m.setValue(num, v, itype, length);
        }
        mfact.addMessageTemplate(m);
    }

    // Read the parsing guides
    nodes = root.getElementsByTagName("parse");
    for (int i = 0; i < nodes.getLength(); i++) {
        elem = (Element) nodes.item(i);
        int type = parseType(elem.getAttribute("type"));
        if (type == -1) {
            throw new IOException("Invalid type for parse guide: " + elem.getAttribute("type"));
        }
        NodeList fields = elem.getElementsByTagName("field");
        HashMap<Integer, FieldParseInfo> parseMap = new HashMap<Integer, FieldParseInfo>();
        for (int j = 0; j < fields.getLength(); j++) {
            Element f = (Element) fields.item(j);
            int num = Integer.parseInt(f.getAttribute("num"));
            IsoType itype = IsoType.valueOf(f.getAttribute("type"));
            int length = 0;
            if (f.getAttribute("length").length() > 0) {
                length = Integer.parseInt(f.getAttribute("length"));
            }
            parseMap.put(num, new FieldParseInfo(itype, length));
        }
        mfact.setParseMap(type, parseMap);
    }

}