Example usage for org.dom4j Element attributeIterator

List of usage examples for org.dom4j Element attributeIterator

Introduction

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

Prototype

Iterator<Attribute> attributeIterator();

Source Link

Document

DOCUMENT ME!

Usage

From source file:InitializeDB.java

License:Open Source License

public static void setupDB(int year) {

    try {//from  ww w.j ava2 s  .co m

        //String filename = "nvdcve-2008.xml";

        Connection con = getConnection();

        Statement sql = con.createStatement();

        sql.execute("drop table if exists nvd"); //,primary key(id)

        sql.execute(
                "create table nvd(id varchar(20) not null,soft varchar(160) not null default 'ndefined',rng varchar(100) not null default 'undefined',lose_types varchar(100) not null default 'undefind',severity varchar(20) not null default 'unefined',access varchar(20) not null default 'unefined');");

        SAXReader saxReader = new SAXReader();

        for (int ct = 2002; ct <= year; ct++) {

            //String fname="/transient/mulval/oval/nvd/nvdcve-"+Integer.toString(ct)+".xml";
            String fname = "nvd_old/nvdcve-" + Integer.toString(ct) + ".xml";

            Document document = saxReader.read(fname);

            List entry = document.selectNodes("/*[local-name(.)='nvd']/*[local-name(.)='entry']");

            Iterator ent = entry.iterator();

            int act = 0;

            while (ent.hasNext()) { // varchar(20) not null default 'name',

                Element id = (Element) ent.next();

                String cveid = id.attributeValue("name");

                String cvss = "";

                String access = "";

                //   System.out.println(cveid + access);

                String sev = "";

                String host = "localhost";

                String sftw = "";

                String rge = "";

                String rge_tmp = "";

                String lose_tmp = "";

                String lose_types = "";

                ArrayList<String> subele = new ArrayList<String>();

                ArrayList<String> attr = new ArrayList<String>();

                Iterator ei = id.elementIterator();

                while (ei.hasNext()) { // put all of the subelements'

                    // names(subelement of entry) to the

                    // array list

                    Element sube = (Element) ei.next();

                    subele.add(sube.getName());

                }

                //System.out.println(id.getText());

                Iterator i = id.attributeIterator();

                while (i.hasNext()) { // put the attributes of the entries to

                    // the arraylist

                    Attribute att = (Attribute) i.next();

                    attr.add(att.getName());

                }

                if (subele.contains("vuln_soft")) {

                    Element vs = (Element) id.element("vuln_soft");

                    Iterator itr = vs.elementIterator("prod");

                    while (itr.hasNext()) { // record all of the softwares

                        Element n = (Element) itr.next();

                        //sftw = sftw + n.attributeValue("name") + ",";

                        sftw = n.attributeValue("name");

                        if (sftw.contains("'")) {

                            sftw = sftw.replace("'", "''");

                        }

                        break;

                    }

                    //int lsf = sftw.length();

                    //sftw = sftw.substring(0, lsf - 1);// delete the last comma

                }

                if (attr.contains("severity")) {

                    sev = id.attributeValue("severity");

                }

                if (attr.contains("CVSS_vector")) {

                    cvss = id.attributeValue("CVSS_vector");
                    char ac = cvss.charAt(9);
                    if (ac == 'L')
                        access = "l";
                    else if (ac == 'M')
                        access = "m";
                    else if (ac == 'H')
                        access = "h";
                    else
                        ;

                }

                if (subele.contains("range")) { // to get the range as a array

                    Element vs = (Element) id.element("range");

                    Iterator rgi = vs.elementIterator();

                    while (rgi.hasNext()) { // record all of the softwares

                        Element rg = (Element) rgi.next();

                        if (rg.getName().equals("user_init"))

                            rge_tmp = "user_action_req";

                        else if (rg.getName().equals("local_network"))

                            rge_tmp = "lan";

                        else if (rg.getName().equals("network"))

                            rge_tmp = "remoteExploit";

                        else if (rg.getName().equals("local"))

                            rge_tmp = "local";

                        else

                            rge_tmp = "other";

                        rge = rge + "''" + rge_tmp + "'',";

                    }

                    int lr = rge.length();

                    rge = rge.substring(0, lr - 1);// delete the last comma

                }

                if (subele.contains("loss_types")) {

                    Element lt = (Element) id.element("loss_types");

                    Iterator lti = lt.elementIterator();

                    while (lti.hasNext()) {

                        ArrayList<String> isecat = new ArrayList<String>();

                        Element ls = (Element) lti.next();

                        if (ls.getName().equals("avail"))

                            lose_tmp = "availability_loss";

                        else if (ls.getName().equals("conf"))

                            lose_tmp = "data_loss";

                        else if (ls.getName().equals("int"))

                            lose_tmp = "data_modification";

                        else

                            lose_tmp = "other";

                        lose_types = lose_types + "''" + lose_tmp + "'',";

                    }

                    int ltp = lose_types.length();

                    lose_types = lose_types.substring(0, ltp - 1);// delete the

                    // last

                    // comma

                }

                //System.out.println(cveid + lose_types + rge + sftw + sev + access);

                String insert = "insert nvd values('" + cveid + "','"

                        + sftw + "','" + rge + "','" + lose_types + "','" + sev

                        + "','" + access + "')";

                sql.execute(insert);

            }

        }

        sql.close();

        con.close();

    } catch (java.lang.ClassNotFoundException e) {

        System.err.println("ClassNotFoundException:" + e.getMessage());

    } catch (SQLException ex) {

        System.err.println("SQLException:" + ex.getMessage());

    } catch (DocumentException e) {

        e.printStackTrace();

    } catch (IOException e) {

        // TODO Auto-generated catch block

        e.printStackTrace();

    }

}

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

