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.apereo.portal.tenants.TemplateDataTenantOperationsListener.java

License:Apache License

private boolean evaluatePortalDataKeyMatch(Document doc, PortalDataKey pdk) {
    // Matching is tougher because it's dom4j <> w3c...
    final QName qname = doc.getRootElement().getQName();
    if (!qname.getName().equals(pdk.getName().getLocalPart())
            || !qname.getNamespaceURI().equals(pdk.getName().getNamespaceURI())) {
        // Rule these out straight off...
        return false;
    }/*w ww .  jav a2s  . c o m*/

    // If the PortalDataKey declares a script
    // (old method), the document must match it
    final Attribute s = doc.getRootElement().attribute(PortalDataKey.SCRIPT_ATTRIBUTE_NAME.getLocalPart());
    final String script = s != null ? s.getValue() : null;
    if (pdk.getScript() != null) {
        // If the pdk declares a script, the data document MUST match it...
        if (pdk.getScript().equals(script)) {
            /*
             * A data document that matches on script need not match on version
             * as well.  It appears that the pdk.version member is overloaded
             * with two purposes...
             *
             *   - A numeric version (e.g. 4.0) indicates a match IN THE ABSENCE
             *     OF a script attribute (newer method, below)
             *   - A word (e.g. 'GROUP' or 'MEMBERS') indicates the type of data
             *     the pdk handles, where more than one pdk applies to the data
             *     document
             */
            return true;
        } else {
            return false;
        }
    }

    // If the PortalDataKey declares a version BUT NOT a script (new
    // method), the document must match it
    final Attribute v = doc.getRootElement().attribute(PortalDataKey.VERSION_ATTRIBUTE_NAME.getLocalPart());
    final String version = v != null ? v.getValue() : null;
    if (pdk.getVersion() != null && pdk.getVersion().equals(version)) {
        return true;
    }

    // This pdk is not a match
    return false;
}

From source file:org.b5chat.crossfire.core.plugin.PluginManager.java

License:Open Source License

/**
 * Loads a plug-in module into the container. Loading consists of the
 * following steps:<ul>/*from www  .  j a  va  2  s .c om*/
 * <p/>
 * <li>Add all jars in the <tt>lib</tt> dir (if it exists) to the class loader</li>
 * <li>Add all files in <tt>classes</tt> dir (if it exists) to the class loader</li>
 * <li>Locate and load <tt>module.xml</tt> into the context</li>
 * <li>For each b5chat.module entry, load the given class as a module and start it</li>
 * <p/>
 * </ul>
 *
 * @param pluginDir the plugin directory.
 */
