Example usage for org.jdom2 Element getAttribute

List of usage examples for org.jdom2 Element getAttribute

Introduction

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

Prototype

public Attribute getAttribute(final String attname) 

Source Link

Document

This returns the attribute for this element with the given name and within no namespace, or null if no such attribute exists.

Usage

From source file:com.locutus.outils.graphes.ConvertisseurXGMLGraphe.java

License:Open Source License

/**
 * //from   w  ww  .  j a  v  a 2s. c  om
 * @param dgc
 * @param el
 * @throws GraphConversionException
 */
private void createNodeAndAdd(DiGraph<String> dgc, Element el) throws GraphConversionException {
    // On cre un itrateur sur les informations du Node qu'on a
    // pralablement dtect.
    Iterator<Element> it3 = el.getChildren().iterator();
    // On prpare la rcupration de l'id du node et du nomFichier du
    // Concept.
    int[] id = new int[1];
    String nomFichierConcept = null;

    while (it3.hasNext()) {

        Element local3 = it3.next();

        // On rcupre l'id
        if (local3.getAttribute("key") != null && local3.getAttribute("key").getValue().equals("id"))
            id[0] = Integer.parseInt(local3.getText());

        // On rcupre le nom
        else if (local3.getAttribute("key") != null && local3.getAttribute("key").getValue().equals("label"))
            nomFichierConcept = new String(local3.getText());

    }

    // Si le nom n'est pas vide
    if (nomFichierConcept != null) {
        idNode.put(id, new Node<String>(nomFichierConcept));
    } else {
        throw new GraphConversionException(
                "Il manque le nom fichier du concept pour le node en cours de traitement");
    }

}

From source file:com.locutus.outils.graphes.ConvertisseurXGMLGraphe.java

License:Open Source License

/**
 * //  ww w. j  a v a  2s .  c  o m
 * @param dgc
 * @param local2
 */
private void createEdge(DiGraph<String> dgc, Element local2) {
    Iterator<Element> it3 = local2.getChildren().iterator();
    int[] arcId = new int[2];

    while (it3.hasNext()) {

        Element local3 = it3.next();

        if (local3.getAttribute("key") != null && local3.getAttribute("key").getValue().equals("source"))
            arcId[0] = Integer.parseInt(local3.getText());
        else if (local3.getAttribute("key") != null && local3.getAttribute("key").getValue().equals("target"))
            arcId[1] = Integer.parseInt(local3.getText());

    }
    edges.add(arcId);
}

From source file:com.novell.ldapchai.impl.edir.NmasResponseSet.java

License:Open Source License

static ChallengeSet parseNmasUserResponseXML(final String str)
        throws IOException, JDOMException, ChaiValidationException {
    final List<Challenge> returnList = new ArrayList<Challenge>();

    final Reader xmlreader = new StringReader(str);
    final SAXBuilder builder = new SAXBuilder();
    final Document doc = builder.build(xmlreader);

    final Element rootElement = doc.getRootElement();
    final int minRandom = StringHelper.convertStrToInt(rootElement.getAttributeValue("RandomQuestions"), 0);

    final String guidValue;
    {// w w  w  .  j a v  a  2s.c om
        final Attribute guidAttribute = rootElement.getAttribute("GUID");
        guidValue = guidAttribute == null ? null : guidAttribute.getValue();
    }

    for (Iterator iter = doc.getDescendants(new ElementFilter("Challenge")); iter.hasNext();) {
        final Element loopQ = (Element) iter.next();
        final int maxLength = StringHelper.convertStrToInt(loopQ.getAttributeValue("MaxLength"), 255);
        final int minLength = StringHelper.convertStrToInt(loopQ.getAttributeValue("MinLength"), 2);
        final String defineStrValue = loopQ.getAttributeValue("Define");
        final boolean adminDefined = defineStrValue.equalsIgnoreCase("Admin");
        final String typeStrValue = loopQ.getAttributeValue("Type");
        final boolean required = typeStrValue.equalsIgnoreCase("Required");
        final String challengeText = loopQ.getText();

        final Challenge challenge = new ChaiChallenge(required, challengeText, minLength, maxLength,
                adminDefined, 0, false);
        returnList.add(challenge);
    }

    return new ChaiChallengeSet(returnList, minRandom, null, guidValue);
}

