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:org.apache.openmeetings.rss.LoadAtomRssFeed.java

License:Apache License

public LinkedHashMap<String, LinkedHashMap<String, LinkedHashMap<String, Object>>> parseRssFeed(
        String urlEndPoint) {//from   w  w  w .ja v  a 2 s  .c o  m
    try {
        LinkedHashMap<String, LinkedHashMap<String, LinkedHashMap<String, Object>>> lMap = new LinkedHashMap<String, LinkedHashMap<String, LinkedHashMap<String, Object>>>();

        URL url = new URL(urlEndPoint);

        HttpURLConnection conn = (HttpURLConnection) url.openConnection();

        conn.setDoOutput(true);
        conn.setDoInput(true);
        conn.setRequestProperty("user-agent",
                "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)");
        conn.setRequestProperty("Referer", "http://openmeetings.apache.org/");
        conn.connect();

        SAXReader reader = new SAXReader();
        Document document = reader.read(conn.getInputStream());

        Element root = document.getRootElement();
        int l = 0;

        for (@SuppressWarnings("unchecked")
        Iterator<Element> i = root.elementIterator(); i.hasNext();) {
            Element item = i.next();
            LinkedHashMap<String, LinkedHashMap<String, Object>> items = new LinkedHashMap<String, LinkedHashMap<String, Object>>();
            boolean isSubElement = false;

            for (@SuppressWarnings("unchecked")
            Iterator<Element> it2 = item.elementIterator(); it2.hasNext();) {
                Element subItem = it2.next();

                LinkedHashMap<String, Object> itemObj = new LinkedHashMap<String, Object>();

                itemObj.put("name", subItem.getName());
                itemObj.put("text", subItem.getText());

                LinkedHashMap<String, String> attributes = new LinkedHashMap<String, String>();

                for (@SuppressWarnings("unchecked")
                Iterator<Attribute> attr = subItem.attributeIterator(); attr.hasNext();) {
                    Attribute at = attr.next();
                    attributes.put(at.getName(), at.getText());
                }
                itemObj.put("attributes", attributes);

                // log.error(subItem.getName()+ ": " +subItem.getText());
                items.put(subItem.getName(), itemObj);
                isSubElement = true;
            }

            if (isSubElement) {
                l++;
                lMap.put("item" + l, items);
            }

        }

        return lMap;

    } catch (Exception err) {
        log.error("[parseRssFeed]", err);
    }
    return null;

}

From source file:org.codehaus.aspectwerkz.attribdef.definition.DocumentParser.java

License:Open Source License

/**
 * Parses the <tt>use-aspect</tt> elements.
 *
 * @param systemElement the system element
 * @param definition the definition object
 * @param packageName the package name/* ww  w. ja  v a 2s.c  om*/
 * @return flag that says if we have a definition of this kind or not
 */
private static boolean parseUseAspectElements(final Element systemElement,
        final AspectWerkzDefinition definition, final String packageName) {
    boolean hasDef = false;
    for (Iterator it1 = systemElement.elementIterator("use-aspect"); it1.hasNext();) {

        String className = null;
        Element aspect = (Element) it1.next();
        for (Iterator it2 = aspect.attributeIterator(); it2.hasNext();) {
            Attribute attribute = (Attribute) it2.next();

            final String name = attribute.getName().trim();
            final String value = attribute.getValue().trim();
            if (name.equals("class")) {
                className = value;
                break;
            }
        }
        String aspectClassName = packageName + className;
        AspectWerkzDefinitionImpl def = (AspectWerkzDefinitionImpl) definition;
        for (Iterator it2 = aspect.elementIterator(); it2.hasNext();) {
            Element nestedAdviceElement = (Element) it2.next();
            if (nestedAdviceElement.getName().trim().equals("param")) {
                def.addParameter(aspectClassName, nestedAdviceElement.attributeValue("name"),
                        nestedAdviceElement.attributeValue("value"));
            }
        }
        definition.addAspectToUse(aspectClassName);
        hasDef = true;
    }
    return hasDef;
}

