Example usage for org.dom4j Attribute getValue

List of usage examples for org.dom4j Attribute getValue

Introduction

In this page you can find the example usage for org.dom4j Attribute getValue.

Prototype

String getValue();

Source Link

Document

Returns the value of the attribute.

Usage

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

License:Open Source License

/**
 * Parses the <tt>introduction</tt> elements.
 *
 * @param root the root element/*w ww  . ja  v  a2s  .  co  m*/
 * @param definition the definition object
 * @param packageName the package name
 * @return flag that says if we have a definition of this kind or not
 */
private static boolean parseIntroductionElements(final Element root, final AspectWerkzDefinitionImpl definition,
        final String packageName) {
    boolean hasDef = false;
    for (Iterator it1 = root.elementIterator("introduction-def"); it1.hasNext();) {
        final IntroductionDefinition introDef = new IntroductionDefinition();

        Element introduction = (Element) it1.next();
        for (Iterator it2 = introduction.attributeIterator(); it2.hasNext();) {
            Attribute attribute = (Attribute) it2.next();

            final String name = attribute.getName().trim();
            final String value = attribute.getValue().trim();
            if (name.equals("name")) {
                introDef.setName(value);
            } else if (name.equals("interface")) {
                introDef.setInterface(packageName + value);
            } else if (name.equals("implementation")) {
                introDef.setImplementation(packageName + value);
            } else if (name.equals("deployment-model")) {
                introDef.setDeploymentModel(value);
            } else if (name.equals("attribute")) {
                introDef.setAttribute(value);
            }
        }
        definition.addIntroduction(introDef);
        hasDef = true;
    }
    return hasDef;
}

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

License:Open Source License

/**
 * Parses the <tt>advice</tt> elements.
 *
 * @param root the root element/* w w  w.j a  v a 2s. c  o m*/
 * @param definition the definition object
 * @param packageName the package name
 * @return flag that says if we have a definition of this kind or not
 */
private static boolean parseAdviceElements(final Element root, final AspectWerkzDefinitionImpl definition,
        final String packageName) {
    boolean hasDef = false;
    for (Iterator it1 = root.elementIterator("advice-def"); it1.hasNext();) {
        final AdviceDefinition adviceDef = new AdviceDefinition();

        Element advice = (Element) it1.next();
        for (Iterator it2 = advice.attributeIterator(); it2.hasNext();) {
            Attribute attribute = (Attribute) it2.next();
            final String name = attribute.getName().trim();
            final String value = attribute.getValue().trim();

            if (name.equals("name")) {
                adviceDef.setName(value);
            } else if (name.equals("class")) {
                adviceDef.setAdviceClassName(packageName + value);
            } else if (name.equals("deployment-model")) {
                adviceDef.setDeploymentModel(value);
            } else if (name.equals("attribute")) {
                adviceDef.setAttribute(value);
            }
        }
        for (Iterator it2 = advice.elementIterator(); it2.hasNext();) {
            Element nestedAdviceElement = (Element) it2.next();
            if (nestedAdviceElement.getName().trim().equals("param")) {
                adviceDef.addParameter(nestedAdviceElement.attributeValue("name"),
                        nestedAdviceElement.attributeValue("value"));
            }
        }
        definition.addAdvice(adviceDef);
        hasDef = true;
    }
    return hasDef;
}

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

License:Open Source License

/**
 * Parses the <tt>aspect</tt> elements.
 *
 * @param root the root element/*w w w  .  j  av a 2 s. c  om*/
 * @param definition the definition object
 * @param packageName the package name
 * @return flag that says if we have a definition of this kind or not
 */
