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.unicon.alchemist.access.rdbms.RdbmsAccessBroker.java

License:Open Source License

public static AccessBroker parse(Element e) {

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

    // Call parse() in the specified implementation.
    Attribute impl = e.attribute("impl");

    Attribute handle = e.attribute("handle");
    String name = handle.getText();

    // AccessTypes lookup
    Element el = (Element) e.selectSingleNode("access");
    if (el == null)
        throw new IllegalArgumentException("RdbmsAccessBroker requires the <access> element.");
    impl = el.attribute("impl");
    if (impl == null)
        throw new IllegalArgumentException("The element <access> must contain an 'impl' attribute.");
    AccessType[] accessTypes = null;
    try {
        accessTypes = (AccessType[]) Class.forName(impl.getValue()).getMethod("getInstances", null).invoke(null,
                null);
    } catch (ClassNotFoundException ex1) {
        throw new RuntimeException("Could not find the class " + impl.getValue(), ex1);
    } catch (NoSuchMethodException ex2) {
        throw new RuntimeException("Could not find the Method 'getInstances' on class " + impl.getValue(), ex2);
    } catch (Exception ex3) {
        throw new RuntimeException("Unable to execute Method 'getInstances' on class " + impl.getValue(), ex3);
    }
    if (accessTypes == null || accessTypes.length == 0)
        throw new IllegalArgumentException("No AccessTypes found from <access> element declaration.");

    return getInstance(getDataSource(), name, accessTypes, true, e);

}

From source file:net.unicon.alchemist.encrypt.EncryptionService.java

License:Open Source License

/**
 * Initialize the service instances from the configuration file.
 *///from  w w  w  .ja  v  a  2s.c o m
private static Map createInstances() {
    Map serviceInstances = new HashMap();

    try {
        URL configUrl = EncryptionService.class.getResource(CONFIGURATION_FILE);

        if (configUrl == null)
            throw new IllegalArgumentException("Unable to locate encryption service configuration.");

        Element configElement = (Element) (new SAXReader()).read(configUrl.toString())
                .selectSingleNode("encryption-services");
        ;

        SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");

        List eList = configElement.selectNodes("encryption-instance");
        Iterator it = eList.iterator();
        while (it.hasNext()) {
            Element e = (Element) it.next();
            Attribute attr = e.attribute("encrypt");
            boolean encrypt = (attr == null || !attr.getValue().equalsIgnoreCase("false"));
            String appName = e.selectSingleNode("name").getText();
            String keyStr = e.selectSingleNode("key").getText();

            if (keyStr == null || keyStr.length() < 8)
                throw new IllegalArgumentException("<key> must be at least 8 characters.");

            Key key = skf.generateSecret(new DESKeySpec(keyStr.getBytes()));

            serviceInstances.put(appName, new EncryptionService(encrypt, key));
        }

    } catch (Exception ex) {
        throw new RuntimeException("Failed to initialize EncryptionService", ex);
    }

    return Collections.unmodifiableMap(serviceInstances);
}

From source file:net.unicon.alchemist.rdbms.QueryManager.java

License:Open Source License

private Map loadQueries(String src, String module) {
    Map rslt = new HashMap();

    StringBuffer buf = new StringBuffer();
    buf.append("//module[@name='").append(module).append("']");

    if (log.isInfoEnabled()) {
        log.info("Loading query file " + src + " for module " + module);
    }/*from  www  .  j  a  v a 2s  .  c o  m*/
    InputStream in = null;
    try {
        in = Thread.currentThread().getContextClassLoader().getResourceAsStream(src);
        if (in == null) {
            in = QueryManager.class.getResourceAsStream(src);
        }
        if (in == null) {
            throw new RuntimeException("Unable to locate resource: " + src);
        }
        Document doc = new SAXReader().read(in);
        Element e = (Element) doc.getRootElement().selectSingleNode(buf.toString());
        List q = e.elements("query");
        for (Iterator it = q.iterator(); it.hasNext();) {
            Element el = (Element) it.next();
            String name = el.attribute("name").getValue();
            rslt.put(name, el.getText());
        }
    } catch (Exception ex) {
        throw new RuntimeException("Failed to load queries for module: " + module, ex);
    } finally {
        if (in != null)
            try {
                in.close();
            } catch (IOException ex) {
                log.error(ex.getMessage(), ex);
            }
    }

    return rslt;
}

