Example usage for org.jdom2 Element indexOf

List of usage examples for org.jdom2 Element indexOf

Introduction

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

Prototype

@Override
    public int indexOf(final Content child) 

Source Link

Usage

From source file:org.mycore.frontend.editor.MCREditorDefReader.java

License:Open Source License

/**
 * Recursively resolves references by the @ref attribute and
 * replaces them with the referenced component.
 *//*from  w w w . j a  va  2 s  .  co  m*/
private void resolveReferences() {
    for (Iterator<Element> it = referencing2ref.keySet().iterator(); it.hasNext();) {
        Element referencing = it.next();
        String id = referencing2ref.get(referencing);
        LOGGER.debug("Resolving reference to " + id);

        Element found = id2component.get(id);
        if (found == null) {
            String msg = "Reference to component " + id + " could not be resolved";
            throw new MCRConfigurationException(msg);
        }

        String name = referencing.getName();
        referencing.removeAttribute("ref");
        it.remove();

        if (name.equals("cell") || name.equals("repeater")) {
            if (found.getParentElement().getName().equals("components")) {
                referencing.addContent(0, found.detach());
            } else {
                referencing.addContent(0, (Element) found.clone());
            }
        } else if (name.equals("panel")) {
            if (referencing2ref.containsValue(id)) {
                referencing.addContent(0, found.cloneContent());
            } else {
                found.detach();
                List<Content> content = found.getContent();
                for (int i = 0; !content.isEmpty(); i++) {
                    Content child = content.remove(0);
                    referencing.addContent(i, child);
                }
            }
        } else if (name.equals("include")) {
            Element parent = referencing.getParentElement();
            int pos = parent.indexOf(referencing);
            referencing.detach();

            if (referencing2ref.containsValue(id)) {
                parent.addContent(pos, found.cloneContent());
            } else {
                found.detach();
                List<Content> content = found.getContent();
                for (int i = pos; !content.isEmpty(); i++) {
                    Content child = content.remove(0);
                    parent.addContent(i, child);
                }
            }
        }
    }

    Element components = editor.getChild("components");
    String root = components.getAttributeValue("root");

    for (int i = 0; i < components.getContentSize(); i++) {
        Content child = components.getContent(i);
        if (!(child instanceof Element)) {
            continue;
        }
        if (((Element) child).getName().equals("headline")) {
            continue;
        }
        if (!root.equals(((Element) child).getAttributeValue("id"))) {
            components.removeContent(i--);
        }
    }
}

From source file:org.mycore.frontend.xeditor.MCRBinding.java

License:Open Source License

public Element cloneBoundElement(int index) {
    Element template = (Element) (boundNodes.get(index));
    Element newElement = template.clone();
    Element parent = template.getParentElement();
    int indexInParent = parent.indexOf(template) + 1;
    parent.addContent(indexInParent, newElement);
    boundNodes.add(index + 1, newElement);
    trackNodeCreated(newElement);/*from   w  ww  .  j a va 2s.  co  m*/
    return newElement;
}

From source file:org.xflatdb.xflat.query.XPathUpdate.java

License:Apache License

/**
 * Applies the update operations to the given DOM Element representing
 * the data in a selected row.//from w  ww  . j a va 2 s.  c o m
 * @param rowData The DOM Element representing the data in a selected row.
 * @return true if any updates were applied.
 */
