Example usage for org.dom4j Attribute getValue

List of usage examples for org.dom4j Attribute getValue

Introduction

In this page you can find the example usage for org.dom4j Attribute getValue.

Prototype

String getValue();

Source Link

Document

Returns the value of the attribute.

Usage

From source file:ch.javasoft.xml.config.XmlConfig.java

License:BSD License

protected void resolveAttributeValue(Attribute att, String path) throws XmlConfigException {
    int start;/*  w  ww.  ja v a 2  s. c om*/
    int end = -1;

    String value = att.getValue();
    String resValue = "";
    while ((start = value.indexOf("${", end + 1)) != -1) {
        resValue += value.substring(end + 1, start);
        end = value.indexOf('}', start + 2);
        if (end == -1) {
            throw new XmlConfigException(
                    "closing } missing for attribute \"" + att.getName() + "=" + value + "\"", path);
        }
        resValue += getResolvedAttributeValue(att, path, value.substring(start + 2, end));
    }
    resValue += value.substring(end + 1, value.length());
    att.setValue(resValue);
}

From source file:ch.javasoft.xml.config.XmlConfig.java

License:BSD License

protected String getResolvedAttributeValue(Attribute att, String path, String resolveKey)
        throws XmlConfigException {
    for (ResolveKey key : ResolveKey.values()) {
        if (key.matches(resolveKey)) {
            return key.resolve(att, path, resolveKey, this);
        }/* ww  w .  j a  v  a  2  s.c o  m*/
    }
    throw new XmlConfigException("cannot resolve '" + resolveKey + "' for attribute \"" + att.getName() + "="
            + att.getValue() + "\"", path);
}

From source file:ch.javasoft.xml.config.XmlPrint.java

License:BSD License

/**
 * Print the given element using the given print writer and initial 
 * indention//w  w  w  .j  a  v a2 s . co m
 * @param elem      the xml element
 * @param indention   the initial indention
 * @param writer   the print writer to use for the output
 */
@SuppressWarnings("unchecked")
public void print(Element elem, String indention, PrintWriter writer) {
    writer.print(indention + "<" + elem.getName());

    Iterator<Attribute> itAtt = elem.attributeIterator();
    Iterator<Element> itElem = elem.elementIterator();

    if (elem.hasMixedContent() || (elem.hasContent() && !itElem.hasNext())) {
        Iterator<Node> it = elem.nodeIterator();
        while (it.hasNext()) {
            Node node = it.next();
            if (node instanceof CharacterData) {
                if (!(node instanceof Comment) && node.getText().trim().length() != 0) {
                    throw new IllegalArgumentException(
                            "text content not supported: \"" + node.getText() + "\"");
                }
            } else if (!(node instanceof Element || node instanceof Attribute)) {
                throw new IllegalArgumentException("only attributes and elements are supported");
            }
        }
    }
    while (itAtt.hasNext()) {
        Attribute att = itAtt.next();
        final String attName = att.getName();
        final String attValue = att.getValue();
        writer.print(" " + attName + "=\"" + escapeAttributeValue(attValue) + "\"");
    }
    if (!itElem.hasNext()) {
        writer.println("/>");
    } else {
        writer.println(">");
        while (itElem.hasNext()) {
            print(itElem.next(), indention + getIndention(), writer);
        }
        writer.println(indention + "</" + elem.getName() + ">");
    }
    writer.flush();
}

From source file:ch.javasoft.xml.config.XmlUtil.java

License:BSD License

/**
 * Returns the desired attribute value, or throws an exception no such
 * attribute exists./*from w  w  w .j  ava2s  . c o  m*/
 * 
 * @param element            the element to get the attribute value from
 * @param attribute            specifies the desired attribute name
 * @return                  the attribute value
 * @throws XmlConfigException   if no such attribute value is defined
 */
public static String getRequiredAttributeValue(Element element, XmlNode attribute) throws XmlConfigException {
    Attribute att = element.attribute(attribute.getXmlName());
    if (att == null) {
        throw new XmlConfigException(
                "missing " + attribute.getXmlName() + " attribute for " + element.getName() + " element",
                element);
    }
    return att.getValue();
}

From source file:ch.javasoft.xml.config.XmlUtil.java

License:BSD License