From source file:org.codehaus.aspectwerkz.attribdef.definition.DocumentParser.java

License:Open Source License

/**
 * Retrieves and returns the package.//w ww .j  a v a 2s  . c om
 *
 * @param packageElement the package element
 * @return the package
 */
private static String getPackage(final Element packageElement) {
    String packageName = "";
    for (Iterator it2 = packageElement.attributeIterator(); it2.hasNext();) {
        Attribute attribute = (Attribute) it2.next();
        if (attribute.getName().trim().equals("name")) {
            packageName = attribute.getValue().trim();
            if (packageName.endsWith(".*")) {
                packageName = packageName.substring(0, packageName.length() - 1);
            } else if (packageName.endsWith(".")) {
                ;// skip
            } else {
                packageName += ".";
            }
            break;
        } else {
            continue;
        }
    }
    return packageName;
}

From source file:org.codehaus.aspectwerkz.definition.DocumentParser.java

License:Open Source License

/**
 * Parses aspect class names./*from  w ww .j a  va  2 s .c o m*/
 *
 * @param document the defintion as a document
 * @return the aspect class names
 */
public static List parseAspectClassNames(final Document document) {
    final List aspectClassNames = new ArrayList();
    for (Iterator it1 = document.getRootElement().elementIterator("system"); it1.hasNext();) {
        Element system = (Element) it1.next();
        final String basePackage = getBasePackage(system);
        for (Iterator it11 = system.elementIterator("aspect"); it11.hasNext();) {
            String className = null;
            Element aspect = (Element) it11.next();
            for (Iterator it2 = aspect.attributeIterator(); it2.hasNext();) {
                Attribute attribute = (Attribute) it2.next();
                final String name = attribute.getName().trim();
                final String value = attribute.getValue().trim();
                if (name.equalsIgnoreCase("class")) {
                    className = value;
                }
            }
            aspectClassNames.add(basePackage + className);
        }
        for (Iterator it11 = system.elementIterator("package"); it11.hasNext();) {
            final Element packageElement = ((Element) it11.next());
            final String packageName = getPackage(packageElement);
            for (Iterator it12 = packageElement.elementIterator("aspect"); it12.hasNext();) {
                String className = null;
                Element aspect = (Element) it12.next();
                for (Iterator it2 = aspect.attributeIterator(); it2.hasNext();) {
                    Attribute attribute = (Attribute) it2.next();
                    final String name = attribute.getName().trim();
                    final String value = attribute.getValue().trim();
                    if (name.equalsIgnoreCase("class")) {
                        className = value;
                    }
                }
                aspectClassNames.add(packageName + className);
            }
        }
    }
    aspectClassNames.add(Virtual.class.getName());

    return aspectClassNames;
}

From source file:org.codehaus.aspectwerkz.definition.DocumentParser.java

License:Open Source License

/**
 * Parses the definition DOM document./*from  ww w  .  j a  v  a2 s.co  m*/
 *
 * @param document    the defintion as a document
 * @param systemDef   the system definition
 * @param aspectClass the aspect class
 * @return the definition
 */