private static boolean parseAspectElements(final Element root, final AspectWerkzDefinitionImpl definition,
        final String packageName) {

    // register the pointcuts before parsing the rest of the aspect
    // to be able to resolve all dependencies correctly
    for (Iterator it1 = root.elementIterator("aspect"); it1.hasNext();) {
        final Element aspect = (Element) it1.next();
        String aspectName = 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.equals("name")) {
                aspectName = value;
                continue;
            } else if (name.equals("extends")) {
                aspectName = value;
                break;
            }
        }
        // if the aspect has an abstract aspect register the pointcuts under the abstract aspects name
        registerPointcuts(aspect, aspectName, packageName);
    }
    for (Iterator it1 = root.elementIterator("abstract-aspect"); it1.hasNext();) {
        final Element aspect = (Element) it1.next();
        String aspectName = 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.equals("name")) {
                aspectName = value;
                break;
            }
        }
        registerPointcuts(aspect, aspectName, packageName);
    }

    // parse the aspect with its pointcut-def, bind-advice and bind-introduction rules
    boolean hasDef = false;
    for (Iterator it1 = root.elementIterator("aspect"); it1.hasNext();) {
        final AspectDefinition aspectDef = new AspectDefinition();
        final 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("name")) {
                aspectDef.setName(value);
            } else if (name.equals("extends")) {
                aspectDef.setExtends(value);
                continue;
            }
        }

        parsePointcutElements(aspect, aspectDef, packageName);
        parseControllerElements(aspect, aspectDef);
        parseBindIntroductionElements(aspect, aspectDef, packageName);
        parseBindAdviceElements(aspect, aspectDef, packageName);

        definition.addAspect(aspectDef);
        hasDef = true;
    }
    for (Iterator it1 = root.elementIterator("abstract-aspect"); it1.hasNext();) {
        final AspectDefinition aspectDef = new AspectDefinition();
        aspectDef.setAbstract(true);

        final 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("name")) {
                aspectDef.setName(value);
            }
        }
        parsePointcutElements(aspect, aspectDef, packageName);
        parseControllerElements(aspect, aspectDef);
        parseBindIntroductionElements(aspect, aspectDef, packageName);
        parseBindAdviceElements(aspect, aspectDef, packageName);

        definition.addAbstractAspect(aspectDef);
        hasDef = true;
    }

    for (Iterator it = definition.getAspectDefinitions().iterator(); it.hasNext();) {
        AspectDefinition aspectDef = (AspectDefinition) it.next();
        handleAbstractAspectDependencies(aspectDef, definition);
    }

    return hasDef;
}

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

License:Open Source License

/**
 * Parses and registers the pointcut elements.
 *
 * @param aspect the aspect element/*from  w  w  w  .  ja  v a2s. com*/
 * @param aspectName the name of the aspect
 * @param packageName the name of the package
 */
private static void registerPointcuts(final Element aspect, final String aspectName, final String packageName) {
    for (Iterator it2 = aspect.elementIterator(); it2.hasNext();) {
        final Element nestedAdviceElement = (Element) it2.next();
        if (nestedAdviceElement.getName().trim().equals("pointcut-def")) {
            String pointcutName = null;
            String expression = null;
            PointcutType pointcutType = null;
            try {
                for (Iterator it3 = nestedAdviceElement.attributeIterator(); it3.hasNext();) {
                    Attribute attribute = (Attribute) it3.next();
                    final String name = attribute.getName().trim();
                    final String value = attribute.getValue().trim();

                    if (name.equals("name")) {
                        pointcutName = value;
                    } else if (name.equals("type")) {
                        if (value.equalsIgnoreCase(METHOD)) {
                            pointcutType = PointcutType.EXECUTION;
                            expression = PatternFactory.createMethodPattern(
                                    nestedAdviceElement.attributeValue("pattern"), packageName);
                        } else if (value.equalsIgnoreCase(CFLOW)) {
                            pointcutType = PointcutType.CFLOW;
                            expression = PatternFactory.createCallPattern(
                                    nestedAdviceElement.attributeValue("pattern"), packageName);
                        } else if (value.equalsIgnoreCase(SET_FIELD)) {
                            pointcutType = PointcutType.SET;
                            expression = PatternFactory.createMethodPattern(
                                    nestedAdviceElement.attributeValue("pattern"), packageName);
                        } else if (value.equalsIgnoreCase(GET_FIELD)) {
                            pointcutType = PointcutType.GET;
                            expression = PatternFactory.createFieldPattern(
                                    nestedAdviceElement.attributeValue("pattern"), packageName);
                        } else if (value.equalsIgnoreCase(THROWS)) {
                            pointcutType = PointcutType.THROWS;
                            expression = PatternFactory.createThrowsPattern(
                                    nestedAdviceElement.attributeValue("pattern"), packageName);
                        } else if (value.equalsIgnoreCase(CALLER_SIDE)) {
                            pointcutType = PointcutType.CALL;
                            expression = PatternFactory.createCallPattern(
                                    nestedAdviceElement.attributeValue("pattern"), packageName);
                        } else if (value.equalsIgnoreCase(CLASS)) {
                            pointcutType = PointcutType.CLASS;
                            expression = PatternFactory.createClassPattern(
                                    nestedAdviceElement.attributeValue("pattern"), packageName);
                        }
                    }
                }
                // create and register the expression
                //                    ExpressionTemplate expressionTemplate =
                //                            Expression.createExpressionTemplate(
                //                                    aspectName,
                //                                    expression,
                //                                    packageName,
                //                                    pointcutName,
                //                                    pointcutType
                //                            );
                //                    Expression.registerExpressionTemplate(expressionTemplate);
                ExpressionNamespace space = ExpressionNamespace.getExpressionNamespace(aspectName);
                space.registerExpression(expression, packageName, pointcutName, pointcutType);
            } catch (Exception e) {
                throw new WrappedRuntimeException(e);
            }
        }
    }
}

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

