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:edu.utep.cs.jasg.specificationGenerator.documentGenerator.ASTDocumentFactory.java

License:Open Source License

private String parseASTDefinition(Element astDefinitionElement) {
    StringBuffer astDefinition = new StringBuffer();
    Element astName = astDefinitionElement.getChild("ast_name");
    Element astSuperClass = astDefinitionElement.getChild("ast_superClass");
    Attribute abstractAttribute = astDefinitionElement.getAttribute("abstract");
    Element astChildren = astDefinitionElement.getChild("ast_children");

    //Check if class is abstract
    if (abstractAttribute.equals("true")) {
        astDefinition.append("abstract ");
    }/*from   w ww.j  av a  2s  .com*/

    //Append AST node name
    astDefinition.append(astName.getText());

    //Check if node extends an existing AST node
    //TODO: this is where the tool should check if the parent is valid and 
    //necessary AST behaviors to implement
    if (astSuperClass != null) {
        astDefinition.append(": " + astSuperClass.getText());
    }

    //Parse AST children
    if (astChildren != null) {
        //Get set of existing AST children elements 
        List<Element> astChildrenList = astChildren.getChildren();
        Iterator<Element> astChildrenIterator = astChildrenList.iterator();

        //Append children operator
        if (astChildrenList.size() > 0)
            astDefinition.append(" ::= ");

        //Add AST children to definition
        while (astChildrenIterator.hasNext()) {
            astDefinition.append(astChildrenIterator.next().getText());
            if (astChildrenIterator.hasNext())
                astDefinition.append(" ");
        }
    }
    astDefinition.append(";\n");
    return astDefinition.toString();
}

From source file:engine.Engine.java

License:Open Source License

public void loadLevel(String filePath) throws JDOMException, IOException {
    SAXBuilder sax = new SAXBuilder();
    Document doc;//from   w  w  w  .j  ava  2s.c  o m

    doc = sax.build(new File(filePath));
    Element root = doc.getRootElement();
    List<Element> listElem = root.getChildren();

    for (Element elem : listElem) {
        Entity entity = null;
        switch (elem.getName()) {
        case "BreakableBlock":
            entity = new BreakableBlock(this);
            break;
        case "SolidBlock":
            entity = new SolidBlock(this);
            break;
        case "Bomb":
            entity = new Bomb(this);
            break;
        case "Fire":
            entity = new Fire(this);
            break;
        case "Bomberman":
            entity = new Bomberman(this);
            break;
        case "Bonus":
            Bonus bonus = new Bonus(this);
            bonus.setBomb(elem.getAttribute("bomb").getIntValue());
            bonus.setPower(elem.getAttribute("power").getIntValue());
            bonus.setSpeed(elem.getAttribute("speed").getIntValue());

            entity = bonus;
            break;
        case "SpeedBonus":
            entity = new SpeedBonus(this);
            break;
        case "PowerBonus":
            entity = new PowerBonus(this);
            break;
        case "BombBonus":
            entity = new BombBonus(this);
            break;
        case "KickBonus":
            entity = new KickBonus(this);
            break;
        case "FutureBonus":
            futureBonus(elem);
            continue;
        default:
            this.log.warn("loadLevel: unknown type object -> " + elem.getName());
            continue;
        }

        Vector2f position = new Vector2f();
        position.x = elem.getAttribute("x").getIntValue() * 1000;
        position.y = elem.getAttribute("y").getIntValue() * 1000;
        entity.setPosition(position);

        entity.setDirection(elem.getAttribute("dir").getIntValue());

        addEntity(entity);
    }

    this.collisionManager.addHandler(new BombermanBlockCH(this.collisionManager));
    this.collisionManager.addHandler(new BombermanBonusCH());
    this.collisionManager.addHandler(new BombermanFireCH());
    this.collisionManager.addHandler(new BombFireCH());
    this.collisionManager.addHandler(new BonusFireCH());
    this.collisionManager.addHandler(new BlockFireCH());
    this.collisionManager.addHandler(new BombBlockCH());
    BombermanBombCH han = new BombermanBombCH(this.collisionManager);
    addListener(han);
    this.collisionManager.addHandler(han);

    this.loaded = true;
}

From source file:engine.Engine.java

License:Open Source License

