List of usage examples for org.dom4j Attribute getValue
String getValue();
From source file:net.unicon.alchemist.access.AccessBroker.java
License:Open Source License
/** * Constructs a new <code>AccessBroker</code> using information from the * specified XML.// w w w. j av a 2 s . c o m * * @param e XML that defines the desired broker. * @return A broker with the characteristics specified in the XML. */ protected AccessBroker(Element e) { // Assertions. if (e == null) { String msg = "Argument 'e [Element]' cannot be null."; throw new IllegalArgumentException(msg); } if (!e.getName().equals("access-broker")) { String msg = "Argument 'e [Element]' must be an <access-broker> " + "element."; throw new IllegalArgumentException(msg); } // Handle. Attribute h = e.attribute("handle"); if (h == null) { String msg = "Element <access-broker> is missing required " + "attribute 'handle'."; throw new IllegalArgumentException(msg); } String val = h.getValue().trim(); if (val.length() == 0) { String msg = "Attribute 'handle' cannot be zero-length or contain " + "only whitespace."; throw new IllegalArgumentException(msg); } this.handle = val; // Add to collection. instances.put(val, this); }
From source file:net.unicon.alchemist.access.AccessRule.java
License:Open Source License
/** * Returns an array of AccessRules from parsing the given XML. * // w w w . j av a 2s . com * @param e * Format of the xml * <access impl="net.unicon.academus.apps.briefcase.BriefcaseAccessType"> * <type handle="READ" value="GRANT"/> * <type handle="EDIT" value="GRANT"/> * </access> * @return An array of AccessRule objects. */ public static AccessRule[] parse(Element e) { // assertions if (e == null) { throw new IllegalArgumentException("Argument 'e' cannot be null."); } if (!e.getName().equalsIgnoreCase("access")) { throw new IllegalArgumentException("Argument 'e' should be ab element with name 'access'."); } Attribute impl = e.attribute("impl"); if (impl == null) { String msg = "Element <access> must contain the attribute " + " 'impl'."; throw new IllegalArgumentException(msg); } Class c; Method m; AccessType[] access; List yList = e.selectNodes("type"); AccessRule[] rules = new AccessRule[yList.size()]; try { c = Class.forName(impl.getValue()); m = c.getDeclaredMethod("getAccessType", new Class[] { String.class }); for (int i = 0; i < yList.size(); i++) { Element a = (Element) yList.get(i); Attribute v = a.attribute("value"); Attribute h = a.attribute("handle"); if (v == null) { String msg = "Element <type> is missing required attribute " + "'value'."; throw new IllegalArgumentException(msg); } if (h == null) { String msg = "Element <type> is missing required attribute " + "'handle'."; throw new IllegalArgumentException(msg); } rules[i] = new AccessRule((AccessType) m.invoke(null, new Object[] { h.getValue() }), v.getValue().equalsIgnoreCase("GRANT") ? true : false); } } catch (Exception ex) { throw new RuntimeException("There was an error in parsing the Access Rule.", ex); } return rules; }
From source file:net.unicon.alchemist.access.permissions.PermissionsAccessBroker.java
License:Open Source License
public static AccessBroker parse(Element ce) { // Targets Access Broker -- Can be null. Element e = (Element) ce.selectSingleNode("targets/access-broker"); AccessBroker targets = null;/*from w w w.j a v a2s .c o m*/ if (e != null) targets = AccessBroker.parse(e); // Permissions Access Broker e = (Element) ce.selectSingleNode("permissions/access-broker"); if (e == null) throw new IllegalArgumentException("PermissionsAccessBroker requires an <access-broker> element " + "under element <permissions>."); AccessBroker permissions = AccessBroker.parse(e); // AccessType[] declaration e = (Element) ce.selectSingleNode("access"); if (e == null) throw new IllegalArgumentException("PermissionsAccessBroker requires an <access> element."); Attribute impl = e.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 new PermissionsAccessBroker(ce, targets, permissions, accessTypes); }
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 a2 s. c o m*/ 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 ww w. ja v a 2 s . 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.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); }/*from www.ja v a 2 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 ww w. j ava 2s .co 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><message></code> * element./*from ww w.ja va2 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;// w ww . j av a2 s. c om 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. *//*w ww . j a va2 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); }