License:Open Source License

/**
 * Parses the pointcut elements./*from www  .  ja v  a2 s. c om*/
 *
 * @TODO does not handle packages correctly
 *
 * @param aspect the aspect element
 * @param aspectDef the aspect definition
 * @param packageName the name of the package
 */
private static void parsePointcutElements(final Element aspect, final AspectDefinition aspectDef,
        final String packageName) {
    for (Iterator it2 = aspect.elementIterator(); it2.hasNext();) {
        final Element nestedAdviceElement = (Element) it2.next();
        if (nestedAdviceElement.getName().trim().equals("pointcut-def")) {
            try {
                final PointcutDefinition pointcutDef = new PointcutDefinition();

                for (Iterator it3 = nestedAdviceElement.attributeIterator(); it3.hasNext();) {
                    Attribute attribute = (Attribute) it3.next();
                    final String name = attribute.getName().trim();
                    final String value = attribute.getValue().trim();
                    if (name.equals("name")) {
                        pointcutDef.setName(value);
                    } else if (name.equals("type")) {
                        PointcutType type = null;
                        String expression = nestedAdviceElement.attributeValue("pattern");
                        if (expression == null || expression.length() == 0)
                            expression = nestedAdviceElement.attributeValue("expression");
                        if (value.equalsIgnoreCase(METHOD)) {
                            type = PointcutType.EXECUTION;
                            pointcutDef
                                    .setExpression(PatternFactory.createMethodPattern(expression, packageName));
                        } else if (value.equalsIgnoreCase(CFLOW)) {
                            //needed during AttributeC merge
                            type = PointcutType.CFLOW;
                            pointcutDef
                                    .setExpression(PatternFactory.createMethodPattern(expression, packageName));
                        } else if (value.equalsIgnoreCase(SET_FIELD)) {
                            type = PointcutType.SET;
                            pointcutDef
                                    .setExpression(PatternFactory.createFieldPattern(expression, packageName));
                        } else if (value.equalsIgnoreCase(GET_FIELD)) {
                            type = PointcutType.GET;
                            pointcutDef
                                    .setExpression(PatternFactory.createFieldPattern(expression, packageName));
                        } else if (value.equalsIgnoreCase(THROWS)) {
                            type = PointcutType.THROWS;
                            pointcutDef
                                    .setExpression(PatternFactory.createThrowsPattern(expression, packageName));
                        } else if (value.equalsIgnoreCase(CALLER_SIDE)) {
                            type = PointcutType.CALL;
                            pointcutDef
                                    .setExpression(PatternFactory.createCallPattern(expression, packageName));
                        } else if (value.equalsIgnoreCase(CLASS)) {
                            type = PointcutType.CLASS;
                            pointcutDef
                                    .setExpression(PatternFactory.createClassPattern(expression, packageName));
                        }
                        pointcutDef.setType(type);
                    } else if (name.equals("non-reentrant")) {
                        pointcutDef.setNonReentrant(value);
                    }
                }
                aspectDef.addPointcutDef(pointcutDef);
            } catch (Exception e) {
                throw new WrappedRuntimeException(e);
            }
        }
    }
}

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