private void futureBonus(Element elem) {
    try {//from  w ww .  jav a 2s .c  o m
        int nbSpeed = elem.getAttribute("nbSpeed").getIntValue();
        int nbBomb = elem.getAttribute("nbBomb").getIntValue();
        int nbPower = elem.getAttribute("nbPower").getIntValue();
        int nbKick = elem.getAttribute("nbKick").getIntValue();

        for (int i = 0; i < nbSpeed; i++)
            this.futureBonus.add(new SpeedBonus(this));
        for (int i = 0; i < nbBomb; i++)
            this.futureBonus.add(new BombBonus(this));
        for (int i = 0; i < nbPower; i++)
            this.futureBonus.add(new PowerBonus(this));
        for (int i = 0; i < nbKick; i++)
            this.futureBonus.add(new KickBonus(this));

    } catch (DataConversionException e) {
        this.log.error("wrong format of attribute nbSpeed, nbBomb, nbPower ou nbKick");
    }

}

From source file:es.ucm.fdi.clover.gui.CloverSave.java

License:Open Source License

/**
 * Restores the CloverSave to a different XML file; totally changes the
 * 'views' (it is basically recreated) output array;
 *//*from   w  ww .j  av  a  2 s. co  m*/
public static ArrayList<ClusterView> restore(BaseGraph base, ClusterViewFactory vf, File f) throws IOException {

    if (vf == null) {
        vf = new DefaultClusterViewFactory();
    }

    ArrayList<ClusterView> views = new ArrayList<ClusterView>();
    HashMap<String, Filter> filters = new HashMap<String, Filter>();
    HashMap<String, ClusterHierarchy> hierarchies = new HashMap<String, ClusterHierarchy>();

    SAXBuilder builder = new SAXBuilder();
    ClassLoader loader = base.getClass().getClassLoader();

    try {
        Document doc = builder.build(f.getAbsolutePath());
        Element root = doc.getRootElement();

        Element sharedElems = (Element) root.getChildren().get(0);

        for (Element e : (List<Element>) sharedElems.getChildren()) {
            if (e.getName().equals("filter")) {
                String className = e.getAttributeValue("filterClass");
                Filter filter = (Filter) loader.loadClass(className).newInstance();
                filter.restore(e);
                filters.put(e.getAttributeValue("id"), filter);
            } else if (e.getName().equals("hierarchy")) {
                Element cluster = (Element) e.getChildren().get(0);

                Element clusterer = (Element) e.getChildren().get(1);
                String className = clusterer.getAttributeValue("engineClass");
                ClusteringEngine engine = (ClusteringEngine) loader.loadClass(className).newInstance();
                engine.restore(clusterer);

                BaseGraph hb = base;
                if (e.getAttribute("filterId") != null) {
                    Filter filter = filters.get(e.getAttributeValue("filterId"));
                    hb = new FilteredGraph(base, filter);
                }

                ClusterHierarchy h = new ClusterHierarchy(e, hb, engine);
                hierarchies.put(e.getAttributeValue("id"), h);
            }
        }

        for (Element e : (List<Element>) root.getChildren()) {
            if (!e.getName().equals("view"))
                continue;

            // build view
            String hid = e.getAttributeValue("hierarchyId");
            ClusterView view = vf.createClusterView(hierarchies.get(hid), e);
            view.restore(e);

            Element layoutCache = (Element) e.getChildren().get(0);
            view.getAnimator().getLayoutCache().restore(layoutCache, view.getBase());

            Element animatorProps = (Element) e.getChildren().get(1);
            view.getAnimator().restore(animatorProps);

            views.add(view);
        }
    }

    // indicates a well-formedness error
    catch (JDOMException jde) {
        log.warn("Document is not well-formed XML");
        jde.printStackTrace();
    } catch (IOException ioe) {
        System.out.println(ioe);
        ioe.printStackTrace();
    } catch (Exception e) {
        System.out.println(e);
        e.printStackTrace();
    }

    return views;
}

From source file:es.ucm.fdi.clover.model.ClusterHierarchy.java

License:Open Source License

/**
 * Restores the current hierarchy from an XML representation (the one
 * that is generated by 'save')/*www .  j a v  a 2s .  c  om*/
 */
public void restore(Element e) {
    Element first = (Element) e.getChildren().get(0);

    HashMap<String, Object> map = new HashMap<String, Object>();
    for (Object v : base.vertexSet()) {
        map.put(base.getId(v), v);
    }

    if (first.getName().equals("vertex")) {
        root = new Cluster(base, map.get(first.getAttributeValue("id")));
    } else {
        root = new Cluster();
        if (first.getAttribute("name") != null) {
            root.setName(first.getAttributeValue("name"));
        }
        for (Element child : (List<Element>) first.getChildren()) {
            restore(child, root, map);
        }
    }
}

