Example usage for org.dom4j Element attribute

List of usage examples for org.dom4j Element attribute

Introduction

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

Prototype

Attribute attribute(QName qName);

Source Link

Document

DOCUMENT ME!

Usage

From source file:net.techest.railgun.action.DbstoreActionNode.java

License:Apache License

@Override
public Shell execute(Element node, Shell shell) throws Exception {
    if (node.attribute("source") == null) {
        throw new ActionException("?");
    }// w ww .j av  a 2s  .  c  om
    String source = node.attributeValue("source").toUpperCase();
    DBConnection connection = null;
    try {
        connection = new DBConnection(source);
    } catch (DBException ex) {
        if (node.elements("mapping") != null) {
            node.elements("mapping").clear();
        }
        throw new ActionException(ex.getMessage());
    }
    Iterator mappings = node.elements("mapping").iterator();
    while (mappings.hasNext()) {
        Element mapping = (Element) mappings.next();
        String formName = mapping.attributeValue("form");
        String consist = mapping.attributeValue("consist");
        ArrayList<String> colNames = new ArrayList<String>();
        ArrayList<String> colsValue = new ArrayList<String>();
        // ??form?mapping cols??
        if (mapping.elements("enty") == null) {
            throw new ActionException("form mapping");
        }
        Iterator enties = mapping.elements("enty").iterator();
        while (enties.hasNext()) {
            Element entry = (Element) enties.next();
            colNames.add(entry.elementTextTrim("name"));
            colsValue.add(entry.elementTextTrim("content"));
        }
        // ??
        for (Iterator i = shell.getResources().iterator(); i.hasNext();) {
            Resource res = (Resource) i.next();
            // content
            ArrayList<String> colsValueConverted = new ArrayList<String>();
            Iterator<String> valueIterator = colsValue.iterator();
            while (valueIterator.hasNext()) {
                ArrayList<String> valueConverted = PatternHelper.convertAll(valueIterator.next(), res, shell);
                // ? $[xx,xx]
                colsValueConverted.add(valueConverted.get(0));
            }
            // consist?.??
            if (consist != null) {
                int pos = colNames.indexOf(consist);
                if (pos == -1) {
                    Log4j.getInstance().error("Consist???");
                }
                try {
                    String keys[] = { consist };
                    String values[] = { colsValueConverted.get(pos) };
                    if (connection.existed(formName, keys, values)) {
                        Log4j.getInstance()
                                .debug(shell.getName() + " DB : Consist " + " [VAL]:" + values[0]);
                        continue;
                    }
                } catch (DBException ex) {
                    Log4j.getInstance().error(ex.getMessage());
                }
            }

            try {
                int id = connection.insert(formName, colNames, colsValueConverted);
                if (id > 0) {
                    res.putParam("id", +id + "");
                    Log4j.getInstance()
                            .debug(shell.getName() + "[ID] " + id + "  " + formName + " ?");
                }
            } catch (DBException ex) {
                Log4j.getInstance().error(ex.getMessage());
            }

        }
        Log4j.getInstance().info(shell.getName() + "DB " + formName + " ?");
        mapping.detach();
    }

    connection.getConnection().close();

    return shell;
}

From source file:net.techest.railgun.action.ParseActionNode.java

License:Apache License

@Override
public Shell execute(Element node, Shell shell) throws Exception {
    if (node.attribute("method") == null || node.element("rule") == null) {
        throw new ActionException("ParseNode ?rule");
    }//from  w w w  .  j  a  v  a2 s  . c om
    String rule = node.elementTextTrim("rule");
    node.element("rule").detach();
    String returnType = "html";
    if (node.element("returnType") != null) {
        returnType = node.elementTextTrim("returnType");
        node.element("returnType").detach();
    }
    String set = node.elementTextTrim("set");
    if (node.element("set") != null) {
        node.element("set").detach();
    }

    Log4j.getInstance().info("??" + shell.getResources().size());
    LinkedList<Resource> resnew = new LinkedList<Resource>();
    for (Iterator i = shell.getResources().iterator(); i.hasNext();) {
        Resource res = (Resource) i.next();
        if (node.attributeValue("method").equals("dom")) {
            resnew = DomParser.parse(res, rule, set, returnType);
        }
        if (node.attributeValue("method").equals("regxp")) {
            resnew = RegxpParser.parse(res, rule, set, returnType);
        }
    }
    // ?
    Log4j.getInstance().info("??" + resnew.size());
    shell.setResources(resnew);

    return shell;
}

From source file:net.techest.railgun.action.StairActionNode.java

License:Apache License