License:Open Source License

/**
 * Parses the introduce elements.//w  w w  .  ja v  a  2  s  .com
 *
 * @param aspect the aspect element
 * @param aspectDef the aspect definition
 * @param packageName the name of the package
 */
private static void parseBindIntroductionElements(final Element aspect, final AspectDefinition aspectDef,
        final String packageName) {
    for (Iterator it2 = aspect.elementIterator(); it2.hasNext();) {
        final Element nestedAdviceElement = (Element) it2.next();
        if (nestedAdviceElement.getName().trim().equals("bind-introduction")) {
            try {
                final BindIntroductionRule bindIntroductionRule = new BindIntroductionRule();
                for (Iterator it3 = nestedAdviceElement.attributeIterator(); it3.hasNext();) {
                    Attribute attribute = (Attribute) it3.next();
                    final String name = attribute.getName().trim();
                    final String value = attribute.getValue().trim();
                    if (name.equals("class")) {
                        bindIntroductionRule.setExpression(
                                //                                    Expression.createRootExpression(
                                //                                            aspectDef.getName(),
                                //                                            packageName + value,
                                //                                            PointcutType.CLASS // needed for anonymous expressions
                                //                                    ));
                                ExpressionNamespace.getExpressionNamespace(aspectDef.getName())
                                        .createExpression(packageName + value, PointcutType.CLASS));
                    } else if (name.equals("introduction-ref")) {
                        bindIntroductionRule.addIntroductionRef(value);
                    }
                }
                parseIntroductionWeavingRuleNestedElements(nestedAdviceElement, bindIntroductionRule);
                aspectDef.addBindIntroductionRule(bindIntroductionRule);
            } catch (Exception e) {
                throw new DefinitionException("introduction definition in aspect " + aspectDef.getName()
                        + " is not well-formed: " + e.toString());
            }
        }
    }
}

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

License:Open Source License

/**
 * Parses the advise elements./*w ww  . ja  va 2s .c o  m*/
 *
 * @TODO: how to handle cflow?
 *
 * @param aspect the aspect element
 * @param aspectDef the aspect definition
 * @param packageName the name of the package
 */
private static void parseBindAdviceElements(final Element aspect, final AspectDefinition aspectDef,
        final String packageName) {
    for (Iterator it2 = aspect.elementIterator(); it2.hasNext();) {
        final Element nestedAdviceElement = (Element) it2.next();
        if (nestedAdviceElement.getName().trim().equals("bind-advice")) {
            try {
                final BindAdviceRule bindAdviceRule = new BindAdviceRule();
                String pointcutExpression = "";
                for (Iterator it3 = nestedAdviceElement.attributeIterator(); it3.hasNext();) {
                    Attribute attribute = (Attribute) it3.next();
                    final String name = attribute.getName().trim();
                    final String value = attribute.getValue().trim();
                    if (name.equals("cflow")) {
                        // support for old system cflow=.. pc=..
                        pointcutExpression += " IN (" + value + ")";
                        //                            bindAdviceRule.setCflowExpression(
                        //                                    Expression.createCflowExpression(
                        //                                        aspectDef.getName(),
                        //                                        aspectDef.getPointcutDef(value).getExpression(),
                        //                                        packageName,
                        //                                        value)
                        //                            );
                    } else if (name.equals("pointcut") || name.equals("expression")) {
                        pointcutExpression = value + pointcutExpression;
                        //                            bindAdviceRule.setExpression(
                        //                                    Expression.createRootExpression(
                        //                                            aspectDef.getName(),
                        //                                            value
                        //                                    ));
                    } else if (name.equals("advice-ref")) {
                        bindAdviceRule.addAdviceRef(value);
                    }
                }
                // add binding here once cflow expr has been assembled (@since jjtree)
                ExpressionNamespace space = ExpressionNamespace.getExpressionNamespace(aspectDef.getName());
                bindAdviceRule.setExpression(space.createExpression(pointcutExpression));

                parseAdviceWeavingRuleNestedElements(nestedAdviceElement, bindAdviceRule);
                aspectDef.addBindAdviceRule(bindAdviceRule);
            } catch (Exception e) {
                e.printStackTrace();
                throw new DefinitionException("advice definition in aspect " + aspectDef.getName()
                        + " is not well-formed: " + e.toString());
            }
        }
    }
}

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

