Example usage for org.dom4j Element getQualifiedName

List of usage examples for org.dom4j Element getQualifiedName

Introduction

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

Prototype

String getQualifiedName();

Source Link

Document

Returns the fully qualified name of this element.

Usage

From source file:com.shopximity.jpt.PageTemplateImpl.java

License:Open Source License

private void processElement(Element element, ContentHandler contentHandler, LexicalHandler lexicalHandler,
        Interpreter beanShell, Stack slotStack) throws SAXException, PageTemplateException, IOException {
    // Get attributes
    Expressions expressions = new Expressions();
    AttributesImpl attributes = getAttributes(element, expressions);

    // Process instructions

    // use macro//from   www  .  j a v a  2 s .co  m
    if (expressions.useMacro != null) {
        processMacro(expressions.useMacro, element, contentHandler, lexicalHandler, beanShell, slotStack);
        return;
    }

    // fill slot
    if (expressions.defineSlot != null) {
        //System.err.println( "fill slot: " + expressions.defineSlot );
        if (!slotStack.isEmpty()) {
            Map slots = (Map) slotStack.pop();
            Slot slot = (Slot) slots.get(expressions.defineSlot);
            //System.err.println( "slot: " + slot );
            if (slot != null) {
                slot.process(contentHandler, lexicalHandler, beanShell, slotStack);
                slotStack.push(slots);
                return;
            }
            // else { use content in macro }
            slotStack.push(slots);
        } else {
            throw new PageTemplateException("slot definition not allowed outside of macro");
        }
    }

    // define
    if (expressions.define != null) {
        processDefine(expressions.define, beanShell);
    }

    // condition
    if (expressions.condition != null && !Expression.evaluateBoolean(expressions.condition, beanShell)) {
        // Skip this element (and children)
        return;
    }

    // repeat
    Loop loop = new Loop(expressions.repeat, beanShell);
    while (loop.repeat(beanShell)) {
        // content or replace
        Object jptContent = null;
        if (expressions.content != null) {
            jptContent = processContent(expressions.content, beanShell);
        }

        // attributes
        if (expressions.attributes != null) {
            processAttributes(attributes, expressions.attributes, beanShell);
        }

        // omit-tag
        boolean jptOmitTag = false;
        if (expressions.omitTag != null) {
            if (expressions.omitTag.equals("")) {
                jptOmitTag = true;
            } else {
                jptOmitTag = Expression.evaluateBoolean(expressions.omitTag, beanShell);
            }
        }

        // Declare element
        Namespace namespace = element.getNamespace();
        if (!jptOmitTag) {
            contentHandler.startElement(namespace.getURI(), element.getName(), element.getQualifiedName(),
                    attributes);
        }

        // Process content
        if (jptContent != null) {
            // Content for this element has been generated dynamically
            if (jptContent instanceof HTMLFragment) {
                HTMLFragment html = (HTMLFragment) jptContent;
                html.toXhtml(contentHandler, lexicalHandler);
            }

            // plain text
            else {
                char[] text = ((String) jptContent).toCharArray();
                contentHandler.characters(text, 0, text.length);
            }
        } else {
            defaultContent(element, contentHandler, lexicalHandler, beanShell, slotStack);
        }

        // End element
        if (!jptOmitTag) {
            contentHandler.endElement(namespace.getURI(), element.getName(), element.getQualifiedName());
        }
    }
}

From source file:com.webslingerz.jpt.PageTemplateImpl.java

License:Open Source License

