📄 xmlconfiguration.java
字号:
{ if ("String".equals(type)) aClass = java.lang.String.class; else if ("URL".equals(type)) aClass = java.net.URL.class; else if ("InetAddress".equals(type)) aClass = java.net.InetAddress.class; else aClass = Loader.loadClass(XmlConfiguration.class, type,true); } } Object al=null; Iterator iter = node.iterator("Item"); while(iter.hasNext()) { XmlParser.Node item= (XmlParser.Node)iter.next(); String nid = item.getAttribute("id"); Object v = value(obj, item); al=LazyList.add(al,(v==null&&aClass.isPrimitive())?ZERO:v); if (nid != null) _idMap.put(nid, v); } Object array = LazyList.toArray(al,aClass); if (id != null) _idMap.put(id, array); return array; } /* ------------------------------------------------------------ */ /* * Create a new map object. * */ private Object newMap(Object obj, XmlParser.Node node) throws Exception { String id = node.getAttribute("id"); Map map = new HashMap(); if (id != null) _idMap.put(id, map); for (int i = 0; i < node.size(); i++) { Object o = node.get(i); if (o instanceof String) continue; XmlParser.Node entry = (XmlParser.Node) o; if (!entry.getTag().equals("Entry")) throw new IllegalStateException("Not an Entry"); XmlParser.Node key=null; XmlParser.Node value=null; for (int j = 0; j < entry.size(); j++) { o = entry.get(j); if (o instanceof String) continue; XmlParser.Node item = (XmlParser.Node) o; if (!item.getTag().equals("Item")) throw new IllegalStateException("Not an Item"); if (key==null) key=item; else value=item; } if (key==null || value==null) throw new IllegalStateException("Missing Item in Entry"); String kid = key.getAttribute("id"); String vid = value.getAttribute("id"); Object k = value(obj, key); Object v = value(obj, value); map.put(k,v); if (kid != null) _idMap.put(kid, k); if (vid != null) _idMap.put(vid, v); } return map; } /* ------------------------------------------------------------ */ /* * Create a new value object. * * @param obj @param node @return @exception Exception */ private Object propertyObj(Object obj, XmlParser.Node node) throws Exception { String id = node.getAttribute("id"); String name = node.getAttribute("name"); Object defval = node.getAttribute("default"); Object prop=null; if (_propertyMap!=null && _propertyMap.containsKey(name)) { prop=_propertyMap.get(name); } else if (defval != null) prop=defval; if (id != null) _idMap.put(id, prop); if (prop!=null) configure(prop, node, 0); return prop; } /* ------------------------------------------------------------ */ /* * Get the value of an element. If no value type is specified, then white space is trimmed out * of the value. If it contains multiple value elements they are added as strings before being * converted to any specified type. @param node */ private Object value(Object obj, XmlParser.Node node) throws Exception { Object value = null; // Get the type String type = node.getAttribute("type"); // Try a ref lookup String ref = node.getAttribute("ref"); if (ref != null) { value = _idMap.get(ref); } else { // handle trivial case if (node.size() == 0) { if ("String".equals(type)) return ""; return null; } // Trim values int first = 0; int last = node.size() - 1; // Handle default trim type if (type == null || !"String".equals(type)) { // Skip leading white Object item = null; while (first <= last) { item = node.get(first); if (!(item instanceof String)) break; item = ((String) item).trim(); if (((String) item).length() > 0) break; first++; } // Skip trailing white while (first < last) { item = node.get(last); if (!(item instanceof String)) break; item = ((String) item).trim(); if (((String) item).length() > 0) break; last--; } // All white, so return null if (first > last) return null; } if (first == last) // Single Item value value = itemValue(obj, node.get(first)); else { // Get the multiple items as a single string StringBuffer buf = new StringBuffer(); synchronized (buf) { for (int i = first; i <= last; i++) { Object item = node.get(i); buf.append(itemValue(obj, item)); } value = buf.toString(); } } } // Untyped or unknown if (value == null) { if ("String".equals(type)) return ""; return null; } // Try to type the object if (type == null) { if (value != null && value instanceof String) return ((String) value).trim(); return value; } if ("String".equals(type) || "java.lang.String".equals(type)) return value.toString(); Class pClass = TypeUtil.fromName(type); if (pClass != null) return TypeUtil.valueOf(pClass, value.toString()); if ("URL".equals(type) || "java.net.URL".equals(type)) { if (value instanceof URL) return value; try { return new URL(value.toString()); } catch (MalformedURLException e) { throw new InvocationTargetException(e); } } if ("InetAddress".equals(type) || "java.net.InetAddress".equals(type)) { if (value instanceof InetAddress) return value; try { return InetAddress.getByName(value.toString()); } catch (UnknownHostException e) { throw new InvocationTargetException(e); } } throw new IllegalStateException("Unknown type " + type); } /* ------------------------------------------------------------ */ /* * Get the value of a single element. @param obj @param item @return @exception Exception */ private Object itemValue(Object obj, Object item) throws Exception { // String value if (item instanceof String) return item; XmlParser.Node node = (XmlParser.Node) item; String tag = node.getTag(); if ("Call".equals(tag)) return call(obj, node); if ("Get".equals(tag)) return get(obj, node); if ("New".equals(tag)) return newObj(obj, node); if ("Ref".equals(tag)) return refObj(obj, node); if ("Array".equals(tag)) return newArray(obj, node); if ("Map".equals(tag)) return newMap(obj, node); if ("Property".equals(tag)) return propertyObj(obj,node); if ("SystemProperty".equals(tag)) { String name = node.getAttribute("name"); String defaultValue = node.getAttribute("default"); return System.getProperty(name, defaultValue); } Log.warn("Unknown value tag: " + node, new Throwable()); return null; } /* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */ /** * Run the XML configurations as a main application. * The command line is used to obtain properties files (must be named '*.properties') and XmlConfiguration * files. * <p> * Any property file on the command line is added to a combined Property instance that is passed to * each configuration file via {@link XmlConfiguration#setProperties(Map)}. * <p> * Each configuration file on the command line is used to create a new XmlConfiguration instance and the * {@link XmlConfiguration#configure()} method is used to create the configured object. If the resulting * object is an instance of {@link LifeCycle}, then it is started. * <p> * Any IDs created in a configuration are passed to the next configuration file on the command line using * {@link #getIdMap()} and {@link #setIdMap(Map)}. This allows objects with IDs created in one config file to * be referenced in subsequent config files on the command line. * * @param args array of property and xml configuration filenames or {@link Resource}s. */ public static void main(String[] args) { try { Properties properties=new Properties(); XmlConfiguration last=null; Object[] obj = new Object[args.length]; for (int i = 0; i < args.length; i++) { if (args[i].toLowerCase().endsWith(".properties")) { properties.load(Resource.newResource(args[i]).getInputStream()); } else { XmlConfiguration configuration = new XmlConfiguration(Resource.newResource(args[i]).getURL()); if (last!=null) configuration.getIdMap().putAll(last.getIdMap()); if (properties.size()>0) configuration.setProperties(properties); obj[i] = configuration.configure(); last=configuration; } } for (int i = 0; i < args.length; i++) { if (obj[i] instanceof LifeCycle) { LifeCycle lc = (LifeCycle)obj[i]; if (!lc.isRunning()) lc.start(); } } } catch (Exception e) { Log.warn(Log.EXCEPTION, e); } } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -