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:net.unicon.academus.apps.messaging.engine.SendMessageAction.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 ww w  .j  a v  a2  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)
        throw new XmlFormatException("ComposeNavigateAction requires attribute 'inpt'.");
    choices = p.getValue().split(",");

    return new SendMessageAction(owner, handle, choices);

}

From source file:net.unicon.academus.apps.messaging.engine.SetMessageItemsPerPageAction.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 ww w.  ja 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) {
        choices = p.getValue().split(",");
    }

    return new SetMessageItemsPerPageAction(owner, handle, choices);

}

From source file:net.unicon.academus.apps.messaging.engine.SetReportItemsPerPageAction.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 ww w.  j  a  va 2 s  .  com*/
    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) {
        choices = p.getValue().split(",");
    }

    return new SetReportItemsPerPageAction(owner, handle, choices);

}

From source file:net.unicon.academus.apps.messaging.engine.SetViewModeAction.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);
    }/*  ww  w . j  ava 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) {
        choices = p.getValue().split(",");
    }

    return new SetViewModeAction(owner, handle, choices);

}

From source file:net.unicon.academus.apps.messaging.engine.UploadAction.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  ww w  . ja  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) {
        choices = p.getValue().split(",");
    }

    return new UploadAction(owner, handle, choices);

}

From source file:net.unicon.academus.apps.messaging.engine.WelcomePageAction.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 ww.j  a  va  2s.  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) {
        choices = p.getValue().split(",");
    }

    return new WelcomePageAction(owner, handle, choices);

}

From source file:net.unicon.academus.apps.messaging.ImportExportHelper.java

License:Open Source License

/**
 * Parse an XML fragment containing a message.
 * @param e Element object representing the <code>&lt;message&gt;</code>
 *          element.//  www .  jav a 2  s .  c  o  m
 * @param facs Civis factories used to validate users and groups
 * @param gexpand Flag to indicate whether groups should be expanded into
 *                their users, or left as groups.
 * @return IMessage representing the given XML element
 */
public static DraftMessage parse(Element e, ICivisFactory[] facs) throws MercuryException {
    // Assertions.
    if (e == null) {
        throw new IllegalArgumentException("Argument 'e [Element]' cannot be null.");
    }
    if (!e.getName().equals("message")) {
        throw new XmlFormatException("Argument 'e [Element]' must be a <message> element.");
    }

    DraftMessage msg = new DraftMessage();
    String buf = null;
    Attribute recipType = null;
    Element e2 = null;
    List list = null;

    // Priority.
    e2 = (Element) e.selectSingleNode("priority");
    if (e2 != null) {
        String tmp = e2.getText();
        Priority p = null;
        try {
            p = Priority.getInstance(tmp);
        } catch (IllegalArgumentException ex) {
            try {
                p = Priority.getInstance(Integer.parseInt(tmp));
            } catch (Exception ex2) {
                p = null;
            }
        }

        if (p == null) {
            throw new XmlFormatException("Illegal priority specified: " + tmp);
        }

        msg.setPriority(p);
    }

    // Recipient.
    list = e.elements("recipient");
    Element recipElement = null;
    Element eType = null;
    Element recipId = null;
    for (Iterator it = list.iterator(); it.hasNext();) {
        recipElement = (Element) it.next();
        recipType = recipElement.attribute("type");

        // get the id 
        recipId = (Element) recipElement.selectSingleNode("id");
        if (recipId == null) {
            throw new XmlFormatException("Missing Element <id> under <recipient>");
        }

        // get the entityType
        eType = (Element) recipElement.selectSingleNode("entity-type");
        if (eType == null) {
            throw new XmlFormatException(
                    "Missing Element <entity-type> under <recipient> for entity " + recipId.getText());
        }

        // entity type is a group
        if (eType.getTextTrim().equalsIgnoreCase(EntityType.GROUP.toString())) {
            // If we have factories to check with, make sure they are valid
            // groups.
            if (facs != null && facs.length > 0) {
                IGroup group = findGroup(facs, recipId.getText());
                if (group == null) {
                    throw new RuntimeException("Could not find the specified group: " + recipId.getText());
                }
            }

            // Don't expand groups -- it is handled by the message factory.
            msg.addRecipient(recipType.getValue(), recipId.getText(), recipId.getText(), EntityType.GROUP);

        } else if (eType.getTextTrim().equalsIgnoreCase(EntityType.USER.toString())) {
            Element eLabel = (Element) recipElement.selectSingleNode("label");
            String uid = recipId.getText();
            String name = uid;

            if (eLabel != null)
                name = eLabel.getText();

            name = validateUsername(facs, uid, name);

            msg.addRecipient(recipType.getValue(), name, uid, EntityType.USER);
        } else {
            throw new XmlFormatException("Illegal value for <entity-type> element: " + eType.getText());
        }
    }

    // Subject.
    e2 = (Element) e.selectSingleNode("subject");
    if (e2 != null)
        msg.setSubject(e2.getText());
    else
        throw new XmlFormatException("Missing element <subject>: Message subject required.");

    // Body.
    e2 = (Element) e.selectSingleNode("body");
    if (e2 != null) {
        Element e3 = (Element) e2.selectSingleNode("html");
        if (e3 != null)
            msg.setBody(e3.asXML());
        else
            msg.setBody(e2.getText());
    } else
        throw new XmlFormatException("Missing element <body>: Message body required.");

    //e2 = (Element)e.selectSingleNode("expires");

    return msg;
}