From source file:com.novell.ldapchai.impl.edir.value.nspmComplexityRules.java

License:Open Source License

private static List<Policy> readComplexityPoliciesFromXML(final String input) {
    final List<Policy> returnList = new ArrayList<Policy>();
    try {// ww  w .  j av a2 s. c o m
        final SAXBuilder builder = new SAXBuilder();
        final Document doc = builder.build(new StringReader(input));
        final Element rootElement = doc.getRootElement();

        final List policyElements = rootElement.getChildren("Policy");
        for (final Object policyNode : policyElements) {
            final Element policyElement = (Element) policyNode;
            final List<RuleSet> returnRuleSets = new ArrayList<RuleSet>();
            for (final Object ruleSetObjects : policyElement.getChildren("RuleSet")) {
                final Element loopRuleSet = (Element) ruleSetObjects;
                final Map<Rule, String> returnRules = new HashMap<Rule, String>();
                int violationsAllowed = 0;

                final org.jdom2.Attribute violationsAttribute = loopRuleSet.getAttribute("ViolationsAllowed");
                if (violationsAttribute != null && violationsAttribute.getValue().length() > 0) {
                    violationsAllowed = Integer.parseInt(violationsAttribute.getValue());
                }

                for (final Object ruleObject : loopRuleSet.getChildren("Rule")) {
                    final Element loopRuleElement = (Element) ruleObject;

                    final List ruleAttributes = loopRuleElement.getAttributes();
                    for (final Object attributeObject : ruleAttributes) {
                        final org.jdom2.Attribute loopAttribute = (org.jdom2.Attribute) attributeObject;

                        final Rule rule = Rule.valueOf(loopAttribute.getName());
                        final String value = loopAttribute.getValue();
                        returnRules.put(rule, value);
                    }
                }
                returnRuleSets.add(new RuleSet(violationsAllowed, returnRules));
            }
            returnList.add(new Policy(returnRuleSets));
        }
    } catch (JDOMException e) {
        LOGGER.debug("error parsing stored response record: " + e.getMessage());
    } catch (IOException e) {
        LOGGER.debug("error parsing stored response record: " + e.getMessage());
    } catch (NullPointerException e) {
        LOGGER.debug("error parsing stored response record: " + e.getMessage());
    } catch (IllegalArgumentException e) {
        LOGGER.debug("error parsing stored response record: " + e.getMessage());
    }
    return returnList;
}

From source file:com.rometools.modules.atom.io.AtomModuleParser.java

License:Apache License

protected String getAttributeValue(final Element e, final String attributeName) {
    Attribute attr = e.getAttribute(attributeName);
    if (attr == null) {
        attr = e.getAttribute(attributeName, NS);
    }// w  w w.  j  ava2  s.c  o m
    if (attr != null) {
        return attr.getValue();
    } else {
        return null;
    }
}

From source file:com.rometools.modules.itunes.io.ITunesParser.java

License:Open Source License