public int apply(Element rowData) {
    int updateCount = 0;

    for (Update update : this.updates) {
        //the update's value will be one or the other, don't know which
        Content asContent = null;
        String asString = null;

        if (update.value instanceof String) {
            asString = (String) update.value;
        }

        if (update.value instanceof Content) {
            asContent = (Content) update.value;
        }

        for (Object node : update.path.evaluate(rowData)) {
            if (node == null)
                continue;

            Parent parent;
            Element parentElement;

            if (update.getUpdateType() == UpdateType.UNSET) {
                if (node instanceof Attribute) {
                    parentElement = ((Attribute) node).getParent();
                    if (parentElement != null) {
                        parentElement.removeAttribute((Attribute) node);
                        updateCount++;
                    }
                } else if (node instanceof Content) {
                    parent = ((Content) node).getParent();
                    //remove this node from its parent element
                    if (parent != null) {
                        parent.removeContent((Content) node);
                        updateCount++;
                    }
                }

                continue;
            }

            //it's a set

            if (node instanceof Attribute) {
                //for attributes we set the value to empty string
                //this way it can still be selected by xpath for future updates
                if (update.value == null) {
                    ((Attribute) node).setValue("");
                    updateCount++;
                } else {
                    if (asString == null) {
                        asString = getStringValue(update.value);
                    }

                    //if we fail conversion then do nothing.
                    if (asString != null) {
                        ((Attribute) node).setValue(asString);
                        updateCount++;
                    }
                }

                continue;
            } else if (!(node instanceof Content)) {
                //can't do anything
                continue;
            }

            Content contentNode = (Content) node;

            //need to convert
            if (update.value != null && asContent == null) {
                asContent = getContentValue(update.value);
                if (asContent == null) {
                    //failed conversion, try text
                    asString = getStringValue(update.value);
                    if (asString != null) {
                        //success!
                        asContent = new Text(asString);
                    }
                }
            }

            if (node instanceof Element) {
                //for elements we also set the value, but the value could be Content
                if (update.value == null) {
                    ((Element) node).removeContent();
                    updateCount++;
                } else if (asContent != null) {
                    if (asContent.getParent() != null) {
                        //we used the content before, need to clone it
                        asContent = asContent.clone();
                    }

                    ((Element) node).setContent(asContent);
                    updateCount++;
                }
                continue;
            }

            //at this point the node is Text, CDATA or something else.
            //The strategy now is to replace the value in its parent.
            parentElement = contentNode.getParentElement();
            if (parentElement == null) {
                //can't do anything
                continue;
            }

            if (update.value == null || asContent != null) {
                //replace this content in the parent element
                int index = parentElement.indexOf(contentNode);
                parentElement.removeContent(index);
                if (update.value != null) {
                    //if it was null then act like an unset, otherwise
                    //its a replace

                    if (asContent.getParent() != null) {
                        //we used the content before, need to clone it
                        asContent = asContent.clone();
                    }

                    parentElement.addContent(index, asContent);
                }
                updateCount++;
            }
        }
    }

    return updateCount;
}

From source file:ru.iteco.xmldoc.Config.java

License:Open Source License

/**
 *    ? element.//ww  w  .  j a va  2 s. co  m
 * ? ? ,  null.
 *
 * @param element
 * @return
 */
protected static String getPreviousComment(Element element) {
    //? ?    
    Element parent = element.getParentElement();
    //?  ?  
    int index = parent.indexOf(element);

    // ""   ?,   ?-
    for (int i = index - 1; i > 0; i--) {
        //? 
        Content prev = parent.getContent(i);
        //? ?  ? - ?, , ? /??
        if (prev.getCType().equals(Content.CType.Text))
            continue;
        //? ?  -  ,  ?,  
        else if (prev.getCType().equals(Content.CType.Comment))
            return prev.getValue();
        // ?,  ?  ,   ?  ? ?. .
        else
            return "";
    }
    //   ?    .
    return "";
}

From source file:XMLWriter.WriteCoursesXML.java

License:Open Source License

public static boolean addCode(String idCourse, String idCode, String xmlSource) {
    try {//from w w  w.  ja va  2s.co m
        Document doc = builder.build(xmlSource);
        Element rootNode = doc.getRootElement();
        List<Element> lista = rootNode.getChildren("curso", ns);
        boolean find = false;

        for (Element e : lista)
            if (e.getAttributeValue("id").equals(idCourse)) {
                find = true;
                Element idCo = new Element("idCodigo", ns);
                idCo.setText(idCode);
                e.addContent(e.indexOf(e.getChild("nombre", ns)), idCo);
                break;
            }
        if (!find)
            return false;

        Format form = Format.getPrettyFormat().clone();
        form.setTextMode(Format.TextMode.TRIM_FULL_WHITE);
        XMLOutputter xmlOut = new XMLOutputter(form);
        FileOutputStream file = new FileOutputStream(xmlSource);
        xmlOut.output(doc, file);
        file.close();
    } catch (IOException io) {
        System.out.println(io.getMessage());
    } catch (JDOMException jdomex) {
        System.out.println(jdomex.getMessage());
    }

    return true;
}

From source file:XMLWriter.WriteUsersXML.java

License:Open Source License

public static boolean addCourse(String idUser, String idCourse, String xmlSource) {
    try {/*w  w  w .j a v  a  2 s .  com*/
        Document doc = builder.build(xmlSource);
        Element rootNode = doc.getRootElement();
        boolean find = false;

        for (Element e : rootNode.getChildren("user", ns))
            if (e.getAttributeValue("id").equals(idUser)) {
                find = true;
                Element idCu = new Element("idCurso", ns);
                idCu.setText(idCourse);
                e.setContent(e.indexOf(e.getChild("idCurso", ns)), idCu);
                break;
            }
        if (!find)
            return false;

        Format form = Format.getPrettyFormat().clone();
        form.setTextMode(Format.TextMode.TRIM_FULL_WHITE);
        XMLOutputter xmlOut = new XMLOutputter(form);
        FileOutputStream file = new FileOutputStream(xmlSource);
        xmlOut.output(doc, file);
        file.close();
    } catch (IOException io) {
        System.out.println(io.getMessage());
    } catch (JDOMException jdomex) {
        System.out.println(jdomex.getMessage());
    }

    return true;
}