From source file:es.ucm.fdi.clover.model.ClusterHierarchy.java

License:Open Source License

private void restore(Element e, Cluster parent, HashMap<String, Object> map) {
    if (e.getName().equals("vertex")) {
        Cluster c = new Cluster(base, map.get(e.getAttributeValue("id")));
        parent.add(c);//w w  w .  j a v a2 s . c om
    } else {
        Cluster c = new Cluster();
        if (e.getAttribute("name") != null) {
            c.setName(e.getAttributeValue("name"));
        }
        for (Element child : (List<Element>) e.getChildren()) {
            restore(child, c, map);
        }
        parent.add(c);
    }
}

From source file:eu.himeros.hocr.HocrInfoAggregator.java

License:Open Source License

private void makeCompliantHocr() {
    xpath = XPathFactory.instance().compile("//ns:span[@id|@idx]", Filters.element(), null,
            Namespace.getNamespace("ns", "http://www.w3.org/1999/xhtml"));
    List<Element> elements = xpath.evaluate(root);
    int spanId = 0;
    for (Element span : elements) {
        if (span.getAttribute("idx") != null) {
            try {
                span = span.getChildren().get(0);
            } catch (Exception ex) {
                //
            }/* www .  ja v  a 2  s  .  co  m*/
        }
        LinkedList<Attribute> attributeLl = new LinkedList(span.getParentElement().getAttributes());
        attributeLl.addFirst(new Attribute("id", "w_" + spanId++));
        span.getParentElement().setAttributes(attributeLl);
        String[] suggestions = null;
        String title = span.getAttributeValue("title");
        if (title != null) {
            suggestions = title.split(" ");
        }
        if (suggestions == null) {
            suggestions = new String[] { "" };
        }
        Element ins = new Element("ins", xmlns);
        ins.setAttribute("class", "alt");
        ins.setAttribute("title", makeNlp(span.getAttributeValue("class")));
        ins.setText(span.getText());
        span.removeContent();
        span.addContent(ins);
        span.setAttribute("class", "alternatives");
        span.removeAttribute("uc");
        span.removeAttribute("occ");
        span.removeAttribute("title");
        span.removeAttribute("anchor");
        span.removeAttribute("anchor-id");
        span.removeAttribute("id");
        span.getParentElement().removeAttribute("idx");
        span.removeAttribute("whole");
        span.getParentElement().removeAttribute("whole");
        if (title == null || "".equals(title)) {
            continue;
        }
        double score = 0.90;
        for (String suggestion : suggestions) {
            if (suggestion == null || "".equals(suggestion)) {
                continue;
            }
            Element del = new Element("del", xmlns);
            del.setAttribute("title", "nlp " + String.format("%.2f", score).replaceAll(",", "."));
            score = score - 0.01;
            suggestion = suggestion.replaceAll(l1PunctMarkFilter, "");
            Matcher leftMatcher = l1LeftPunctMarkPattern.matcher(ins.getText());
            if (leftMatcher.matches()) {
                suggestion = leftMatcher.group(1) + suggestion;
            }
            Matcher rightMatcher = l1RightPunctMarkPattern.matcher(ins.getText());
            if (rightMatcher.matches()) {
                String ngtSymbol = "";
                if (suggestion.endsWith("\u261a")) {
                    ngtSymbol = "\u261a";
                    suggestion = suggestion.substring(0, suggestion.length() - 1);
                }
                suggestion = suggestion + rightMatcher.group(1) + ngtSymbol;
            }
            ///!!!!
            if (suggestion.endsWith("\u261a") && ins.getParentElement().getParentElement()
                    .getAttributeValue("lang", Namespace.XML_NAMESPACE) != null) {
                String buff = suggestion.substring(0, suggestion.length() - 1);
                sa.align(buff, ins.getText());
                double sim = 1 - sa.getEditDistance()
                        / Math.max((double) buff.length(), (double) ins.getText().length());
                if (sim > 0.6) {

                    suggestion = ins.getText() + "\u261b";
                    ins.setText(buff);
                    ins.setAttribute("title", "nlp 0.70");
                }
            }
            del.addContent(suggestion);
            span.addContent(del);
        }
    }
}

From source file:fourmiz.engine.Engine.java

License:Open Source License

/**
 * Parse entity part of xml//from  w  ww .  jav a2  s .com
 * @param elems
 * @throws DataConversionException
 */
