Example usage for org.dom4j Element elements

List of usage examples for org.dom4j Element elements

Introduction

In this page you can find the example usage for org.dom4j Element elements.

Prototype

List<Element> elements();

Source Link

Document

Returns the elements contained in this element.

Usage

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

License:Open Source License

/**
 * <pre>/*  w  w  w  . j  a  v a2  s. c  om*/
 * 
 *  
 *   
 *    
 *         options
 *             option name=&quot;...&quot; property=&quot;...&quot; [seecode=&quot;...&quot;] [negate=&quot;true&quot;]
 *                 [enumID name=&quot;...&quot; seecode=&quot;...&quot; propertyValue=&quot;...&quot;]
 *                 ...
 *             ...
 *                 
 *     
 *  
 * </pre>
 * 
 * @param root
 *            the XML root element that this object represents.
 * @param config
 *            the build configuration from which to extract options.
 * @throws ConfigurationException
 *             if unknown compiler options referenced, etc.
 * @throws DocumentException
 *             if bad XML.
 */
SeeCodeOptionsAugmenter(Element root, IConfiguration config) throws ConfigurationException, DocumentException {
    if (!root.getName().equalsIgnoreCase("options")) {
        throw new DocumentException("Unrecognized root node " + root.getName());
    }
    mConfig = config;

    List<Element> elements = Cast.toType(root.elements());
    for (Element e : elements) {
        if (e.getName().equalsIgnoreCase("option")) {
            handleOptionElement(e);
        } else if (e.getName().equalsIgnoreCase("property")) {
            handlePropertyElement(e);
        } else if (e.getName().equalsIgnoreCase("default")) {
            handleDefaultElement(e);
        } else if (e.getName().equalsIgnoreCase("setif")) {
            handleSetIf(e);
        } else
            throw new DocumentException("Unknown element: " + e.getName());
    }
}

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

License:Open Source License

private void handleSetIf(Element e) {
    // <setIf property=...  value=...> conditions </setIf>
    String property = e.attributeValue("property");
    String value = e.attributeValue("value");
    if (property == null || property.length() == 0)
        throw new IllegalStateException("Missing property in setIf");
    if (value == null)
        throw new IllegalStateException("Value is null in setIf");
    @SuppressWarnings("unchecked")
    List<Element> kids = e.elements();
    if (kids.size() != 1) {
        throw new IllegalStateException("setIf must have one and only one child element");
    }/*from w w w  . j  a v a 2s. com*/
    Condition cond = Condition.parse(kids.get(0));
    mSetIfList.add(new SetIf(property, value, cond));
}

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

License:Open Source License

/**
 * Process the "option" element.//  w w w .  j a v  a2 s . c  om
 * 
 * @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.seecode.internal.display.SeeCodeTextViewerFactory.java

License:Open Source License

private void addUserGUI(final int ugui_number, String specification, MenuDescriptor menu,
        final ISeeCodeTextViewerCallback callback, IValueSender sender, ICheckBoxRecorder cbrecorder) {
    // Add user GUI. This is expressed via the XML-like language
    // used by the options processor.
    // Globals.SOP("Processing user GUI specification "+specification);
    Element root = null;
    try {//from  ww w  .  java2s  .c  o  m
        root = com.metaware.guihili.GuihiliParser.parseString(specification).getRootElement();
        List<Element> elements = Cast.toType(root.elements());
        if (elements.size() == 1)
            root = elements.get(0);
    } catch (Exception x) {
        callback.internalError(null, "Guihili processing error for " + specification, x);
        return;
    }
    if (root == null) {
        callback.notifyError(null, "Invalid GUI specification: " + specification, "Configuration error");
        return;
    }
    new UserGuiWalker(sender, ugui_number, cbrecorder, fDisplayCreator).walk(root, menu);
}

From source file:com.arc.seecode.internal.display.UserGuiWalker.java

License:Open Source License

private void doKids(Element parent, MenuDescriptor md) {
    // Process the kids of this tree and return a vector of formed
    // components. Recursively erase any sequences that appear below
    // so we get just the flattened tree.
    List<Element> kids = Cast.toType(parent.elements());
    for (Element kid : kids) {
        walk(kid, md);// w  w  w .  j av  a  2  s  .  co m
    }
}

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

License:Open Source License

/**
 * Process an element.//from  ww w.jav  a2 s.  co  m
 * @param e the element
 * @param binding the binding definition for the element's parent.
 * @return the constructed object, which is also the value of the data property of the element.
 */