@Override
public com.rometools.rome.feed.module.Module parse(final Element element, final Locale locale) {
    AbstractITunesObject module = null;/*  www  . j  a  v a2 s. c om*/

    if (element.getName().equals("channel")) {
        final FeedInformationImpl feedInfo = new FeedInformationImpl();
        module = feedInfo;

        // Now I am going to get the channel specific tags
        final Element owner = element.getChild("owner", ns);

        if (owner != null) {
            final Element name = owner.getChild("name", ns);

            if (name != null) {
                feedInfo.setOwnerName(name.getValue().trim());
            }

            final Element email = owner.getChild("email", ns);

            if (email != null) {
                feedInfo.setOwnerEmailAddress(email.getValue().trim());
            }
        }

        final Element image = element.getChild("image", ns);

        if (image != null && image.getAttributeValue("href") != null) {
            try {
                final URL imageURL = new URL(image.getAttributeValue("href").trim());
                feedInfo.setImage(imageURL);
            } catch (final MalformedURLException e) {
                LOG.debug("Malformed URL Exception reading itunes:image tag: {}",
                        image.getAttributeValue("href"));
            }
        }

        final List<Element> categories = element.getChildren("category", ns);
        for (final Element element2 : categories) {
            final Element category = element2;
            if (category != null && category.getAttribute("text") != null) {
                final Category cat = new Category();
                cat.setName(category.getAttribute("text").getValue().trim());

                final Element subcategory = category.getChild("category", ns);

                if (subcategory != null && subcategory.getAttribute("text") != null) {
                    final Subcategory subcat = new Subcategory();
                    subcat.setName(subcategory.getAttribute("text").getValue().trim());
                    cat.setSubcategory(subcat);
                }

                feedInfo.getCategories().add(cat);
            }
        }

    } else if (element.getName().equals("item")) {
        final EntryInformationImpl entryInfo = new EntryInformationImpl();
        module = entryInfo;

        // Now I am going to get the item specific tags

        final Element duration = element.getChild("duration", ns);

        if (duration != null && duration.getValue() != null) {
            final Duration dur = new Duration(duration.getValue().trim());
            entryInfo.setDuration(dur);
        }
    }
    if (module != null) {
        // All these are common to both Channel and Item
        final Element author = element.getChild("author", ns);

        if (author != null && author.getText() != null) {
            module.setAuthor(author.getText());
        }

        final Element block = element.getChild("block", ns);

        if (block != null) {
            module.setBlock(true);
        }

        final Element explicit = element.getChild("explicit", ns);

        if (explicit != null && explicit.getValue() != null
                && explicit.getValue().trim().equalsIgnoreCase("yes")) {
            module.setExplicit(true);
        }

        final Element keywords = element.getChild("keywords", ns);

        if (keywords != null) {
            final StringTokenizer tok = new StringTokenizer(getXmlInnerText(keywords).trim(), ",");
            final String[] keywordsArray = new String[tok.countTokens()];

            for (int i = 0; tok.hasMoreTokens(); i++) {
                keywordsArray[i] = tok.nextToken();
            }

            module.setKeywords(keywordsArray);
        }

        final Element subtitle = element.getChild("subtitle", ns);

        if (subtitle != null) {
            module.setSubtitle(subtitle.getTextTrim());
        }

        final Element summary = element.getChild("summary", ns);

        if (summary != null) {
            module.setSummary(summary.getTextTrim());
        }
    }

    return module;
}

From source file:com.rometools.modules.opensearch.impl.OpenSearchModuleParser.java

License:Apache License

/** Use feed links and/or xml:base attribute to determine baseURI of feed */
private static URL findBaseURI(final Element root) {
    URL baseURI = null;/*from   w  ww.ja  v  a  2s .  co m*/
    final List<Element> linksList = root.getChildren("link", OS_NS);
    if (linksList != null) {
        for (final Element element : linksList) {
            final Element link = element;
            if (!root.equals(link.getParent())) {
                break;
            }
            String href = link.getAttribute("href").getValue();
            if (link.getAttribute("rel", OS_NS) == null
                    || link.getAttribute("rel", OS_NS).getValue().equals("alternate")) {
                href = resolveURI(null, link, href);
                try {
                    baseURI = new URL(href);
                    break;
                } catch (final MalformedURLException e) {
                    System.err.println("Base URI is malformed: " + href);
                }
            }
        }
    }
    return baseURI;
}

From source file:com.rometools.modules.sle.io.ModuleParser.java

License:Apache License

/**
 * Parses the XML node (JDOM element) extracting module information.
 * <p>//from  www  . ja va 2s  .co  m
 *
 * @param element the XML node (JDOM element) to extract module information from.
 * @return a module instance, <b>null</b> if the element did not have module information.
 */