public static AspectDefinition parseAspectDefinition(final Document document, final SystemDefinition systemDef,
        final Class aspectClass) {

    final Element aspect = document.getRootElement();

    if (!aspect.getName().equals("aspect")) {
        throw new DefinitionException("XML definition for aspect is not well-formed: " + document.asXML());
    }
    String specialAspectName = null;
    String className = null;
    String deploymentModelAsString = null;
    String containerClassName = null;
    for (Iterator it2 = aspect.attributeIterator(); it2.hasNext();) {
        Attribute attribute = (Attribute) it2.next();
        final String name = attribute.getName().trim();
        final String value = attribute.getValue().trim();
        if (name.equalsIgnoreCase("class")) {
            className = value;
        } else if (name.equalsIgnoreCase("deployment-model")) {
            deploymentModelAsString = value;
        } else if (name.equalsIgnoreCase("name")) {
            specialAspectName = value;
        } else if (name.equalsIgnoreCase("container")) {
            containerClassName = value;
        }
    }
    if (specialAspectName == null) {
        specialAspectName = className;
    }

    final ClassInfo classInfo = JavaClassInfo.getClassInfo(aspectClass);
    final ClassLoader loader = aspectClass.getClassLoader();

    // create the aspect definition
    final AspectDefinition aspectDef = new AspectDefinition(specialAspectName, classInfo, systemDef);
    //TODO: if this XML centric deployment is supposed to PRESERVE @Aspect values, then it is broken
    aspectDef.setContainerClassName(containerClassName);
    aspectDef.setDeploymentModel(DeploymentModel.getDeploymentModelFor(deploymentModelAsString));

    parsePointcutElements(aspect, aspectDef); //needed to support undefined named pointcut in Attributes AW-152

    // load the different aspect model and let them define their aspects
    AspectModelManager.defineAspect(classInfo, aspectDef, loader);

    // parse the aspect info
    parseParameterElements(aspect, aspectDef);
    parsePointcutElements(aspect, aspectDef); //reparse pc for XML override (AW-152)
    parseAdviceElements(aspect, aspectDef, JavaClassInfo.getClassInfo(aspectClass));
    parseIntroduceElements(aspect, aspectDef, "", aspectClass.getClassLoader());

    systemDef.addAspect(aspectDef);
    return aspectDef;
}

From source file:org.codehaus.aspectwerkz.definition.DocumentParser.java

License:Open Source License

/**
 * Parses the global pointcuts./*from  w w  w . j a va2  s. c  o m*/
 *
 * @param systemElement the system element
 * @return a list with the pointcuts
 */
private static List parseGlobalPointcutDefs(final Element systemElement) {
    final List globalPointcuts = new ArrayList();
    for (Iterator it11 = systemElement.elementIterator("pointcut"); it11.hasNext();) {
        PointcutInfo pointcutInfo = new PointcutInfo();
        Element globalPointcut = (Element) it11.next();
        for (Iterator it2 = globalPointcut.attributeIterator(); it2.hasNext();) {
            Attribute attribute = (Attribute) it2.next();
            final String name = attribute.getName().trim();
            final String value = attribute.getValue().trim();
            if (name.equalsIgnoreCase("name")) {
                pointcutInfo.name = value;
            } else if (name.equalsIgnoreCase("expression")) {
                pointcutInfo.expression = value;
            }
        }
        // pointcut CDATA is expression unless already specified as an attribute
        if (pointcutInfo.expression == null) {
            pointcutInfo.expression = globalPointcut.getTextTrim();
        }
        globalPointcuts.add(pointcutInfo);
    }
    return globalPointcuts;
}

From source file:org.codehaus.aspectwerkz.definition.DocumentParser.java

License:Open Source License

/**
 * Parses the global deployment-scope elements.
 *
 * @param systemElement the system element
 * @param definition//from w  ww. jav  a 2s . c o  m
 */
private static void parseDeploymentScopeDefs(final Element systemElement, final SystemDefinition definition) {
    for (Iterator it11 = systemElement.elementIterator("deployment-scope"); it11.hasNext();) {
        String expression = null;
        String name = null;
        Element globalPointcut = (Element) it11.next();
        for (Iterator it2 = globalPointcut.attributeIterator(); it2.hasNext();) {
            Attribute attribute = (Attribute) it2.next();
            final String attrName = attribute.getName().trim();
            final String attrValue = attribute.getValue().trim();
            if (attrName.equalsIgnoreCase("name")) {
                name = attrValue;
            } else if (attrName.equalsIgnoreCase("expression")) {
                expression = attrValue;
            }
        }
        // pointcut CDATA is expression unless already specified as an attribute
        if (expression == null) {
            expression = globalPointcut.getTextTrim();
        }
        DefinitionParserHelper.createAndAddDeploymentScopeDef(name, expression, definition);
    }
}