@Override
public Shell execute(Element node, Shell shell) throws Exception {
    // stair??/*from w w w. j av  a 2 s . c  om*/
    if (node.attribute("fork") != null && node.attribute("fork").getData().toString().equals("true")) {
        Log4j.getInstance().info("Shell Cloned");
        Shell newshell = (Shell) shell.clone();
        shell = newshell;
    }
    // ???
    if (node.attribute("reset") != null && node.attribute("reset").getData().toString().equals("true")) {
        Log4j.getInstance().info("Stair Reset");
        LinkedList<Resource> res = new LinkedList();
        res.add(new Resource());
        shell.setResources(res);
        shell.setClient(new HttpClient());
    }

    return shell;
}

From source file:net.unicon.academus.apps.blogger.access.AcademusAuthorizationProvider.java

License:Open Source License

/**
 * Initialization method for the authorization provider
 *
 * @param servletConfig ServletConfig for obtaining any initialization parameters
 * @param blojsomConfiguration BlojsomConfiguration for blojsom-specific
 * configuration information//  w ww. j  a  v a2  s  . c  om
 * @throws org.blojsom.blog.BlojsomConfigurationException
 *   If there is an error initializing the provider
 */
public void init(ServletConfig servletConfig, BlojsomConfiguration blojsomConfiguration)
        throws BlojsomConfigurationException {
    if (_inited)
        return;

    _servletConfig = servletConfig;

    try {
        // Parse the gateway config file.
        String configPath = blojsomConfiguration.getBaseConfigurationDirectory() + ACADEMUS_CONFIGURATION;

        URL configUrl = _servletConfig.getServletContext().getResource(configPath);

        SAXReader reader = new SAXReader();
        Element configElement = (Element) reader.read(configUrl.toString()).getRootElement();

        Element el = (Element) configElement.selectSingleNode("jndi-ref");
        if (el != null) {
            _jndiName = el.getText();
        }

        // Bootstrap all RDBMS Access Brokers.
        List bsList = configElement.selectNodes("//*[@needsDataSource='true']");
        if (!bsList.isEmpty()) {
            DataSource ds = new AcademusDataSource();

            for (Iterator it = bsList.iterator(); it.hasNext();) {
                Element e = (Element) it.next();

                Attribute impl = e.attribute("impl");
                if (impl == null)
                    throw new IllegalArgumentException("Elements with the 'needsDataSource' attribute "
                            + " must have an 'impl' attribute.");

                Class.forName(impl.getValue()).getDeclaredMethod("bootstrap", new Class[] { DataSource.class })
                        .invoke(null, new Object[] { ds });
            }
        }

        // Get Permissions Access Broker.
        List list = configElement.selectNodes("access-broker");
        if (list == null)
            throw new Exception(
                    "AcademusAuthorizationProvider requires one <access-broker> element per blog instance.");
        else {
            Iterator it = list.iterator();
            while (it.hasNext()) {
                el = (Element) it.next();
                AccessBroker ab = AccessBroker.parse(el);
                String handle = el.attribute("handle").getValue().substring(5);
                _logger.debug("Adding AccessBroker for blog instance '" + handle + "'");
                _brokers.put(handle, ab);
            }
        }

        // Get Academus Facade.
        _facade = AcademusFacadeContainer.retrieveFacade();

    } catch (Throwable e) {
        _logger.debug("Error AcademusAuthorizationProvider::init");
        e.printStackTrace(System.err);
        throw new BlojsomConfigurationException(e);
    }

    _logger.debug("Initialized Academus authorization provider");
    _inited = true;

}

From source file:net.unicon.academus.apps.briefcase.BriefcasePortlet.java

License:Open Source License