License:BSD License

@SuppressWarnings("unchecked")
protected List<Element> resolve(Element element, String path) throws XmlConfigException {
    final List<Element> resolved = new ArrayList<Element>();
    Attribute refAtt = element.attribute(XmlAttribute.ref.getXmlName());
    if (refAtt == null) {
        resolved.add(element);//from  w  w  w  .  ja v a  2  s.  com
    } else {
        resolveAttributeValue(refAtt, path);
        List<Element> refElCont = getReferredElementContent(refAtt.getValue(), path);
        for (final Element el : refElCont) {
            final Element newEl = el.createCopy();
            element.getParent().add(newEl);
            resolved.addAll(resolve(newEl, XmlUtil.getElementPath(el, true/*recurseParents*/)));
        }
        if (!element.getParent().remove(element)) {
            throw new RuntimeException("internal error: should have been removed");
        }
    }

    for (Element elem : resolved) {
        Iterator<Attribute> itA = elem.attributeIterator();
        while (itA.hasNext()) {
            Attribute att = itA.next();
            resolveAttributeValue(att, path);
        }

        //         resolve(elem.elementIterator(), path);
        Iterator<Element> itE = elem.elementIterator();
        while (itE.hasNext()) {
            Element child = itE.next();
            resolve(child, path + "/" + XmlUtil.getElementPath(child, false /*recurseParents*/));
        }
        if (elem.attribute(XmlAttribute.ref.getXmlName()) != null) {
            throw new RuntimeException("internal error: should have been resolved");
        }
    }
    return resolved;
}

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

License:BSD License

/**
 * Print the given element using the given print writer and initial 
 * indention/*from  ww w.  j a  v  a 2s.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:com.amalto.workbench.utils.XmlUtil.java

License:Open Source License

public static void iterateAttribute(Element element, AttributeProcess attributeProcess)
        throws DocumentException {

    // iterate through attributes of element
    for (Iterator i = element.attributeIterator(); i.hasNext();) {
        Attribute attribute = (Attribute) i.next();
        // do something
        attributeProcess.process(attribute);
    }//w w w. j a  v a2  s.com
}

From source file:com.beetle.framework.business.service.server.ServiceConfig.java

License:Apache License

private static void gendoc(Document doc) throws ClassNotFoundException {
    Node node = doc.selectSingleNode("binder");
    if (node != null) {
        Iterator<?> it = node.selectNodes("item").iterator();
        while (it.hasNext()) {
            ServiceDef sdf = new ServiceDef();
            Element e = (Element) it.next();
            String face = e.valueOf("@interface");
            String imp = e.valueOf("@implement");
            String enabled = e.valueOf("@enabled");
            sdf.setIface(face);//  w w w  .j a  v a2  s. c o m
            sdf.setImp(imp);
            sdf.setEnabled(enabled);
            @SuppressWarnings("unchecked")
            Iterator<Attribute> ait = e.attributeIterator();
            while (ait.hasNext()) {
                Attribute at = ait.next();
                sdf.addExtension(at.getName(), at.getValue());
            }
            register(sdf);
        }
    }
}

From source file:com.chinarewards.license.util.XmlUtil_dom4j.java

public static void searchAttribute(Element paramElement) {
    Iterator localIterator = paramElement.attributeIterator();
    while (localIterator.hasNext()) {
        Attribute localAttribute = (Attribute) localIterator.next();
        System.out.println(localAttribute.getName() + " " + localAttribute.getValue());
    }/*  ww  w.  j  a  va 2  s  . c  o m*/
}

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

License:Open Source License