From source file:org.codehaus.aspectwerkz.definition.DocumentParser.java

License:Open Source License

/**
 * Parses the global advisable elements.
 *
 * @param systemElement the system element
 * @param definition/*from www .j a va2 s .c om*/
 */
private static void parseAdvisableDefs(final Element systemElement, final SystemDefinition definition) {
    for (Iterator it11 = systemElement.elementIterator("advisable"); it11.hasNext();) {
        Element advisableElement = (Element) it11.next();
        String expression = "";
        String pointcutTypes = "all";
        for (Iterator it2 = advisableElement.attributeIterator(); it2.hasNext();) {
            Attribute attribute = (Attribute) it2.next();
            final String name = attribute.getName().trim();
            final String value = attribute.getValue().trim();
            if (name.equalsIgnoreCase("expression")) {
                expression = value;
            } else if (name.equalsIgnoreCase("pointcut-type")) {
                pointcutTypes = value;
            }
        }
        // pointcut CDATA is expression unless already specified as an attribute
        if (expression == null) {
            expression = advisableElement.getTextTrim();
        }
        handleAdvisableDefinition(definition, expression, pointcutTypes);
    }
}

From source file:org.codehaus.aspectwerkz.definition.DocumentParser.java

License:Open Source License

/**
 * Parses the <tt>aspect</tt> elements.
 *
 * @param loader          the current class loader
 * @param systemElement   the system element
 * @param definition      the definition object
 * @param packageName     the package name
 * @param globalPointcuts the global pointcuts
 *//*from  ww  w. j av a 2  s. c om*/
private static void parseAspectElements(final ClassLoader loader, final Element systemElement,
        final SystemDefinition definition, final String packageName, final List globalPointcuts) {

    for (Iterator it1 = systemElement.elementIterator("aspect"); it1.hasNext();) {
        String aspectName = null;
        String className = null;
        String deploymentModel = null;
        String containerClassName = null;
        Element aspect = (Element) it1.next();
        for (Iterator it2 = aspect.attributeIterator(); it2.hasNext();) {
            Attribute attribute = (Attribute) it2.next();
            final String name = attribute.getName().trim();
            final String value = attribute.getValue().trim();
            if (name.equalsIgnoreCase("class")) {
                className = value;
            } else if (name.equalsIgnoreCase("deployment-model")) {
                deploymentModel = value;
            } else if (name.equalsIgnoreCase("name")) {
                aspectName = value;
            } else if (name.equalsIgnoreCase("container")) {
                containerClassName = value;
            }
        }

        // class is mandatory
        if (Strings.isNullOrEmpty(className)) {
            System.err.println("Warning: could not load aspect without 'class=..' attribute");
            new Exception().printStackTrace();
            continue;
        }

        String aspectClassName = packageName + className;
        if (aspectName == null) {
            aspectName = aspectClassName;
        }

        // create the aspect definition
        ClassInfo aspectClassInfo;
        try {
            aspectClassInfo = AsmClassInfo.getClassInfo(aspectClassName, loader);
        } catch (Exception e) {
            System.err.println("Warning: could not load aspect " + aspectClassName + " from " + loader
                    + "due to: " + e.toString());
            e.printStackTrace();
            continue;
        }

        final AspectDefinition aspectDef = new AspectDefinition(aspectName, aspectClassInfo, definition);

        // add the global pointcuts to the aspect
        for (Iterator it = globalPointcuts.iterator(); it.hasNext();) {
            PointcutInfo pointcutInfo = (PointcutInfo) it.next();
            DefinitionParserHelper.createAndAddPointcutDefToAspectDef(pointcutInfo.name,
                    pointcutInfo.expression, aspectDef);
        }
        parsePointcutElements(aspect, aspectDef); //needed to support undefined named pointcut in Attributes AW-152

        // load the different aspect model and let them define their aspects
        AspectModelManager.defineAspect(aspectClassInfo, aspectDef, loader);

        // parse the class bytecode annotations
        AspectAnnotationParser.parse(aspectClassInfo, aspectDef, loader);

        // XML definition settings always overrides attribute definition settings
        // AW-357
        if (!Strings.isNullOrEmpty(deploymentModel)) {
            aspectDef.setDeploymentModel(DeploymentModel.getDeploymentModelFor(deploymentModel));
        }
        if (!Strings.isNullOrEmpty(aspectName)) {
            aspectDef.setName(aspectName);
        }
        if (!Strings.isNullOrEmpty(containerClassName)) {
            aspectDef.setContainerClassName(containerClassName);
        }

        // parse the aspect info
        parseParameterElements(aspect, aspectDef);
        parsePointcutElements(aspect, aspectDef); //reparse pc for XML override (AW-152)
        parseAdviceElements(aspect, aspectDef, aspectClassInfo);
        parseIntroduceElements(aspect, aspectDef, packageName, loader);

        definition.addAspect(aspectDef);
    }
}