public void init(PortletConfig config) {

    // Instance Id.
    id = config.getInitParameter("Id");

    // Initialize.
    StateMachine m = null;//from   www  . j  a va 2 s . c  o m
    try {

        PortletContext ctx = config.getPortletContext();

        // Parse the config file.
        String configPath = (String) config.getInitParameter("configPath");
        SAXReader reader = new SAXReader();
        URL configUrl = ctx.getResource(configPath);
        Element configElement = (Element) reader.read(configUrl.toString()).selectSingleNode("briefcase");

        boolean useXsltc = Boolean.valueOf(configElement.attributeValue("useXsltc"));

        boolean cacheTemplates = true;

        if (configElement.attributeValue("cacheTemplates") != null) {
            cacheTemplates = Boolean.valueOf(configElement.attributeValue("cacheTemplates"));
        }

        // Prep the drive(s).
        List dList = configElement.selectNodes("drive");
        Drive[] drives = new Drive[dList.size()];
        for (int i = 0; i < drives.length; i++) {
            drives[i] = Drive.parse((Element) dList.get(i));
        }

        // read the selector civis factory information
        List civisFactories = new ArrayList();
        ICivisFactory cFac = null;
        Map groupRestrictors = new HashMap();
        Element el = (Element) configElement.selectSingleNode("civis[@id='addressBook']");
        if (el != null) {
            Attribute impl = el.attribute("impl");
            if (impl == null)
                throw new IllegalArgumentException("Element <civis> must have an 'impl'" + " attribute.");
            try {
                cFac = (ICivisFactory) Class.forName(impl.getValue())
                        .getDeclaredMethod("parse", new Class[] { Element.class })
                        .invoke(null, new Object[] { el });

                civisFactories.add(cFac);
            } catch (Throwable t) {
                throw new RuntimeException("Failed to parse the <civis> element.", t);
            }

            // parse for the group restrictor                
            Element gAttr = (Element) el.selectSingleNode("restrictor");
            if (gAttr != null) {
                Attribute a = gAttr.attribute("impl");
                IGroupRestrictor restrictor = (IGroupRestrictor) Class.forName(a.getValue()).newInstance();

                groupRestrictors.put(cFac.getUrl(), restrictor);

            } else {
                throw new IllegalArgumentException("Element <civis> must have an 'restrictor'" + " element.");
            }

        }

        // Construct the application context.
        context = new BriefcaseApplicationContext(drives, id,
                (ICivisFactory[]) (civisFactories.toArray(new ICivisFactory[0])), groupRestrictors);

        // Construct the rendering engine;
        URL xslUrl = ctx.getResource("/rendering/templates/layout.xsl");

        Source trans = new StreamSource(xslUrl.toString());
        IWarlockFactory fac = null;

        if (useXsltc) {
            fac = new XmlWarlockFactory(trans, TransletsConstants.xsltcTransformerFactoryImplementation,
                    TransletsConstants.xsltcDebug, TransletsConstants.xsltcPackage,
                    TransletsConstants.xsltcGenerateTranslet, TransletsConstants.xsltcAutoTranslet,
                    TransletsConstants.xsltcUseClasspath, cacheTemplates);
        } else {
            fac = new XmlWarlockFactory(trans, cacheTemplates);
        }

        // Construct the screens;
        List list = new ArrayList();
        IScreen peephole = null;
        Iterator it = ctx.getResourcePaths("/rendering/screens/briefcase/").iterator();
        while (it.hasNext()) {
            String path = (String) it.next();
            URL screenUrl = ctx.getResource(path);
            Element e = (Element) reader.read(screenUrl.toString()).selectSingleNode("screen");
            IScreen s = fac.parseScreen(e);
            if (s.getHandle().equals("welcome")) {
                peephole = s;
            }
            list.add(s);
        }

        // construct the selector screens
        it = ctx.getResourcePaths("/rendering/screens/selector/").iterator();
        while (it.hasNext()) {
            String path = (String) it.next();
            URL screenUrl = ctx.getResource(path);
            Element e = (Element) reader.read(screenUrl.toString()).selectSingleNode("screen");
            IScreen s = fac.parseScreen(e);
            list.add(s);
        }

        // Build the state machine.
        IScreen[] screens = (IScreen[]) list.toArray(new IScreen[0]);
        m = new StateMachine(context, screens, peephole);

        // initailize a logger for the application
        // read the logging information to activate the specified categories            
        Element loggerElement = (Element) configElement.selectSingleNode("logger");
        if (loggerElement != null) {
            Logger logger = Logger.parse(loggerElement);
            BriefcaseLogger.bootStrap(logger);
        }

    } catch (Throwable t) {
        String msg = "BriefcasePortlet failed to initialize.  See stack " + "trace below.";
        throw new RuntimeException(msg, t);
    }

    initialScreen = m.getScreen(Handle.create("welcome"));

    super.init(id, m);
}

From source file:net.unicon.academus.apps.briefcase.Drive.java

License:Open Source License

public static Drive parse(Element e) {

    // Assertions.
    if (e == null) {
        String msg = "Argument 'e [Element]' cannot be null.";
        throw new IllegalArgumentException(msg);
    }/*  www  .j  av  a  2 s . c o  m*/
    if (!e.getName().equals("drive")) {
        String msg = "Argument 'e [Element]' must be a <drive> element.";
        throw new IllegalArgumentException(msg);
    }

    // Handle.
    Attribute h = e.attribute("handle");
    if (h == null) {
        String msg = "Element <drive> is missing required attribute " + "'handle'.";
        throw new IllegalArgumentException(msg);
    }
    String handle = h.getValue();

    // MaxFileSize.
    long maxFileSize = 0L;
    Attribute x = e.attribute("max-upload");
    // NB:  Attribute 'max-upload' is optional.
    if (x != null) {
        try {
            maxFileSize = Long.parseLong(x.getValue());
        } catch (Throwable t) {
            String msg = "Attribute 'max-upload' must be a valid integer.";
            throw new RuntimeException(msg, t);
        }
    }

    // Large Icon.
    Attribute lgi = e.attribute("large-icon");
    if (lgi == null) {
        String msg = "Element <drive> is missing required attribute " + "'large-icon'.";
        throw new IllegalArgumentException(msg);
    }
    String largeIcon = lgi.getValue();

    // Open Icon.
    Attribute opi = e.attribute("open-icon");
    if (opi == null) {
        String msg = "Element <drive> is missing required attribute " + "'open-icon'.";
        throw new IllegalArgumentException(msg);
    }
    String openIcon = opi.getValue();

    // Closed Icon.
    Attribute cli = e.attribute("closed-icon");
    if (cli == null) {
        String msg = "Element <drive> is missing required attribute " + "'closed-icon'.";
        throw new IllegalArgumentException(msg);
    }
    String closedIcon = cli.getValue();

    // Label.
    Element l = (Element) e.selectSingleNode("label");
    if (l == null) {
        String msg = "Element <drive> is missing required child element " + "<label>.";
        throw new IllegalArgumentException(msg);
    }
    String label = l.getText();

    // Description.
    Element d = (Element) e.selectSingleNode("description");
    if (d == null) {
        String msg = "Element <drive> is missing required child element " + "<description>.";
        throw new IllegalArgumentException(msg);
    }
    String description = d.getText();

    // ShareTarget.
    Attribute s = e.attribute("share-target");
    String shareTarget = null;
    if (s != null) {
        // Not provided means don't share.
        shareTarget = s.getValue();
    }

    // Broker.
    Element b = (Element) e.selectSingleNode("access-broker");
    if (b == null) {
        String msg = "Element <drive> is missing required child element " + "<access-broker>.";
        throw new IllegalArgumentException(msg);
    }
    AccessBroker broker = AccessBroker.parse(b);

    return new Drive(handle, maxFileSize, largeIcon, openIcon, closedIcon, label, description, shareTarget,
            broker);

}

From source file:net.unicon.academus.apps.briefcase.engine.ChangeFolderPageAction.java

License:Open Source License

public static IAction parse(Element e, IWarlockFactory owner) throws WarlockException {

    // Assertions.
    if (e == null) {
        String msg = "Argument 'e [Element]' cannot be null.";
        throw new IllegalArgumentException(msg);
    }/*from w  ww  .j a  v a 2  s  .c o m*/
    if (!e.getName().equals("action")) {
        String msg = "Argument 'e [Element]' must be an <action> element.";
        throw new IllegalArgumentException(msg);
    }
    if (owner == null) {
        String msg = "Argument 'owner+' cannot be null.";
        throw new IllegalArgumentException(msg);
    }

    // Handle.
    Attribute h = e.attribute("handle");
    if (h == null) {
        String msg = "Element <action> is missing required attribute " + "'handle'.";
        throw new XmlFormatException(msg);
    }
    Handle handle = Handle.create(h.getValue());

    // Choices.
    String[] choices = new String[0];
    Attribute p = e.attribute("inpt");
    if (p != null) {
        StringTokenizer tokens = new StringTokenizer(p.getValue(), ",");
        choices = new String[tokens.countTokens()];
        for (int i = 0; tokens.hasMoreTokens(); i++) {
            choices[i] = tokens.nextToken();
        }
    }

    // Move.
    Attribute m = e.attribute("move");
    if (m == null) {
        String msg = "Element <action> is missing required attribute " + "'mode'.";
        throw new XmlFormatException(msg);
    }
    PageChange change = PageChange.getInstance(m.getValue());

    return new ChangeFolderPageAction(owner, handle, choices, change);

}

From source file:net.unicon.academus.apps.briefcase.engine.ChangeFolderSortAction.java

License:Open Source License