AttributesImpl getAttributes(Element element, Expressions expressions) throws PageTemplateException {
    AttributesImpl attributes = new AttributesImpl();
    for (Iterator i = element.attributeIterator(); i.hasNext();) {
        Attribute attribute = (Attribute) i.next();
        Namespace namespace = attribute.getNamespace();
        //String prefix = namespace.getPrefix();
        //System.err.println( "attribute: name=" + attribute.getName() + "\t" +
        //                    "qualified name=" + attribute.getQualifiedName() + "\t" +
        //                    "ns prefix=" + namespace.getPrefix() + "\t" +
        //                    "ns uri=" + namespace.getURI() );
        //String qualifiedName = attribute.getName();
        //String name = qualifiedName;
        //if ( qualifiedName.startsWith( prefix + ":" ) ) {
        //    name = qualifiedName.substring( prefix.length() + 1 );
        //}//from  w  w  w.ja  v a2 s.c  o m
        String name = attribute.getName();

        // Handle JPT attributes
        //if ( prefix.equals( talNamespacePrefix ) ) {
        if (TAL_NAMESPACE_URI.equals(namespace.getURI())) {
            // tal:define
            if (name.equals("define")) {
                expressions.define = attribute.getValue();
            }

            // tal:condition
            else if (name.equals("condition")) {
                expressions.condition = attribute.getValue();
            }

            // tal:repeat
            else if (name.equals("repeat")) {
                expressions.repeat = attribute.getValue();
            }

            // tal:content
            else if (name.equals("content")) {
                expressions.content = attribute.getValue();
            }

            // tal:replace
            else if (name.equals("replace")) {
                if (expressions.omitTag == null) {
                    expressions.omitTag = "";
                }
                expressions.content = attribute.getValue();
            }

            // tal:attributes
            else if (name.equals("attributes")) {
                expressions.attributes = attribute.getValue();
            }

            // tal:omit-tag
            else if (name.equals("omit-tag")) {
                expressions.omitTag = attribute.getValue();
            }

            // tal:evaluate
            else if (name.equals("evaluate")) {
                expressions.evaluate = attribute.getValue();
            }

            // error
            else {
                throw new PageTemplateException("unknown tal attribute: " + name);
            }
        }
        //else if ( prefix.equals( metalNamespacePrefix ) ) 
        else if (METAL_NAMESPACE_URI.equals(namespace.getURI())) {
            // metal:use-macro
            if (name.equals("use-macro")) {
                expressions.useMacro = attribute.getValue();
            }

            // metal:define-slot
            else if (name.equals("define-slot")) {
                expressions.defineSlot = attribute.getValue();
            }

            // metal:define-macro
            // metal:fill-slot
            else if (name.equals("define-macro") || name.equals("fill-slot")) {
                // these are ignored here, as they don't affect processing of current
                // template, but are called from other templates
            }

            // error
            else {
                throw new PageTemplateException("unknown metal attribute: " + name);
            }
        }

        // Pass on all other attributes
        else {
            //String qualifiedName = namespace.getPrefix() + ":" + name;
            attributes.addAttribute(namespace.getURI(), name, attribute.getQualifiedName(), "CDATA",
                    attribute.getValue());
            //attributes.addAttribute( getNamespaceURIFromPrefix(prefix), name, qualifiedName, "CDATA", attribute.getValue() );
        }
    }
    return attributes;
}

From source file:com.hihframework.osplugins.dom4j.XmlParseUtil.java

License:Apache License

/**
 * ??//  w ww .  j  ava2  s  .c om
 *
 * @param element
 *            
 * @return 
 */
public Iterator<Object> attributeIterator(Element element) {
    @SuppressWarnings("unchecked")
    Iterator<Object> attrIterator = element.attributeIterator();
    return attrIterator;
}

From source file:com.hihframework.osplugins.dom4j.XmlParseUtil.java

License:Apache License

/**
 * ??/*from w ww.ja  v  a  2s  .  c o m*/
 *
 * @param element
 *            
 * @return 
 */
public HashMap<String, String> attributeMap(Element element) {
    HashMap<String, String> attributeHah = new HashMap<String, String>();
    Iterator<?> elemIterator = element.attributeIterator();
    while (elemIterator.hasNext()) {
        Attribute attribute = (Attribute) elemIterator.next();
        String attributeName = attribute.getName();
        String attributeValue = attribute.getValue();
        attributeHah.put(attributeName, attributeValue);
    }
    return attributeHah;
}

From source file:com.hihframework.osplugins.dom4j.XmlParseUtil.java

License:Apache License

/**
 * ?// ww w .j av  a  2s  .  c om
 *
 * @param element
 *            ?
 */
public boolean deleteAttribute(Element element) {
    boolean deleteFlag = false;
    Iterator<?> iterator = element.attributeIterator();
    while (iterator.hasNext()) {
        Attribute attribute = (Attribute) iterator.next();
        deleteFlag = element.remove(attribute);
    }
    return deleteFlag;
}