License:Open Source License

/**
 * Parses the <tt>advices</tt> elements.
 *
 * @param root the root element/*from  w w  w  . j a  v a2  s .  com*/
 * @param definition the definition object
 * @return flag that says if we have a definition of this kind or not
 */
private static boolean parseAdviceStackElements(final Element root,
        final AspectWerkzDefinitionImpl definition) {
    boolean hasDef = false;
    for (Iterator it1 = root.elementIterator("advices-def"); it1.hasNext();) {
        final AdviceStackDefinition adviceStackDef = new AdviceStackDefinition();

        Element adviceStack = (Element) it1.next();
        for (Iterator it2 = adviceStack.attributeIterator(); it2.hasNext();) {
            Attribute attribute = (Attribute) it2.next();
            final String name = attribute.getName().trim();
            final String value = attribute.getValue().trim();
            if (name.equals("name")) {
                adviceStackDef.setName(value);
            }
        }
        for (Iterator it2 = adviceStack.elementIterator(); it2.hasNext();) {
            Element nestedElement = (Element) it2.next();
            if (nestedElement.getName().trim().equals("advice-ref")) {
                adviceStackDef.addAdvice(nestedElement.attributeValue("name"));
            }
        }
        definition.addAdviceStack(adviceStackDef);
        hasDef = true;
    }
    return hasDef;
}

From source file:org.codehaus.modello.plugin.utils.Dom4jUtils.java

License:Apache License

/**
 * Verifies if a specified {@link Attribute} exists in the specified
 * {@link Document} and its actual value matches the passed in expected
 * value./*from  ww w. jav  a2 s.  c o m*/
 * 
 * @param doc target {@link Document} where the attribute is expected to
 *            occur.
 * @param xpathToNode XPATH expression to locate the attribute in the target
 *            {@link Document}.
 * @param attributeKey identifier/name of the attribute.
 * @param expectedValue expected value to match the attribute's actual
 *            value.
 */
public static void assertAttributeEquals(Document doc, String xpathToNode, String attributeKey,
        String expectedValue) {
    if (expectedValue == null) {
        throw new AssertionFailedError("Unable to assert an attribute using a null expected value.");
    }

    Attribute attribute = findAttribute(doc, xpathToNode, attributeKey);

    if (attribute == null) {
        throw new AssertionFailedError(
                "Element at '" + xpathToNode + "' is missing the '" + attributeKey + "' attribute.");
    }

    Assert.assertEquals("Attribute value for '" + xpathToNode + "'", expectedValue, attribute.getValue());
}

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

License:Open Source License

@SuppressWarnings("unchecked")
protected Map<String, String> getMergeCueParams(Element element, Attribute mergeCueAttribute) {
    Map<String, String> params = new HashMap<String, String>();
    String paramsPrefix = mergeCueAttribute.getQualifiedName() + "-";
    List<Attribute> attributes = element.attributes();

    for (Iterator<Attribute> i = attributes.iterator(); i.hasNext();) {
        Attribute attribute = i.next();
        String attributeQualifiedName = attribute.getQualifiedName();
        if (attributeQualifiedName.startsWith(paramsPrefix)) {
            i.remove();//  w  w w. j a v a  2s. com

            String paramName = attributeQualifiedName.substring(paramsPrefix.length());
            String paramValue = attribute.getValue();

            params.put(paramName, paramValue);
        }
    }

    return params;
}