public static IAction parse(Element e, IWarlockFactory owner) throws WarlockException {

    // Assertions.
    if (e == null) {
        String msg = "Argument 'e [Element]' cannot be null.";
        throw new IllegalArgumentException(msg);
    }//from w  w w  .  j av a  2 s  .c om
    if (!e.getName().equals("action")) {
        String msg = "Argument 'e [Element]' must be an <action> element.";
        throw new IllegalArgumentException(msg);
    }
    if (owner == null) {
        String msg = "Argument 'owner+' cannot be null.";
        throw new IllegalArgumentException(msg);
    }

    // Handle.
    Attribute h = e.attribute("handle");
    if (h == null) {
        String msg = "Element <action> is missing required attribute " + "'handle'.";
        throw new XmlFormatException(msg);
    }
    Handle handle = Handle.create(h.getValue());

    // Choices.
    String[] choices = new String[0];
    Attribute p = e.attribute("inpt");
    if (p != null) {
        StringTokenizer tokens = new StringTokenizer(p.getValue(), ",");
        choices = new String[tokens.countTokens()];
        for (int i = 0; tokens.hasMoreTokens(); i++) {
            choices[i] = tokens.nextToken();
        }
    }

    // Mode.
    Attribute m = e.attribute("mode");
    if (m == null) {
        String msg = "Element <action> is missing required attribute " + "'mode'.";
        throw new XmlFormatException(msg);
    }
    String mode = m.getValue();

    return new ChangeFolderSortAction(owner, handle, choices, mode);

}

From source file:net.unicon.academus.apps.briefcase.engine.ChangeShareeViewAction.java

License:Open Source License

public static IAction parse(Element e, IWarlockFactory owner) throws WarlockException {

    // Assertions.
    if (e == null) {
        String msg = "Argument 'e [Element]' cannot be null.";
        throw new IllegalArgumentException(msg);
    }//  w w  w .  j  a va 2  s .  c om
    if (!e.getName().equals("action")) {
        String msg = "Argument 'e [Element]' must be an <action> element.";
        throw new IllegalArgumentException(msg);
    }
    if (owner == null) {
        String msg = "Argument 'owner+' cannot be null.";
        throw new IllegalArgumentException(msg);
    }

    // Handle.
    Attribute h = e.attribute("handle");
    if (h == null) {
        String msg = "Element <action> is missing required attribute " + "'handle'.";
        throw new XmlFormatException(msg);
    }
    Handle handle = Handle.create(h.getValue());

    // Choices.
    String[] choices = new String[0];
    Attribute p = e.attribute("inpt");
    if (p != null) {
        StringTokenizer tokens = new StringTokenizer(p.getValue(), ",");
        choices = new String[tokens.countTokens()];
        for (int i = 0; tokens.hasMoreTokens(); i++) {
            choices[i] = tokens.nextToken();
        }
    }

    return new ChangeShareeViewAction(owner, handle, choices);

}

From source file:net.unicon.academus.apps.briefcase.engine.ChangeUserPrefPageAction.java

License:Open Source License

public static IAction parse(Element e, IWarlockFactory owner) throws WarlockException {

    // Assertions.
    if (e == null) {
        String msg = "Argument 'e [Element]' cannot be null.";
        throw new IllegalArgumentException(msg);
    }//from w w w . j  a  v a2s .  co  m
    if (!e.getName().equals("action")) {
        String msg = "Argument 'e [Element]' must be an <action> element.";
        throw new IllegalArgumentException(msg);
    }
    if (owner == null) {
        String msg = "Argument 'owner+' cannot be null.";
        throw new IllegalArgumentException(msg);
    }

    // Handle.
    Attribute h = e.attribute("handle");
    if (h == null) {
        String msg = "Element <action> is missing required attribute " + "'handle'.";
        throw new XmlFormatException(msg);
    }
    Handle handle = Handle.create(h.getValue());

    // ToScreen.
    Attribute s = e.attribute("to-screen");
    if (s == null) {
        String msg = "Element <action> is missing required attribute " + "'to-screen'.";
        throw new XmlFormatException(msg);
    }
    String toScreen = s.getValue();

    // Choices.
    String[] choices = new String[0];
    Attribute p = e.attribute("inpt");
    if (p != null) {
        StringTokenizer tokens = new StringTokenizer(p.getValue(), ",");
        choices = new String[tokens.countTokens()];
        for (int i = 0; tokens.hasMoreTokens(); i++) {
            choices[i] = tokens.nextToken();
        }
    }

    // Move.
    Attribute m = e.attribute("move");
    if (m == null) {
        String msg = "Element <action> is missing required attribute " + "'mode'.";
        throw new XmlFormatException(msg);
    }
    PageChange change = PageChange.getInstance(m.getValue());

    return new ChangeUserPrefPageAction(owner, handle, choices, change, toScreen);

}