@Override
public Module parse(final Element element, final Locale locale) {
    if (element.getChild("treatAs", NS) == null) {
        return null;
    }

    final SimpleListExtension sle = new SimpleListExtensionImpl();
    sle.setTreatAs(element.getChildText("treatAs", NS));

    final Element listInfo = element.getChild("listinfo", NS);
    ArrayList<Object> values = new ArrayList<Object>();
    for (final Element ge : listInfo.getChildren("group", NS)) {
        final Namespace ns = ge.getAttribute("ns") == null ? element.getNamespace()
                : Namespace.getNamespace(ge.getAttributeValue("ns"));
        final String elementName = ge.getAttributeValue("element");
        final String label = ge.getAttributeValue("label");
        values.add(new Group(ns, elementName, label));
    }

    sle.setGroupFields(values.toArray(new Group[values.size()]));
    values = values.size() == 0 ? values : new ArrayList<Object>();

    for (final Element se : listInfo.getChildren("sort", NS)) {
        LOG.debug("Parse cf:sort {}{}", se.getAttributeValue("element"), se.getAttributeValue("data-type"));
        final Namespace ns = se.getAttributeValue("ns") == null ? element.getNamespace()
                : Namespace.getNamespace(se.getAttributeValue("ns"));
        final String elementName = se.getAttributeValue("element");
        final String label = se.getAttributeValue("label");
        final String dataType = se.getAttributeValue("data-type");
        final boolean defaultOrder = se.getAttributeValue("default") == null ? false
                : new Boolean(se.getAttributeValue("default")).booleanValue();
        values.add(new Sort(ns, elementName, dataType, label, defaultOrder));
    }

    sle.setSortFields(values.toArray(new Sort[values.size()]));
    insertValues(sle, element.getChildren());

    return sle;
}

From source file:com.rometools.propono.atom.client.ClientCollection.java

License:Open Source License

@Override
protected void parseCollectionElement(final Element element) throws ProponoException {
    if (workspace == null) {
        return;/*from  ww w .  j ava  2s .  c  o  m*/
    }

    setHref(element.getAttribute("href").getValue());

    final Element titleElem = element.getChild("title", AtomService.ATOM_FORMAT);
    if (titleElem != null) {
        setTitle(titleElem.getText());
        if (titleElem.getAttribute("type", AtomService.ATOM_FORMAT) != null) {
            setTitleType(titleElem.getAttribute("type", AtomService.ATOM_FORMAT).getValue());
        }
    }

    final List<Element> acceptElems = element.getChildren("accept", AtomService.ATOM_PROTOCOL);
    if (acceptElems != null && !acceptElems.isEmpty()) {
        for (final Element acceptElem : acceptElems) {
            addAccept(acceptElem.getTextTrim());
        }
    }

    // Loop to parse <app:categories> element to Categories objects
    final List<Element> catsElems = element.getChildren("categories", AtomService.ATOM_PROTOCOL);
    for (final Element catsElem : catsElems) {
        final Categories cats = new ClientCategories(catsElem, this);
        addCategories(cats);
    }
}

From source file:com.rometools.propono.atom.common.Collection.java

License:Apache License

protected void parseCollectionElement(final Element element) throws ProponoException {
    setHref(element.getAttribute("href").getValue());

    final Element titleElem = element.getChild("title", AtomService.ATOM_FORMAT);
    if (titleElem != null) {
        setTitle(titleElem.getText());//from   w  w  w  . j  a v a 2s  .  co  m
        if (titleElem.getAttribute("type", AtomService.ATOM_FORMAT) != null) {
            setTitleType(titleElem.getAttribute("type", AtomService.ATOM_FORMAT).getValue());
        }
    }

    final List<Element> acceptElems = element.getChildren("accept", AtomService.ATOM_PROTOCOL);
    if (acceptElems != null && !acceptElems.isEmpty()) {
        for (final Element acceptElem : acceptElems) {
            addAccept(acceptElem.getTextTrim());
        }
    }

    // Loop to parse <app:categories> element to Categories objects
    final List<Element> catsElems = element.getChildren("categories", AtomService.ATOM_PROTOCOL);
    for (final Element catsElem : catsElems) {
        final Categories cats = new Categories(catsElem, baseURI);
        addCategories(cats);
    }
}