private void loadEntity(Collection<Element> elems) throws DataConversionException {
    for (Element elem : elems) {
        Entity entity = null;
        switch (elem.getName()) {
        case "Anthill":
            entity = EntityFactory.createEntity(EntityName.Anthill, this);
            break;
        case "Egg":
            entity = EntityFactory.createEntity(EntityName.Egg, this);
            break;
        case "Larva":
            entity = EntityFactory.createEntity(EntityName.Larva, this);
            break;
        case "Nymph":
            entity = EntityFactory.createEntity(EntityName.Nymph, this);
            break;
        case "FourmizWorker":
            entity = EntityFactory.createEntity(EntityName.FourmizWorker, this);
            break;
        case "FourmizSoldier":
            entity = EntityFactory.createEntity(EntityName.FourmizSoldier, this);
            break;
        case "FourmizSex":
            entity = EntityFactory.createEntity(EntityName.FourmizSex, this);
            break;
        case "Queen":
            entity = EntityFactory.createEntity(EntityName.Queen, this);
            break;
        case "Prey":
            entity = EntityFactory.createEntity(EntityName.Prey, this);
            break;
        case "PopPrey":
            entity = EntityFactory.createEntity(EntityName.PopPrey, this);
            break;
        default:
            log.warn("loadLevel: unknown type entity -> " + elem.getName());
            continue;
        }

        Vector2f position = new Vector2f();
        position.x = elem.getAttribute("x").getIntValue() * SIZE_CASE;
        position.y = elem.getAttribute("y").getIntValue() * SIZE_CASE;
        entity.setPosition(position);

        entity.setDirection(elem.getAttribute("dir").getIntValue());

        addEntity(entity);
    }
}

From source file:fr.paris.lutece.plugins.dila.service.impl.DilaBatchXMLService.java

License:Open Source License

@Override
public XmlDTO buildDocument(File file, AudienceCategoryEnum typeXML) throws JDOMException, IOException {
    XmlDTO dilaXml = new XmlDTO();
    dilaXml.setIdAudience(typeXML.getId());

    // Convert file to document object
    Document document = _saxBuilder.build(file);

    // Get root// w w w .  java2 s . c  om
    Element root = document.getRootElement();

    // Get attribute ID
    Attribute rootAttrId = root.getAttribute(ATTRIBUTE_ID);

    if (rootAttrId != null) {
        String strId = rootAttrId.getValue();
        dilaXml.setIdXml(strId);
    }

    // Get title
    Element title = root.getChild(CHILD_TITLE, NAMESPACE_DC);

    if (title != null) {
        String strTitle = title.getTextTrim();
        dilaXml.setTitle(strTitle);
    }

    // Get type
    Element type = root.getChild(CHILD_TYPE, NAMESPACE_DC);

    if (type != null) {
        if (file.getName().startsWith("R")) {
            dilaXml.setResourceType(RESSOURCE_TYPE);
        } else {
            String strType = type.getTextTrim();
            dilaXml.setResourceType(strType);
        }
    }

    // Get breadcrumb
    Element rootBreadcrumb = root.getChild(CHILD_BREADCRUMB);

    if (rootBreadcrumb != null) {
        List<Element> listBreadcrumb = rootBreadcrumb.getChildren();

        if (CollectionUtils.isNotEmpty(listBreadcrumb)) {
            List<String> listBreadcrumbId = new ArrayList<String>(listBreadcrumb.size());

            for (Element elementBreadcrumb : listBreadcrumb) {
                listBreadcrumbId.add(elementBreadcrumb.getAttributeValue(ATTRIBUTE_ID));
            }

            String strBreadcrumb = StringUtils.join(listBreadcrumbId, ";");
            dilaXml.setBreadCrumb(strBreadcrumb);
        }
    }

    return dilaXml;
}

From source file:insa.h4401.utils.PlanningDocument.java

/**
 * Returns the Node matching with the warehouse described in the XML file.
 *
 * @param map map where there is the warehouse
 * @return the warehouse/* w w  w  .ja v  a2 s. co  m*/
 */
public Node getWarehouse(Map map) {
    // Retreive warehouse element 
    Element warehouse = getDom().getRootElement().getChild("Entrepot");
    Node node = null;
    try {
        // Retreive the Id of the node
        int nodeId = warehouse.getAttribute("adresse").getIntValue();
        // Return the node which is associated with the id
        node = map.getNodeById(nodeId);
    } catch (DataConversionException ex) {
        Logger.getLogger(PlanningDocument.class.getName()).log(Level.SEVERE, null, ex);
    }
    return node;
}