From source file:net.unicon.alchemist.rdbms.Sequencer.java

License:Open Source License

public static Sequencer parse(Element e) {

    // Assertions.
    if (e == null) {
        String msg = "Argument 'e [Element]' cannot be null.";
        throw new IllegalArgumentException(msg);
    }// ww w .  j  a  v a2  s.  com
    if (!e.getName().equals("sequencer")) {
        String msg = "Argument 'e' must be a <sequencer> element.";
        throw new IllegalArgumentException(msg);
    }
    if (defaultDataSource == null) {
        String msg = "The sequencer component must be bootsraped before " + "the parse method may be called.";
        throw new IllegalStateException(msg);
    }

    // Name.
    Attribute n = e.attribute("name");
    if (n == null) {
        String msg = "Element <sequencer> is missing required attribute " + "'name'.";
        throw new IllegalArgumentException(msg);
    }
    String name = n.getValue();

    // CacheSize.
    Attribute z = e.attribute("cache-size");
    if (z == null) {
        String msg = "Element <sequencer> is missing required attribute " + "'cache-size'.";
        throw new IllegalArgumentException(msg);
    }
    int cacheSize = 0;
    try {
        cacheSize = Integer.parseInt(z.getValue());
    } catch (NumberFormatException nfe) {
        String msg = "Attribute " + "'cache-size'.";
        throw new IllegalArgumentException(msg);
    }

    return new Sequencer(defaultDataSource, name, cacheSize);

}

From source file:net.unicon.civis.fac.AbstractCivisFactory.java

License:Open Source License

public static ICivisFactory parse(Element e) {

    // Assertions.
    if (e == null) {
        String msg = "Argument 'e [Element]' cannot be null.";
        throw new IllegalArgumentException(msg);
    }//from w  w w  .j a va2 s  .c  o  m
    if (!e.getName().equals("civis-factory")) {
        String msg = "Argument 'e [Element]' must be a <civis-factory> " + "element.";
        throw new IllegalArgumentException(msg);
    }

    // Impl.
    Attribute p = e.attribute("impl");
    if (p == null) {
        String msg = "Element <civis-factory> is missing required " + "attribute 'impl'.";
        throw new IllegalArgumentException(msg);
    }
    String impl = p.getValue();

    ICivisFactory rslt = null;
    try {

        Class c = Class.forName(impl);
        Method m = c.getDeclaredMethod("parse", new Class[] { Element.class });
        rslt = (ICivisFactory) m.invoke(null, new Object[] { e });

    } catch (Throwable t) {
        String msg = "Unable to evaluate the specified factory:  " + impl;
        throw new RuntimeException(msg, t);
    }

    return rslt;

}

From source file:net.unicon.mercury.DraftMessage.java

License:Open Source License

/**
 * Parse an XML fragment containing a message.
 * @param e Element object representing the <code>&lt;message&gt;</code>
 *          element.//from   ww w.  j  a  v  a 2  s.c om
 * @return IMessage representing the given XML element
 */
public static DraftMessage parse(Element e) throws MercuryException {
    // Assertions.
    assert e != null : "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 t = null;
    Attribute et = null;
    List list = null;

    // Priority.
    list = e.elements("priority");
    if (!list.isEmpty())
        msg.priority = Priority.getInstance(Integer.parseInt(((Element) list.get(0)).getText()));

    // Recipient.
    list = e.elements("recipient");
    for (Iterator it = list.iterator(); it.hasNext();) {
        Element el = (Element) it.next();
        t = el.attribute("type");
        et = el.attribute("entity-type");
        msg.addRecipient(t.getValue(), AddressImpl.parse((Element) el.selectSingleNode("address")),
                EntityType.getInstance(et.getValue()));
    }

    // Subject.
    list = e.elements("subject");
    if (!list.isEmpty())
        msg.subject = ((Element) list.get(0)).getText();

    // Body.
    list = e.elements("body");
    if (!list.isEmpty())
        msg.body = ((Element) list.get(0)).getText();
    /*
            list = e.elements("body");
            if (!list.isEmpty()) {
    Element b = (Element)list.get(0);
    b = b.createCopy();
    b.setName("xhtml");
    msg.body = b.asXML();
            }
    */

    //list = e.elements("expires");

    return msg;
}

From source file:net.unicon.mercury.fac.AddressImpl.java