private void loadPlugin(File pluginDir) {
    // Only load the admin plugin during setup mode.
    if (XmppServer.getInstance().isSetupMode() && !(pluginDir.getName().equals("admin"))) {
        return;
    }
    Log.debug("PluginManager: Loading plugin " + pluginDir.getName());
    IPlugin plugin;
    try {
        File pluginConfig = new File(pluginDir, "plugin.xml");
        if (pluginConfig.exists()) {
            SAXReader saxReader = new SAXReader();
            saxReader.setEncoding("UTF-8");
            Document pluginXML = saxReader.read(pluginConfig);

            // See if the plugin specifies a version of crossfire
            // required to run.
            Element minServerVersion = (Element) pluginXML.selectSingleNode("/plugin/minServerVersion");
            if (minServerVersion != null) {
                String requiredVersion = minServerVersion.getTextTrim();
                Version version = XmppServer.getInstance().getServerInfo().getVersion();
                String hasVersion = version.getMajor() + "." + version.getMinor() + "." + version.getMicro();
                if (hasVersion.compareTo(requiredVersion) < 0) {
                    String msg = "Ignoring plugin " + pluginDir.getName() + ": requires " + "server version "
                            + requiredVersion;
                    Log.warn(msg);
                    System.out.println(msg);
                    return;
                }
            }

            PluginClassLoader pluginLoader;

            // Check to see if this is a child plugin of another plugin. If it is, we
            // re-use the parent plugin's class loader so that the plugins can interact.
            Element parentPluginNode = (Element) pluginXML.selectSingleNode("/plugin/parentPlugin");

            String pluginName = pluginDir.getName();
            String webRootKey = pluginName + ".webRoot";
            String classesDirKey = pluginName + ".classes";
            String webRoot = System.getProperty(webRootKey);
            String classesDir = System.getProperty(classesDirKey);

            if (webRoot != null) {
                final File compilationClassesDir = new File(pluginDir, "classes");
                if (!compilationClassesDir.exists()) {
                    compilationClassesDir.mkdir();
                }
                compilationClassesDir.deleteOnExit();
            }

            if (parentPluginNode != null) {
                String parentPlugin = parentPluginNode.getTextTrim();
                // See if the parent is already loaded.
                if (plugins.containsKey(parentPlugin)) {
                    pluginLoader = classloaders.get(getPlugin(parentPlugin));
                    pluginLoader.addDirectory(pluginDir, classesDir != null);

                } else {
                    // See if the parent plugin exists but just hasn't been loaded yet.
                    // This can only be the case if this plugin name is alphabetically before
                    // the parent.
                    if (pluginName.compareTo(parentPlugin) < 0) {
                        // See if the parent exists.
                        File file = new File(pluginDir.getParentFile(), parentPlugin + ".jar");
                        if (file.exists()) {
                            // Silently return. The child plugin will get loaded up on the next
                            // plugin load run after the parent.
                            return;
                        } else {
                            file = new File(pluginDir.getParentFile(), parentPlugin + ".war");
                            if (file.exists()) {
                                // Silently return. The child plugin will get loaded up on the next
                                // plugin load run after the parent.
                                return;
                            } else {
                                String msg = "Ignoring plugin " + pluginName + ": parent plugin " + parentPlugin
                                        + " not present.";
                                Log.warn(msg);
                                System.out.println(msg);
                                return;
                            }
                        }
                    } else {
                        String msg = "Ignoring plugin " + pluginName + ": parent plugin " + parentPlugin
                                + " not present.";
                        Log.warn(msg);
                        System.out.println(msg);
                        return;
                    }
                }
            }
            // This is not a child plugin, so create a new class loader.
            else {
                pluginLoader = new PluginClassLoader();
                pluginLoader.addDirectory(pluginDir, classesDir != null);
            }

            // Check to see if development mode is turned on for the plugin. If it is,
            // configure dev mode.

            PluginDevEnvironment dev = null;
            if (webRoot != null || classesDir != null) {
                dev = new PluginDevEnvironment();

                System.out.println("IPlugin " + pluginName + " is running in development mode.");
                Log.info("IPlugin " + pluginName + " is running in development mode.");
                if (webRoot != null) {
                    File webRootDir = new File(webRoot);
                    if (!webRootDir.exists()) {
                        // Ok, let's try it relative from this plugin dir?
                        webRootDir = new File(pluginDir, webRoot);
                    }

                    if (webRootDir.exists()) {
                        dev.setWebRoot(webRootDir);
                    }
                }

                if (classesDir != null) {
                    File classes = new File(classesDir);
                    if (!classes.exists()) {
                        // ok, let's try it relative from this plugin dir?
                        classes = new File(pluginDir, classesDir);
                    }

                    if (classes.exists()) {
                        dev.setClassesDir(classes);
                        pluginLoader.addURLFile(classes.getAbsoluteFile().toURI().toURL());
                    }
                }
            }

            String className = pluginXML.selectSingleNode("/plugin/class").getText().trim();
            plugin = (IPlugin) pluginLoader.loadClass(className).newInstance();
            if (parentPluginNode != null) {
                String parentPlugin = parentPluginNode.getTextTrim();
                // See if the parent is already loaded.
                if (plugins.containsKey(parentPlugin)) {
                    pluginLoader = classloaders.get(getPlugin(parentPlugin));
                    classloaders.put(plugin, pluginLoader);
                }
            }

            plugins.put(pluginName, plugin);
            pluginDirs.put(plugin, pluginDir);

            // If this is a child plugin, register it as such.
            if (parentPluginNode != null) {
                String parentPlugin = parentPluginNode.getTextTrim();
                List<String> childrenPlugins = parentPluginMap.get(plugins.get(parentPlugin));
                if (childrenPlugins == null) {
                    childrenPlugins = new ArrayList<String>();
                    parentPluginMap.put(plugins.get(parentPlugin), childrenPlugins);
                }
                childrenPlugins.add(pluginName);
                // Also register child to parent relationship.
                childPluginMap.put(plugin, parentPlugin);
            } else {
                // Only register the class loader in the case of this not being
                // a child plugin.
                classloaders.put(plugin, pluginLoader);
            }

            // Check the plugin's database schema (if it requires one).
            if (!DbConnectionManager.getSchemaManager().checkPluginSchema(plugin)) {
                // The schema was not there and auto-upgrade failed.
                Log.error(pluginName + " - " + LocaleUtils.getLocalizedString("upgrade.database.failure"));
                System.out.println(
                        pluginName + " - " + LocaleUtils.getLocalizedString("upgrade.database.failure"));
            }

            // Load any JSP's defined by the plugin.
            File webXML = new File(pluginDir, "web" + File.separator + "WEB-INF" + File.separator + "web.xml");
            if (webXML.exists()) {
                PluginServlet.registerServlets(this, plugin, webXML);
            }
            // Load any custom-defined servlets.
            File customWebXML = new File(pluginDir,
                    "web" + File.separator + "WEB-INF" + File.separator + "web-custom.xml");
            if (customWebXML.exists()) {
                PluginServlet.registerServlets(this, plugin, customWebXML);
            }

            if (dev != null) {
                pluginDevelopment.put(plugin, dev);
            }

            // Configure caches of the plugin
            configureCaches(pluginDir, pluginName);

            // Init the plugin.
            ClassLoader oldLoader = Thread.currentThread().getContextClassLoader();
            Thread.currentThread().setContextClassLoader(pluginLoader);
            plugin.initializePlugin(this, pluginDir);
            Thread.currentThread().setContextClassLoader(oldLoader);

            // If there a <adminconsole> section defined, register it.
            Element adminElement = (Element) pluginXML.selectSingleNode("/plugin/adminconsole");
            if (adminElement != null) {
                Element appName = (Element) adminElement
                        .selectSingleNode("/plugin/adminconsole/global/appname");
                if (appName != null) {
                    // Set the plugin name so that the proper i18n String can be loaded.
                    appName.addAttribute("plugin", pluginName);
                }
                // If global images are specified, override their URL.
                Element imageEl = (Element) adminElement
                        .selectSingleNode("/plugin/adminconsole/global/logo-image");
                if (imageEl != null) {
                    imageEl.setText("plugins/" + pluginName + "/" + imageEl.getText());
                    // Set the plugin name so that the proper i18n String can be loaded.
                    imageEl.addAttribute("plugin", pluginName);
                }
                imageEl = (Element) adminElement.selectSingleNode("/plugin/adminconsole/global/login-image");
                if (imageEl != null) {
                    imageEl.setText("plugins/" + pluginName + "/" + imageEl.getText());
                    // Set the plugin name so that the proper i18n String can be loaded.
                    imageEl.addAttribute("plugin", pluginName);
                }
                // Modify all the URL's in the XML so that they are passed through
                // the plugin servlet correctly.
                @SuppressWarnings("unchecked")
                List<Object> urls = adminElement.selectNodes("//@url");
                for (Object url : urls) {
                    Attribute attr = (Attribute) url;
                    attr.setValue("plugins/" + pluginName + "/" + attr.getValue());
                }
                // In order to internationalize the names and descriptions in the model,
                // we add a "plugin" attribute to each tab, sidebar, and item so that
                // the the renderer knows where to load the i18n Strings from.
                String[] elementNames = new String[] { "tab", "sidebar", "item" };
                for (String elementName : elementNames) {
                    @SuppressWarnings("unchecked")
                    List<Object> values = adminElement.selectNodes("//" + elementName);
                    for (Object value : values) {
                        Element element = (Element) value;
                        // Make sure there's a name or description. Otherwise, no need to
                        // override i18n settings.
                        if (element.attribute("name") != null || element.attribute("value") != null) {
                            element.addAttribute("plugin", pluginName);
                        }
                    }
                }

                AdminConsole.addModel(pluginName, adminElement);
            }
            firePluginCreatedEvent(pluginName, plugin);
        } else {
            Log.warn("IPlugin " + pluginDir + " could not be loaded: no plugin.xml file found");
        }
    } catch (Throwable e) {
        Log.error("Error loading plugin: " + pluginDir, e);
    }
}

From source file:org.bigtester.ate.model.page.atewebdriver.ReadXmlFile.java

License:Apache License

/**
 * Gets the node value./*from   w w  w . java  2 s. c  o  m*/
 *
 * @param node the node
 * @return the node value
 */
public static String getNodeValue(Element node) {

    Attribute attri = node.attribute("id");
    String nodeValue = attri.getValue();
    if (null == nodeValue)
        throw GlobalUtils.createNotInitializedException("repo.xml is not correct,cannot get node value");
    return nodeValue;
}

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/* w ww .ja  v a 2 s.c  o  m*/
 * @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.//from   w  w w.j a  v a2  s .c o  m
 *
 * @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 ww w. ja va 2  s.  co  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 w w w. j av a  2  s  .  c om
 *
 * @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.//w w w.  j ava 2 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 w  w  .ja  va2s  .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//w ww.j  av a 2  s. c  o m
 */
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);
    }
}