private void processElement(Element element, ContentHandler contentHandler, LexicalHandler lexicalHandler,
        Interpreter beanShell, Stack<Map<String, Slot>> slotStack)
        throws SAXException, PageTemplateException, IOException {
    // Get attributes
    Expressions expressions = new Expressions();
    AttributesImpl attributes = getAttributes(element, expressions);

    // Skip macro definitions
    if (expressions.macro) {
        return;/*  w w  w  . j  a v a2  s .  c  o  m*/
    }

    // Process instructions
    // 1. define
    if (expressions.define != null) {
        processDefine(expressions.define, beanShell);
    }

    // 2. condition
    if (expressions.condition != null && !Expression.evaluateBoolean(expressions.condition, beanShell)) {
        // Skip this element (and children)
        return;
    }

    // 3. repeat
    Loop loop = new Loop(expressions.repeat, beanShell);
    while (loop.repeat(beanShell)) {
        // expand macro
        if (expressions.useMacro != null) {
            processMacro(expressions.useMacro, element, contentHandler, lexicalHandler, beanShell, slotStack);
            continue;
        }

        // 4. content or replace
        Object jptContent = null;
        if (expressions.content != null) {
            jptContent = processContent(expressions.content, beanShell);
        } else
        // fill slots
        if (expressions.defineSlot != null) {
            // System.err.println( "fill slot: " + expressions.defineSlot );
            if (!slotStack.isEmpty()) {
                Map<String, Slot> slots = slotStack.pop();
                Slot slot = slots.get(expressions.defineSlot);
                // System.err.println( "slot: " + slot );
                if (slot != null) {
                    slot.process(contentHandler, lexicalHandler, beanShell, slotStack);
                    slotStack.push(slots);
                    return;
                }
                // else { use content in macro }
                slotStack.push(slots);
            } else {
                throw new PageTemplateException("slot definition not allowed outside of macro");
            }
        }

        // 5. attributes
        if (expressions.attributes != null) {
            processAttributes(attributes, expressions.attributes, beanShell);
        }

        // 6. omit-tag
        boolean jptOmitTag = false;
        if (expressions.omitTag != null) {
            if (expressions.omitTag.equals("")) {
                jptOmitTag = true;
            } else {
                jptOmitTag = Expression.evaluateBoolean(expressions.omitTag, beanShell);
            }
        }

        // Output

        // Declare element
        Namespace namespace = element.getNamespace();
        if (!jptOmitTag) {
            for (int i = 0; i < attributes.getLength(); i++) {
                String processedValue = Expression.evaluateText(attributes.getValue(i), beanShell);
                attributes.setValue(i, processedValue);
            }
            contentHandler.startElement(namespace.getURI(), element.getName(), element.getQualifiedName(),
                    attributes);
        }

        // Process content
        if (jptContent != null) {
            // Content for this element has been generated dynamically
            if (jptContent instanceof HTMLFragment) {
                HTMLFragment html = (HTMLFragment) jptContent;
                html.toXhtml(contentHandler, lexicalHandler);
            }

            // plain text
            else {
                char[] text = ((String) jptContent).toCharArray();
                contentHandler.characters(text, 0, text.length);
            }
        } else {
            defaultContent(element, contentHandler, lexicalHandler, beanShell, slotStack);
        }

        // End element
        if (!jptOmitTag) {
            contentHandler.endElement(namespace.getURI(), element.getName(), element.getQualifiedName());
        }
    }
}

From source file:edu.umd.cs.findbugs.xml.XPathFind.java

License:Open Source License

public static void main(String[] argv) throws Exception {
    if (argv.length != 2) {
        System.err.println("Usage: " + XPathFind.class.getName() + ": <filename> <xpath expression>");
        System.exit(1);/*from  w  w  w. j a v  a2  s  .  c  o m*/
    }

    String fileName = argv[0];
    String xpath = argv[1];

    SAXReader reader = new SAXReader();
    Document document = reader.read(fileName);

    XPathFind finder = new XPathFind(document) {
        @Override
        protected void match(Node node) {
            // System.out.println(node.toString());
            if (node instanceof Element) {
                Element element = (Element) node;
                System.out.println("Element: " + element.getQualifiedName());
                System.out.println("\tText: " + element.getText());
                System.out.println("\tAttributes:");
                for (Iterator<?> i = element.attributeIterator(); i.hasNext();) {
                    Attribute attribute = (Attribute) i.next();
                    System.out.println("\t\t" + attribute.getName() + "=" + attribute.getValue());
                }
            } else if (node instanceof Attribute) {
                Attribute attribute = (Attribute) node;
                System.out.println("Attribute: " + attribute.getName() + "=" + attribute.getValue());
            }
        }
    };

    finder.find(xpath);
}

From source file:fr.gouv.culture.vitam.utils.XmlDom.java

License:Open Source License