From source file:net.unicon.academus.apps.messaging.MessageCenter.java

License:Open Source License

public MessageCenter(Element e) {
    Attribute attrId = e.attribute("id");
    if (attrId == null)
        throw new IllegalArgumentException("Element <message-center> must have an 'id' attribute");
    this.id = attrId.getValue();

    Element e2 = (Element) e.selectSingleNode("label");
    if (e2 == null) {
        throw new IllegalArgumentException("Element <message-center> must contain a <label> element.");
    } else {//  w  w  w.  ja v a  2s  .  c o m
        this.label = e2.getText();
    }

    e2 = (Element) e.selectSingleNode("description");
    if (e2 == null) {
        throw new IllegalArgumentException("Element <message-center> must contain a <description> element.");
    } else {
        this.description = e2.getText();
    }

    e2 = (Element) e.selectSingleNode("access-broker");
    if (e2 == null) {
        throw new IllegalArgumentException("Element <message-center> must contain a <access-broker> element.");
    } else {
        this.broker = AccessBroker.parse(e2);
    }
}

From source file:net.unicon.academus.apps.messaging.MessagingPortlet.java

License:Open Source License

public void init(PortletConfig config) {

    // Instance Id.
    id = config.getInitParameter("Id");
    session_key = id + ":CONTEXT";

    // Initialize.
    StateMachine m = null;/*from w  ww.ja  v a 2 s .co  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("messaging");

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

        boolean cacheTemplates = true;

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

        configElement = ConfigHelper.handle(configElement);

        Element el = (Element) configElement.selectSingleNode("query-file");
        if (el == null) {
            throw new IllegalArgumentException("Unable to locate required <query-file> element");
        }
        RdbmsMessageFactoryCreator.bootstrap(el.getText());

        IMessageFactory systemEmail = null;

        // Look for the system email account
        el = (Element) configElement.selectSingleNode("message-factory[@id='system']");
        if (el != null) {
            Attribute impl = el.attribute("impl");
            if (impl == null)
                throw new IllegalArgumentException(
                        "Element <message-factory> must have an 'impl'" + " attribute.");
            try {
                systemEmail = (IMessageFactory) Class.forName(impl.getValue())
                        .getDeclaredMethod("parse", new Class[] { Element.class })
                        .invoke(null, new Object[] { el });
            } catch (Throwable t) {
                throw new RuntimeException("Failed to parse the <message-factory> element.", t);
            }
        }

        // Parse the deployment default factories; these are provider
        // per-user by an access broker as defined in the configuration
        // file.
        globalInstances = new HashMap();
        List mList = configElement.selectNodes("message-center");
        for (Iterator mIt = mList.iterator(); mIt.hasNext();) {
            Element e = (Element) mIt.next();
            Attribute id = e.attribute("id");
            if (id == null)
                throw new IllegalArgumentException("Element <message-center> must have an 'id' attribute");
            globalInstances.put(id.getValue(), new MessageCenter(e));
        }

        // Upload Limit setting
        mList = configElement.selectNodes("upload-limit");
        long ulimit = 0;
        if (!mList.isEmpty()) {
            String buf = ((Element) mList.get(0)).getText();
            Pattern p = Pattern.compile("([0-9.]+)([MmKkBb])?");
            Matcher mt = p.matcher(buf);
            if (mt.matches()) {
                double td = Double.parseDouble(mt.group(1));
                buf = mt.group(2);
                if (buf == null || buf.equals("") || buf.equalsIgnoreCase("b"))
                    ulimit = (long) td;
                else if (buf.equalsIgnoreCase("k"))
                    ulimit = (long) (td * 1024);
                else if (buf.equalsIgnoreCase("m"))
                    ulimit = (long) (td * 1024 * 1024);
            }
        }

        boolean xhtml = false;
        Attribute attr = configElement.attribute("allow-xhtml");
        if (attr != null)
            xhtml = Boolean.valueOf(attr.getValue()).booleanValue();

        //  peephole setting
        Element tElement = (Element) configElement.selectSingleNode("peephole/type");
        Element mElement = (Element) configElement.selectSingleNode("peephole/message-limit");
        int msgLimit = PEEPHOLE_MESSAGE_LIMIT; // user default value if element not specified
        String peepholeType = null;
        if (tElement != null) {
            peepholeType = tElement.getText();
        } else {
            throw new RuntimeException(
                    "Element <peephole><type> is missing inside" + "the 'messaging' element.");
        }
        if (mElement != null) {
            msgLimit = Integer.parseInt(mElement.getText());
        }

        // Civis factory for email lookups (Send copy via email)
        ICivisFactory addressBook = null;
        Map groupRestrictors = new HashMap();
        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 {
                addressBook = (ICivisFactory) Class.forName(impl.getValue())
                        .getDeclaredMethod("parse", new Class[] { Element.class })
                        .invoke(null, new Object[] { el });
            } 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(addressBook.getUrl(), restrictor);

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

        }

        // Parsing name mappings for notifications callback
        Map callbacks = new HashMap();
        el = (Element) configElement.selectSingleNode("callbacks");
        if (el != null) {
            List l = el.selectNodes("entry");
            Iterator it = l.iterator();
            while (it.hasNext()) {
                Element en = (Element) it.next();

                Element name = (Element) en.selectSingleNode("name");
                Element loc = (Element) en.selectSingleNode("location");

                if (name == null || loc == null)
                    throw new IllegalArgumentException("callbacks/entry elements must contain both a "
                            + "<name> and <location> declaration.");

                callbacks.put(name.getText(), loc.getText());
            }
        } // else, no mappings.

        // Construct the application context.
        context = new MessagingApplicationContext(id, systemEmail, addressBook, ulimit, groupRestrictors,
                peepholeType, msgLimit, callbacks, xhtml);

        // 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 screenList = new ArrayList();
        List libList = new ArrayList();
        Iterator it = ctx.getResourcePaths("/rendering/screens/messaging/").iterator();
        while (it.hasNext()) {
            String path = (String) it.next();
            URL screenUrl = ctx.getResource(path);
            Element e = (Element) reader.read(screenUrl.toString()).getRootElement();
            if (e.getName().equals("screen")) {
                screenList.add(e);
            } else if (e.getName().equals("library")) {
                libList.add(e);
            }
        }

        // 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()).getRootElement();
            if (e.getName().equals("screen")) {
                screenList.add(e);
            } else if (e.getName().equals("library")) {
                libList.add(e);
            }
        }

        IScreen peephole = null;
        Element[] libs = (Element[]) libList.toArray(new Element[0]);
        IScreen[] screens = new IScreen[screenList.size()];
        for (int i = 0; i < screens.length; i++) {
            Element e = (Element) screenList.get(i);
            IScreen s = fac.parseScreen(e, libs);
            if (s.getHandle().getValue().endsWith(peepholeType)) {
                peephole = s;
            }
            screens[i] = s;
        }

        // Build the state machine.
        m = new StateMachine(context, screens, peephole);

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

    initialScreen = m.getPeephole();

    super.init(id, m);

}

From source file:net.unicon.academus.apps.messaging.RdbmsMessageFactoryCreator.java

License:Open Source License

/**
 * Returns an instance of the class by parsing the <code>Element</code>.
 * @param e XML <code>Element</code>
 * @return an instance of the class//w ww  . j  av a2  s.c o m
 */
public static ICreator parse(Element e) {

    // Assertions.
    if (e == null) {
        String msg = "Argument 'e [Element]' cannot be null.";
        throw new IllegalArgumentException(msg);
    }
    if (!e.getName().equals("creator")) {
        String msg = "Argument 'e [Element]' must be a <creator> " + "element.";
        throw new IllegalArgumentException(msg);
    }

    Attribute t = e.attribute("handle");
    if (t == null)
        throw new IllegalArgumentException("Element <creator> must have an attribute 'handle'.");
    String configName = t.getValue();

    t = e.attribute("group-trigger");
    if (t == null)
        throw new IllegalArgumentException("Element <creator> must have an attribute 'group-trigger'.");
    boolean active = Boolean.valueOf(t.getValue()).booleanValue();

    // get the root attachment folder
    Element el = (Element) e.selectSingleNode("attachment-path");
    if (el == null)
        throw new IllegalArgumentException(
                "Element <creator> needs to have a " + "required element <attachment-path>.");

    String attachPath = el.getText();
    if (attachPath.equals(""))
        throw new IllegalArgumentException(
                "Element <attachment-path> must have a valid file " + "system path.");

    // parse the numbers of folder hashing layers from the XML config
    el = (Element) e.selectSingleNode("hashing-layers");
    int layers = 3;
    if (el != null) {
        layers = Integer.parseInt(el.getText());
    }

    int expiration = 0;
    // parse the numbers of folder hashing layers from the XML config
    el = (Element) e.selectSingleNode("message-expiration");
    if (el != null) {
        expiration = Integer.parseInt(el.getText());
    }

    // Civis factory for email lookups (Send copy via email) and group breakups
    ICivisFactory addressBook = null;
    el = (Element) e.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 {
            addressBook = (ICivisFactory) Class.forName(impl.getValue())
                    .getDeclaredMethod("parse", new Class[] { Element.class })
                    .invoke(null, new Object[] { el });
        } catch (Exception ex) {
            // Allowed to fail in the case of BatchImporter.
        }
    }

    return new RdbmsMessageFactoryCreator(configName, attachPath, layers, expiration, active, addressBook);
}