/**
 * Returns the desired attribute value, or the given default value if no 
 * such attribute exists./*from   w w w . j av  a  2s .co m*/
 * 
 * @param element            the element to get the attribute value from
 * @param attribute            specifies the desired attribute name
 * @param defaultValue         the default value to use if no such 
 *                         attribute exists
 * @return                  the attribute value, or the default if no
 *                         such attribute exists
 */
public static String getOptionalAttributeValue(Element element, XmlNode attribute, String defaultValue)
        throws XmlConfigException {
    Attribute att = element.attribute(attribute.getXmlName());
    if (att == null) {
        return defaultValue;
    }
    return att.getValue();
}

From source file:com.amalto.workbench.utils.XmlUtil.java

License:Open Source License

public static List findLinks(Document document) throws DocumentException {

    List<String> urls = new ArrayList();

    List list = document.selectNodes("//a/@href");//$NON-NLS-1$

    for (Iterator iter = list.iterator(); iter.hasNext();) {
        Attribute attribute = (Attribute) iter.next();
        String url = attribute.getValue();
        urls.add(url);/*  w w w  .  j a v  a2s. c  om*/
    }

    return urls;
}

From source file:com.arc.cdt.debug.seecode.options.SeeCodeOptionsAugmenter.java

License:Open Source License

/**
 * Process the "option" element.//from   w w w . j av a 2 s  .  c  o  m
 * 
 * @param e
 *            the "option" element.
 * @throws DocumentException
 * @throws ConfigurationException
 */
private void handleOptionElement(Element e) throws DocumentException, ConfigurationException {
    List<Attribute> attrs = Cast.toType(e.attributes());
    String optionName = null;
    String propertyName = null;
    String namePropertyName = null;
    String seecodeArg = null;
    String negate = null;
    String alternatePropertyName = null;
    String trueValue = null;
    String falseValue = null;
    boolean setAsDefaultOnly = false;
    List<Element> enums = Cast.toType(e.elements());
    for (Attribute a : attrs) {
        String aname = a.getName().toLowerCase();
        String value = a.getValue();
        if (aname.equals("name"))
            optionName = value;
        else if (aname.equals("property"))
            propertyName = value;
        else if (aname.equals("seecode"))
            seecodeArg = value;
        else if (aname.equals("negate"))
            negate = value;
        else if (aname.equalsIgnoreCase("nameProperty"))
            namePropertyName = value;
        else if (aname.equalsIgnoreCase("defaultOnly")) {
            setAsDefaultOnly = !(value.equals("0") || value.equalsIgnoreCase("false"));
        } else if (aname.equalsIgnoreCase("alternate")) {
            alternatePropertyName = value;
        } else if (aname.equalsIgnoreCase("trueValue")) {
            trueValue = value;
        } else if (aname.equalsIgnoreCase("falseValue")) {
            falseValue = value;
        } else
            throw new DocumentException("Unknown attribute name: " + aname);
    }
    if (optionName == null) {
        throw new DocumentException("\"name\" missing in \"option\" node");
    }
    IOption option = lookupOption(optionName);
    if (option == null) {
        throw new ConfigurationException("Unrecognized option name: " + optionName);
    }
    if (seecodeArg == null && enums.size() == 0) {
        seecodeArg = option.getCommand();
        //                    if (seecodeArg==null)
        //                        throw new DocumentException("Option \"" + optionName +
        // "\" needs 'seecode' attribute");
    }
    if (enums.size() == 0 && namePropertyName != null) {
        throw new DocumentException("nameProperty only applies to enumID options");
    }
    boolean first = true;
    for (Element enumElement : enums) {
        if (!enumElement.getName().equalsIgnoreCase("enum")) {
            throw new DocumentException("Unrecognized element \"" + enumElement.getName() + "\" under option \""
                    + optionName + "\"");
        }
        String name = null;
        String scArg = null;
        String propertyValue = null;
        String booleanProperty = null;
        List<Attribute> eattrs = Cast.toType(enumElement.attributes());
        boolean isDefault = first; // assume first is default
        first = false;
        for (Attribute a : eattrs) {
            String aname = a.getName();
            String value = a.getValue();
            if (aname.equals("name")) {
                name = value;
            } else if (aname.equals("seecode"))
                scArg = value;
            else if (aname.equalsIgnoreCase("propertyvalue"))
                propertyValue = value;
            else if (aname.equalsIgnoreCase("property"))
                booleanProperty = value;
            else
                throw new DocumentException("Unknown enumID attribute: " + aname);
        }
        try {
            if (name == null || option.getEnumCommand(name) == null) {
                throw new DocumentException("Unrecognized enumID id: " + name);
            }
            if (scArg == null && propertyValue == null) {
                scArg = option.getEnumCommand(name);
                if (scArg == null)
                    throw new DocumentException("Enum \"" + name + "\" needs 'seecode' attribute");
            }
        } catch (BuildException e1) {
            throw new ConfigurationException(e1.getMessage(), e1);
        }
        OptionEnum oe = new OptionEnum(option, name, scArg, propertyValue, booleanProperty, namePropertyName,
                isDefault);
        mEnumIdToOptionEnumMap.put(name, oe);
        mFromSeeCodeEnumMap.put(scArg, oe);
    }

    SeeCodeOption sco = new SeeCodeOption(optionName, seecodeArg,
            "true".equalsIgnoreCase(negate) || "1".equals(negate), propertyName, alternatePropertyName,
            trueValue, falseValue);
    mOptions.add(sco);
    if (seecodeArg != null) {
        mFromSeeCodeArgMap.put(seecodeArg, sco);
    }
    if (propertyName != null) {
        mFromPropertyMap.put(propertyName, sco);
        if (setAsDefaultOnly)
            this.mSetAsDefaultOnly.add(propertyName);
    }
}