private final static void setNamespace(Element elem, Namespace ns) {
    elem.setQName(QName.get(elem.getName(), ns, elem.getQualifiedName()));
}

From source file:musite.io.xml.ProteinResidueAnnotationReader.java

License:Open Source License

public Map<String, MultiMap<Integer, Map<String, Object>>> read(InputStream is) throws IOException {
    if (is == null)
        return null;

    final Map<String, MultiMap<Integer, Map<String, Object>>> result = new HashMap();

    SAXReader saxReader = new SAXReader();
    BufferedInputStream bis = new BufferedInputStream(is);

    Document document;//from   w ww  .  jav  a 2 s. co m
    try {
        document = saxReader.read(bis);
    } catch (DocumentException e) {
        throw new IOException(e.getMessage());
    }

    Element root = document.getRootElement();

    // iterate through child elements of root
    for (Iterator i = root.elementIterator(); i.hasNext();) {
        Element annTypeElm = (Element) i.next();
        String annType = annTypeElm.getQualifiedName();
        MultiMap<Integer, Map<String, Object>> sites = result.get(annType);
        if (sites == null) {
            sites = new MultiTreeMap();
            result.put(annType, sites);
        }

        Iterator<Element> itSite = annTypeElm.elementIterator(SITE);
        while (itSite.hasNext()) {
            Element siteElm = (Element) itSite.next();
            String pos = siteElm.attributeValue(POSITION);
            if (pos == null) {
                System.err.println("No site info");
                continue;
            }
            int site = Integer.parseInt(pos) - 1;
            Map<String, Object> annotations = new HashMap();
            sites.add(site, annotations);

            Iterator<Element> itAnn = siteElm.elementIterator();
            while (itAnn.hasNext()) {
                Element annElm = (Element) itAnn.next();
                String name = annElm.getQualifiedName();

                Object obj = null;
                if (annElm.hasContent()) {
                    Reader fieldReader = annotationFieldReaders.get(name);
                    if (fieldReader != null) {
                        try {
                            String text = ProteinsXMLReader.nodeContentToString(annElm);
                            InputStream bais = StringUtil.toStream(text);
                            obj = fieldReader.read(bais);
                            bais.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                            continue;
                        }
                    } else {
                        obj = StringEscapeUtils.unescapeXml(annElm.getTextTrim());
                    }
                }

                annotations.put(name, obj);
            }
        }
    }

    return result;
}

From source file:musite.io.xml.ProteinsXMLReader.java

License:Open Source License

public ProteinsXMLReader(Proteins proteins) {
    this.data = proteins;
    nullData = proteins == null;//ww  w. j  a  va  2s  .c o m
    proteinFieldReaders = new HashMap();
    fieldFilter = null;
    saxReaderHandler = new HashMap();
    String path = "/protein-list/protein";
    if (root != null)
        path = "/" + root + path;
    addSaxReaderHandler(path, new ElementHandler() {
        public void onStart(ElementPath path) {
        }

        public void onEnd(ElementPath path) {
            ProteinImpl protein = new ProteinImpl();

            Element elem = path.getCurrent();
            Iterator<Element> itr = elem.elementIterator();
            while (itr.hasNext()) {
                Element field = (Element) itr.next();
                String name = field.getQualifiedName();
                if (fieldFilter != null && fieldFilterInclude != fieldFilter.contains(name))
                    continue;

                Object obj;

                Reader fieldReader = proteinFieldReaders.get(name);
                if (fieldReader != null) {
                    try {
                        String text = nodeContentToString(field);//field.getTextTrim();
                        InputStream bais = StringUtil.toStream(text);
                        obj = fieldReader.read(bais);
                        bais.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                        continue;
                    }
                } else {
                    obj = StringEscapeUtils.unescapeXml(field.getTextTrim());
                }

                protein.putInfo(name, obj);
            }

            //System.out.println(protein.getAccession());

            if (proteinFilter == null || proteinFilter.filter(protein))
                data.addProtein(protein);

            int count = data.proteinCount();
            if (count % 1000 == 0)
                System.out.println(count);

            // prune the tree
            elem.detach();
        }
    });
}

From source file:net.dontdrinkandroot.lastfm.api.CheckImplementationStatus.java