@Override
public Object build(Element e, IBinding binding, IBuilder parentBuilder) throws SAXException {
    startNewInstance(e);
    doAttributes(e, binding);
    try {
        beginChildren(e);
        List<Element> kids = Cast.toType(e.elements());
        for (Element kid : kids) {
            doChild(kid, binding);
        }
        // Here we decorate the element with the constructed object.
        Object data = null;
        try {
            data = returnObject();
            e.setData(data);
        } catch (SAXParseException x) {
            throw x;
        } catch (SAXException x) {
            Exception ex = x;
            if (x.getException() != null)
                ex = x.getException();
            error(e, ex.getMessage(), ex);
        }
        return data;
    } finally {
        cleanup(); // if any cleanup to do.
    }
}

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

License:Open Source License

private void doBindingKids(Binding binding, Element element) throws BadXmlException {
    List<Element> list = Cast.toType(element.elements());
    for (Element kid : list) {
        String tag = kid.getName();
        if (tag.equals("attr") || tag.equals("attribute")) {
            AttributeDef attr = defineAttribute(binding, kid);
            if (attr != null) // null if error
                binding.addAttribute(attr);
        } else if (tag.equals("binding")) {
            Binding b = createBinding(binding, kid);
            binding.addBinding(b);//  ww w  . ja v  a2  s  .c  om
        } else
            metaError(kid, "unrecognized child of <binding> node");
    }
}

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

License:Open Source License

/**
 * Handle an attribute specification.//from  w  w  w.j  a v a 2  s . co m
 * 
 * <pre>
 * 
 *  &lt;attr name=&quot;...&quot; type=&quot;...&quot; &gt;/&gt;
 *  
 * </pre>
 * 
 * <p>
 * An attribute may correspond to an object, in which case it will have a single binding node underneath.
 * <p>
 * 
 * <pre>
 * 
 *  &lt;attr name=&quot;...&quot; type=&quot;...&quot; &gt;
 *     &lt;binding ... /&gt;
 *  &lt;/attr&gt;
 *  
 * </pre>
 */
AttributeDef defineAttribute(Binding owner, Element element) throws BadXmlException {
    String name = getAttribute(element, "name", true);
    String typeName = getAttribute(element, "type", false);
    String required = getAttribute(element, "required", false);
    String aggregate = getAttribute(element, "aggregate", false);
    String alias = getAttribute(element, "alias", false);
    String deferred = getAttribute(element, "delay", false);
    if (name == null)
        return null;
    boolean isRequired = required != null && required.equalsIgnoreCase("true");
    boolean delayEval = deferred != null && deferred.equalsIgnoreCase("true");
    List<Element> list = Cast.toType(element.elements());
    int kidCnt = list.size();
    int type = kidCnt > 0 ? IAttributeDef.OBJECT : IAttributeDef.STRING;
    if (kidCnt > 0 && aggregate != null)
        metaError(element, "\"aggregate\" attribute applies to object attributes only");

    if (typeName != null) {
        typeName = typeName.toLowerCase();
        if (kidCnt > 0)
            metaError(element, "Superfluous attribute type: " + typeName);
        else if (typeName.equals("int"))
            type = IAttributeDef.INT;
        else if (typeName.equals("string"))
            type = IAttributeDef.STRING;
        else if (typeName.startsWith("bool"))
            type = IAttributeDef.BOOLEAN;
        else if (typeName.startsWith("float"))
            type = IAttributeDef.FLOAT;
        else if (typeName.startsWith("action"))
            type = IAttributeDef.ACTION;
        else if (typeName.startsWith("list"))
            type = IAttributeDef.LIST;
        else
            metaError(element, "Bad attribute type: " + typeName);
    }
    /*
     * If the name starts with "*", then it is to be considered a property of the Builder class.
     */
    boolean isProperty = true;
    if (name.charAt(0) == '*') {
        name = name.substring(1);
        isProperty = false;
    }
    if (kidCnt > 0) {
        Element kid = list.get(0);
        if (kid.getName().equals("binding")) {
            Binding binding = createBinding(owner, kid);
            return new AttributeDef(name, binding, isProperty, isRequired,
                    aggregate != null && aggregate.equalsIgnoreCase("true"));
        }
        metaError(kid, "Unrecognized child of attribute " + name);
    }
    AttributeDef ap = new AttributeDef(name, type, isProperty, isRequired, delayEval);
    if (alias != null)
        ap.setAlias(alias);
    return ap;
}