From source file:com.arc.cdt.debug.seecode.options.SeeCodeOptionsAugmenter.java

License:Open Source License

/**
 * Process the "property" element./*from   w w w .  j ava  2 s.c o  m*/
 * @param e
 *            the "property" element.
 * @throws DocumentException
 */
private void handlePropertyElement(Element e) throws DocumentException {
    List<Attribute> attrs = Cast.toType(e.attributes());
    String propertyName = null;
    String propertyValue = null;
    for (Attribute a : attrs) {
        String aname = a.getName().toLowerCase();
        String value = a.getValue();
        if (aname.equals("name")) {
            propertyName = value;
        } else if (aname.equals("value")) {
            propertyValue = value;
        } else
            throw new DocumentException("Unknown attribute under 'property': " + aname);
    }
    if (propertyName == null || propertyValue == null)
        throw new DocumentException("'name' or 'value' attributes missing for element 'property'");
    mSetPropertyMap.put(propertyName, propertyValue);
}

From source file:com.arc.cdt.debug.seecode.options.SeeCodeOptionsAugmenter.java

License:Open Source License

private void handleDefaultElement(Element e) throws DocumentException {
    @SuppressWarnings("unchecked")
    List<Attribute> attrs = e.attributes();
    String property = null;//from  w  w w  .j  av a2  s . c o  m
    String defaultValue = null;
    String option = null;
    String optionValue = null;
    for (Attribute a : attrs) {
        String aname = a.getName().toLowerCase();
        String value = a.getValue();
        if (aname.equals("property")) {
            property = value;
        } else if (aname.equals("value")) {
            defaultValue = value;
        } else if (aname.equals("option")) {
            option = value;
        } else if (aname.equals("optionvalue")) {
            optionValue = value;
        }
    }
    if (property == null || defaultValue == null || option == null || optionValue == null) {
        throw new DocumentException("Attributes on Default element missing");
    }
    mDefaultList.add(new DefaultSpec(property, defaultValue, option, optionValue));
}

From source file:com.arc.xml.AbstractBuilder.java

License:Open Source License

/**
 * Check that attributes of element are valid and that all required attributes are there Apply attributes that are
 * valid to the builder via reflection by calling <code>"set<i>Property</i>"</code>.
 *///from  w w  w  .j  a  v a2  s .  c  o m
protected void doAttributes(Element e, IBinding binding) throws SAXException {
    List<Attribute> attrs = Cast.toType(e.attributes());
    for (Attribute a : attrs) {
        IAttributeDef adef = binding.getAttribute(a.getName());
        if (adef == null)
            unknownAttribute(e, binding, a);
        else
            try {
                setAttribute(e, a.getName(), adef, a.getValue());
            } catch (NoSuchMethodException x) {
                error(e, "Can't set attribute " + adef.getName());
            }
    }
    /*
     * Look for missing required attributes
     */
    for (IAttributeDef a : binding.getAttributes().values()) {
        if (a.isRequired() && e.attributeValue(a.getName()) == null && !accessedByAlias(e, binding, a))
            error(e, "Required attribute \"" + a.getName() + "\" for tag \"" + binding.getTagName()
                    + "\" is missing");
    }
}