License:Apache License

/**
 * Sets the namespace of the element to the given namespace
 *///from   w  w  w .jav a 2 s.  c o m
public static void setNamespace(final Element elem, final Namespace ns) {

    elem.setQName(QName.get(elem.getName(), ns, elem.getQualifiedName()));
}

From source file:org.adempierelbr.util.WebServiceCep.java

License:Open Source License

/**
 * Faz uma busca a partir do cep enviado, no site 
 * <a href="http://www.republicavirtual.com.br" 
 * target="_blank">republicavirtual.com.br</a>, retornando o resultado em um objeto
 * {@link WebServiceCep}./*w  w w.  ja  va  2  s  .c  om*/
 * <BR>
 * <BR>No se faz necessrio formataes, a string pode ser enviada em qualquer
 * formatao, pois s sero consideradas os primeiros 8 numeros da string.
 * <BR>Por Exemplo:
 * <BR>Uma <tt>{@link String} "14.568-910"</tt>  automaticamente passada para
 * <tt>"14568910"</tt>.
 * <BR>Uma <tt>{@link String} "1%4#5?55%16a8&910"</tt>  automaticamente passada para
 * <tt>"14555168"</tt>, s levando em conta os primeiros 8 nmeros.
 * @param   cep Nmero do cep a ser carregado. S sero considerados os primeiros 8 
 *          nmeros da {@link String} enviada. Todos os caracters no numricos sero
 *          removidos, e a string ser truncada caso seja maior que 8 caracters.
 * @return {@link WebServiceCep} contendo as informaes da pesquisa.
 */
public static WebServiceCep searchCep(String cep) {
    cep = cep.replaceAll("\\D*", ""); //To numeric digits only
    if (cep.length() > 8)
        cep = cep.substring(0, 8);
    WebServiceCep loadCep = new WebServiceCep(cep);
    try {
        XmlEnums enums = new XmlEnums();
        for (Element e : getElements(cep))
            enums.getXml(e.getQualifiedName()).setCep(e.getText(), loadCep);
    } catch (DocumentException ex) {
        if (ex.getNestedException() != null
                && ex.getNestedException() instanceof java.net.UnknownHostException) {
            loadCep.setResultText("Site no encontrado.");
            loadCep.setResulCode(-14);
        } else {
            loadCep.setResultText("No foi possivel ler o documento xml.");
            loadCep.setResulCode(-15);
        }
        loadCep.setExceptio(ex);
    } catch (MalformedURLException ex) {
        loadCep.setExceptio(ex);
        loadCep.setResultText("Erro na formao da url.");
        loadCep.setResulCode(-16);
    } catch (Exception ex) {
        loadCep.setExceptio(ex);
        loadCep.setResultText("Erro inesperado.");
        loadCep.setResulCode(-17);
    }
    return loadCep;
}

From source file:org.apache.archiva.xml.XMLReader.java

License:Apache License

/**
 * Remove namespaces from element recursively.
 *//*from www  .  j  a v a 2s .  co  m*/
@SuppressWarnings("unchecked")
public void removeNamespaces(Element elem) {
    elem.setQName(QName.get(elem.getName(), Namespace.NO_NAMESPACE, elem.getQualifiedName()));

    Node n;

    Iterator<Node> it = elem.elementIterator();
    while (it.hasNext()) {
        n = it.next();

        switch (n.getNodeType()) {
        case Node.ATTRIBUTE_NODE:
            ((Attribute) n).setNamespace(Namespace.NO_NAMESPACE);
            break;
        case Node.ELEMENT_NODE:
            removeNamespaces((Element) n);
            break;
        }
    }
}

From source file:org.craftercms.core.xml.mergers.impl.cues.impl.ElementMergeMatcherImpl.java

License:Open Source License

@Override
public boolean matchForMerge(Element parent, Element child) {
    String parentId = parent.attributeValue(idAttributeName);
    String childId = child.attributeValue(idAttributeName);

    if (parentId == null && childId == null) {
        return parent.getQualifiedName().equals(child.getQualifiedName());
    } else if (parentId != null) {
        return parentId.equals(childId);
    } else {//  www .  j a v a 2s .c o  m
        return false;
    }
}