License:Open Source License

public static IAddress parse(Element e) throws MercuryException {
    String label = null;//from   w w  w  . jav  a 2s . co  m
    String nativeFormat = null;

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

    // Native-format attribute.
    Attribute t = e.attribute("native-format");
    if (t == null) {
        String msg = "Element <address> is missing required " + "attribute 'native-format'";
        throw new XmlFormatException(msg);
    }
    nativeFormat = t.getValue();

    // Label.
    List list = e.elements("label");
    if (list.size() == 1)
        label = ((Element) list.get(0)).getText();
    else
        label = nativeFormat;

    return new AddressImpl(label, nativeFormat);
}

From source file:net.unicon.mercury.fac.email.EmailAccount.java

License:Open Source License

/**
 * Establish an EmailAccount based on an XML fragment.
 * @param e Root element of the XML fragment.
 * @return EmailAccount object representing the given XML fragment.
 * @throws MercuryException if any part of the parsing process fails.
 *//* ww  w . ja v a  2  s .  c o m*/
public static EmailAccount parse(Element e) throws MercuryException {
    assert e != null;
    String tmp = null;
    EmailStoreAccount storeacct = null;
    EmailTransportAccount transportacct = null;

    Element el;
    Attribute t;

    try {
        // Store
        el = (Element) e.selectSingleNode("account[@type='store']");
        if (el != null) {
            t = el.attribute("impl");
            if (t == null)
                throw new XmlFormatException("Element <account> must have an attribute 'impl'");
            storeacct = (EmailStoreAccount) Class.forName(t.getValue()).getConstructor(new Class[0])
                    .newInstance(new Object[0]);

            storeacct.parse(el);
        }

        // Transport
        el = (Element) e.selectSingleNode("account[@type='transport']");
        if (el != null) {
            t = el.attribute("impl");
            if (t == null)
                throw new XmlFormatException("Element <account> must have an attribute 'impl'");
            transportacct = (EmailTransportAccount) Class.forName(t.getValue()).getConstructor(new Class[0])
                    .newInstance(new Object[0]);

            transportacct.parse(el);
        }
    } catch (Exception ex) {
        throw new MercuryException("Error instansiating EmailMessageFactory.", ex);
    }

    return new EmailAccount(storeacct, transportacct);
}

From source file:net.unicon.mercury.fac.email.EmailRecipient.java

License:Open Source License

/**
 * Parse a recipient from an XML fragment.
 * @param e The XML fragment to parse./*from  www .j a  v  a2  s .  c o  m*/
 * @return The resultant recipient object.
 * @throws MercuryException if the parsing fails
 */
public static IRecipient parse(Element e) throws MercuryException {
    IRecipientType type;
    IAddress address;

    assert e != null : "Argument 'e [Element]' cannot be null.";

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

    // Type attribute.
    Attribute t = e.attribute("type");
    if (t == null) {
        String msg = "Element <recipient> is missing required " + "attribute 'type'";
        throw new XmlFormatException(msg);
    }
    type = EmailRecipientType.getType(t.getValue());

    // Address.
    List list = e.elements("address");
    if (list.size() != 1) {
        String msg = "Exactly one <address> element required per " + "recipient.";
        throw new XmlFormatException(msg);
    }
    address = AddressImpl.parse(((Element) list.get(0)));

    return new EmailRecipient(address, type);
}

From source file:net.unicon.mercury.fac.email.JavaMailAccount.java

License:Open Source License

/**
 * Configure the account based on an XML fragment.
 * @param e Element representing the top node of the XML fragment.
 *//*from   w  ww .jav  a2  s  . c  o m*/
public void parse(Element e) throws MercuryException {
    Element e2 = (Element) e.selectSingleNode("host");
    if (e2 == null)
        throw new XmlFormatException("Element <host> required.");
    setHostname(e2.getText());

    e2 = (Element) e.selectSingleNode("port");
    if (e2 != null)
        setPort(Integer.parseInt(e2.getText()));

    e2 = (Element) e.selectSingleNode("username");
    if (e2 != null)
        setUsername(e2.getText());

    e2 = (Element) e.selectSingleNode("password");
    if (e2 != null)
        setPassword(e2.getText());

    Attribute t = e.attribute("timeout");
    if (t != null)
        setTimeout(Integer.parseInt(t.getValue()));
}