From source file:org.codehaus.aspectwerkz.definition.DocumentParser.java

License:Open Source License

/**
 * Parses the <tt>mixin</tt> elements.
 *
 * @param loader           the current class loader
 * @param systemElement    the system element
 * @param systemDefinition the system definition
 * @param packageName      the package name
 *///ww w . ja  va 2 s .  com
private static void parseMixinElements(final ClassLoader loader, final Element systemElement,
        final SystemDefinition systemDefinition, final String packageName) {

    for (Iterator it1 = systemElement.elementIterator("mixin"); it1.hasNext();) {
        String className = null;
        String deploymentModelAsString = null;
        boolean isTransient = false;
        boolean isTransientSetInXML = false;
        String factoryClassName = null;
        String expression = null;
        Element mixin = (Element) it1.next();
        for (Iterator it2 = mixin.attributeIterator(); it2.hasNext();) {
            Attribute attribute = (Attribute) it2.next();
            final String name = attribute.getName().trim();
            final String value = attribute.getValue().trim();
            if (name.equalsIgnoreCase("class")) {
                className = value;
            } else if (name.equalsIgnoreCase("deployment-model") && value != null) {
                deploymentModelAsString = value;
            } else if (name.equalsIgnoreCase("transient")) {
                if (value != null && value.equalsIgnoreCase("true")) {
                    isTransient = true;
                    isTransientSetInXML = true;
                }
            } else if (name.equalsIgnoreCase("factory")) {
                factoryClassName = value;
            } else if (name.equalsIgnoreCase("bind-to")) {
                expression = value;
            }
        }
        String mixinClassName = packageName + className;

        // create the mixin definition
        ClassInfo mixinClassInfo;
        try {
            mixinClassInfo = AsmClassInfo.getClassInfo(mixinClassName, loader);
        } catch (Exception e) {
            System.err.println("Warning: could not load mixin " + mixinClassName + " from " + loader
                    + "due to: " + e.toString());
            e.printStackTrace();
            continue;
        }

        final DeploymentModel deploymentModel = (deploymentModelAsString != null)
                ? DeploymentModel.getDeploymentModelFor(deploymentModelAsString)
                : DeploymentModel.PER_INSTANCE;

        final MixinDefinition mixinDefinition = DefinitionParserHelper.createAndAddMixinDefToSystemDef(
                mixinClassInfo, expression, deploymentModel, isTransient, systemDefinition);

        // parse the class bytecode annotations
        MixinAnnotationParser.parse(mixinClassInfo, mixinDefinition);

        // XML definition settings always overrides attribute definition settings if present
        if (!Strings.isNullOrEmpty(deploymentModelAsString)) {
            mixinDefinition.setDeploymentModel(DeploymentModel.getDeploymentModelFor(deploymentModelAsString));
        }
        if (!Strings.isNullOrEmpty(factoryClassName)) {
            mixinDefinition.setFactoryClassName(factoryClassName);
        }
        if (isTransientSetInXML) {
            mixinDefinition.setTransient(isTransient);
        }

        parseParameterElements(mixin, mixinDefinition);
    }
}