From source file:com.baidu.terminator.plugin.extractor.http.HttpXmlExtractor.java

License:Open Source License

/**
 * ??/*from  ww  w.jav a  2s .co  m*/
 * 
 * @param element
 */
@SuppressWarnings("unchecked")
public void getElementList(Element element) {
    List<Element> elements = element.elements();
    if (elements.size() == 0) {
        // ?
        String key = element.getName();
        String value = element.getTextTrim();
        RequestElement bodyElement = new RequestElement();
        bodyElement.setKey(key);
        bodyElement.setValue(value);
        contentmap.add(bodyElement);
    } else {
        // ?
        for (Iterator<Element> it = elements.iterator(); it.hasNext();) {
            Element elem = (Element) it.next();
            // ??
            getElementList(elem);
        }
    }
}

From source file:com.bay12games.df.rawedit.xml.RawsDescriptionLoader.java

License:Open Source License

/**
 * Load and parse specified raws definition file. If elements is null, only
 * the content of the current file will be returned. If elements is not null
 * and contains not-null token and container maps, the content of current
 * file will be appended as if the definitions were in one file.<br><br>
 *
 * The instances of the underlying containers are preserved.<br><br>
 *
 * <p><strong>Note:</strong> There should be only one global access to the token/container
 * definitions in the application.<br>
 *
 * @param file The file to load and parse
 * @param elements The tuple-container for token, container and id elements
 * @return New instance of ElementContainer with maps of tokens and containers if the
 * supplied ElementContainer was null, or the same instance with updated content.
 * The supplied instance (possibly null) is returned on error
 *///from w  ww  .j  a v  a 2 s.com
public ElementContainer parse(File file, ElementContainer elements) {
    Document d;
    try {
        SAXReader reader = new SAXReader();
        d = reader.read(file);
    } catch (DocumentException ex) {
        log.error("Unable to load file:" + file + '.', ex);
        return elements;
    }

    if (elements == null) {
        containers = new HashMap<String, Container>();
        tokens = new HashMap<String, Token>();
        ids = new HashMap<String, Id>();
        elements = new ElementContainer(containers, tokens, ids);
    } else {
        if (elements.getContainers() != null) {
            containers = elements.getContainers();
        } else {
            containers = new HashMap<String, Container>();
        }

        if (elements.getTokens() != null) {
            tokens = elements.getTokens();
        } else {
            tokens = new HashMap<String, Token>();
        }

        if (elements.getIds() != null) {
            ids = elements.getIds();
        } else {
            ids = new HashMap<String, Id>();
        }
    }

    Element root = d.getRootElement();
    for (Element e : root.elements()) {
        if ("c".equals(e.getName())) {
            parseContainer(e);
        } else if ("t".equals(e.getName())) {
            parseToken(e);
        } else if ("id".equals(e.getName())) {
            parseId(e);
        }
    }

    return elements;//new ElementContainer(containers, tokens, ids);
}