0001: package org.xdev.base.core.object;
0002:
0003: import java.lang.reflect.Constructor;
0004: import java.lang.reflect.Field;
0005: import java.lang.reflect.Method;
0006:
0007: import java.util.*;
0008: import java.util.regex.Pattern;
0009:
0010: import org.jaxen.XPath;
0011: import org.jaxen.jdom.JDOMXPath;
0012:
0013: import org.apache.log4j.*;
0014: import org.jdom.Attribute;
0015: import org.jdom.Document;
0016: import org.jdom.Element;
0017:
0018: import org.jdom.input.SAXBuilder;
0019: import org.jdom.output.XMLOutputter;
0020: import org.jdom.transform.JDOMSource;
0021:
0022: import org.xdev.base.log.LoggerWriter;
0023: import org.xdev.base.schema.AbstractComponentSchema;
0024: import org.xdev.base.schema.ComponentSchemaManager;
0025: import org.xdev.base.util.*;
0026: import org.xdev.base.core.*;
0027: import org.xdev.base.core.compiler.AXCompiler;
0028:
0029: import java.io.File;
0030: import java.io.StringReader;
0031: import java.io.StringWriter;
0032:
0033: import javax.xml.transform.Transformer;
0034: import javax.xml.transform.TransformerFactory;
0035: import javax.xml.transform.stream.StreamResult;
0036: import javax.xml.transform.stream.StreamSource;
0037:
0038: /***
0039: *
0040: * @author AYegorov
0041: *
0042: * Company: xDev Group.org
0043: *
0044: * URL: <a>http://activexml.dev.java.net</a>
0045: *
0046: * File name: Configuration.java
0047: *
0048: * <p>Is bound: false</p>
0049: *
0050: * <table>
0051: * <tr>
0052: * <td>Description:</td>
0053: * <td>
0054: * This class is a base for representing a given XML Element node as a set of properties and child elements.
0055: *
0056: * This class also encapsulated all of the logic for logging, accessing properties via weakly and strong typed methods.
0057: *
0058: * This class cannot be bound to an XML name.
0059: * </td>
0060: * </tr>
0061: * </table>
0062: * <table>
0063: * <tr>
0064: * <td colspan="3">
0065: * Properties
0066: * </td>
0067: * </tr>
0068: * <tr>
0069: * <td>Name</td>
0070: * <td>Value</td>
0071: * <td>Description</td>
0072: * </tr>
0073: * <tr>
0074: * <td>log-level</td>
0075: * <td>.DEBUG</td>
0076: * <td>Runtime control for a log4j logging level - set all of the logging output in the component to DEBUG level (default)</td>
0077: * </tr>
0078: * <tr>
0079: * <td>log-level</td>
0080: * <td>.ERROR</td>
0081: * <td>Runtime control for a log4j logging level - set all of the logging output in the component to ERROR level</td>
0082: * </tr>
0083: * <tr>
0084: * <td>log-level</td>
0085: * <td>.FATAL</td>
0086: * <td>Runtime control for a log4j logging level - set all of the logging output in the component to FATAL level</td>
0087: * </tr>
0088: * <tr>
0089: * <td>log-level</td>
0090: * <td>.WARN</td>
0091: * <td>Runtime control for a log4j logging level - set all of the logging output in the component to WARN level</td>
0092: * </tr>
0093: * <tr>
0094: * <td>log-level</td>
0095: * <td>.INFO</td>
0096: * <td>Runtime control for a log4j logging level - set all of the logging output in the component to INFO level</td>
0097: * </tr>
0098: * <tr>
0099: * <td>log-class</td>
0100: * <td>any valid regular expression</td>
0101: * <td>Runtime control for which log4j statement to execute filtered by a class name</td>
0102: * </tr>
0103: * <tr>
0104: * <td>log-regexp</td>
0105: * <td>any valid regular expression</td>
0106: * <td>Runtime control for which log4j statement to execute filtered by content being logged</td>
0107: * </tr>
0108: *
0109: * </table>
0110: */
0111:
0112: public class Configuration extends Element implements Cloneable,
0113: java.io.Serializable {
0114: public static final String ID = "id";
0115: public static final String XPATH_CHILDREN = "xpath-children";
0116: public static final String DROP_CHILDREN = "xpath-drop";
0117: public static final String OWNER = "$owner";
0118:
0119: public static final Level DEBUG = Level.DEBUG;
0120: public static final Level INFO = Level.INFO;
0121: public static final Level WARN = Level.WARN;
0122: public static final Level ERROR = Level.ERROR;
0123: public static final Level FATAL = Level.FATAL;
0124:
0125: private Logger logger = null;
0126:
0127: private HashMap properties = new HashMap(100, 0.95f);
0128: private IPage config = null;
0129: private String id = null;
0130: private String guid = null;
0131: private String compiledName = null;
0132: private String parentLoggerId = null;
0133: private ArrayList instances = new ArrayList();
0134:
0135: private Configuration parent = null;
0136:
0137: //private ArrayList elements = new ArrayList();
0138: private HashMap elementsMap = new HashMap();
0139:
0140: private List observers = new ArrayList();
0141:
0142: private int nextIndex = 0;
0143: //private int elementsCount = 0;
0144:
0145: private boolean stateChanged = false;
0146:
0147: public Configuration(String id) {
0148: this (id, new HashMap());
0149: }
0150:
0151: public Configuration(String id, HashMap properties) {
0152: this .logger = Logger.getLogger(this .getClass().getName());
0153:
0154: this .id = (id == null) ? "" : id;
0155:
0156: this .properties = properties;
0157:
0158: try {
0159: this .guid = GuidGenerator.getGuid(true);
0160: } catch (Exception ex) {
0161:
0162: }
0163: }
0164:
0165: public String getPath() {
0166: return this .getProperty("path");
0167: }
0168:
0169: public void setConfig(IPage cfg) throws Exception {
0170: String cId = "";
0171: if (this .hasProperty("logger-id")) {
0172: cId = "." + this .getProperty("logger-id");
0173: }
0174: this .config = cfg;
0175:
0176: String loggerId = this .getLoggerId();
0177:
0178: this .logger = Logger.getLogger(loggerId);
0179:
0180: }
0181:
0182: public void setCompiledName(String name) {
0183: this .compiledName = name;
0184: }
0185:
0186: public String getLoggerId() {
0187:
0188: String cId = (this .parentLoggerId != null ? "."
0189: + this .parentLoggerId : "")
0190: + (this .compiledName != null ? "." + this .compiledName
0191: : "");
0192:
0193: if (this .hasProperty("logger-id")) {
0194: cId = "." + this .getProperty("logger-id");
0195: }
0196:
0197: String cfgId = this .getConfig().getLoggerId();
0198:
0199: return (cfgId != null ? cfgId : "") + cId;
0200: }
0201:
0202: public IPage getConfig() {
0203: return this .config;
0204: }
0205:
0206: public String getUniqueIdentifier() {
0207: return this .guid;
0208: }
0209:
0210: public String getId() {
0211: return this .id;
0212: }
0213:
0214: public void setId(String id) {
0215: this .id = id;
0216: }
0217:
0218: public void addProperty(String id, String property) {
0219: this .properties.put(id, property);
0220: }
0221:
0222: public void addProperties(HashMap override) {
0223: this .properties.putAll(override);
0224: }
0225:
0226: public void addProperty(String id, Object property) {
0227: this .properties.put(id, property);
0228: }
0229:
0230: public String getProperty(Object id) {
0231: Object prop = "";
0232: try {
0233: prop = this .getPropertyAsObject(id);
0234: if (prop == null) {
0235: prop = "";
0236: }
0237: } catch (Exception ex) {
0238: }
0239:
0240: return prop.toString();
0241: }
0242:
0243: public String getNullableProperty(String id) {
0244: Object prop = this .getPropertyAsObject(id);
0245:
0246: if ("".equals(prop)) {
0247: prop = null;
0248: }
0249:
0250: return prop != null ? prop.toString() : null;
0251: }
0252:
0253: public Pattern getPatternProperty(String id) {
0254: Pattern pattern = null;
0255:
0256: try {
0257:
0258: pattern = Pattern.compile(this .getNullableProperty(id));
0259:
0260: } catch (Exception ex) {
0261: this .logDebug(ex);
0262: }
0263:
0264: return pattern;
0265: }
0266:
0267: public XPath getXPathProperty(String id) {
0268: JDOMXPath xpath = null;
0269:
0270: try {
0271: xpath = new JDOMXPath(this .getProperty(id));
0272: } catch (Exception ex) {
0273: this .logDebug(ex);
0274: }
0275:
0276: return xpath;
0277: }
0278:
0279: public File getFileProperty(String id) {
0280: return BASE.getFile(this .getProperty(id), this .getClass());
0281: }
0282:
0283: public boolean hasProperty(String id) {
0284: return this .properties.containsKey(id);
0285: }
0286:
0287: public boolean hasProperty(String id, Object value) {
0288: boolean flag = false;
0289: Object prop = this .getPropertyAsObject(id);
0290: if (prop != null) {
0291: flag = prop.equals(value);
0292: }
0293:
0294: return flag;
0295: }
0296:
0297: public int hasProperties(String props) {
0298: return this .hasProperty(props.split("\\|"));
0299: }
0300:
0301: public int hasProperty(String[] props) {
0302:
0303: int i = 0;
0304:
0305: for (; i < props.length; i++) {
0306: if (this .hasProperty(props[i])) {
0307: break;
0308: }
0309: }
0310:
0311: return i;
0312: }
0313:
0314: public Class getClassProperty(String id) {
0315: Class cls = null;
0316: try {
0317:
0318: String className = this .getProperty(id);
0319:
0320: if ("int".equals(className)) {
0321: cls = int.class;
0322: } else if ("long".equals(className)) {
0323: cls = long.class;
0324: } else if ("short".equals(className)) {
0325: cls = short.class;
0326: } else if ("double".equals(className)) {
0327: cls = double.class;
0328: } else if ("float".equals(className)) {
0329: cls = float.class;
0330: } else if ("boolean".equals(className)) {
0331: cls = boolean.class;
0332: } else {
0333: cls = AXCompiler.loadClass(this .getProperty(id));
0334: }
0335: } catch (Exception ex) {
0336: cls = null;
0337: }
0338:
0339: return cls;
0340: }
0341:
0342: public HashMap getHashMapProperty(String id) {
0343: HashMap map = null;
0344: if (this .hasProperty(id)) {
0345: try {
0346: map = (HashMap) this .getPropertyAsObject(id);
0347: } catch (Exception ex) {
0348: map = new HashMap();
0349: }
0350: } else {
0351: map = new HashMap();
0352: }
0353:
0354: return map;
0355: }
0356:
0357: public int getIntegerProperty(String id) {
0358: return Integer.parseInt(this .getProperty(id));
0359: }
0360:
0361: public long getLongProperty(String id) {
0362: return Long.parseLong(this .getProperty(id));
0363: }
0364:
0365: public boolean getBooleanProperty(String id) {
0366: boolean flag = false;
0367: String prop = this .getProperty(id);
0368: if ("true".equalsIgnoreCase(prop)) {
0369: flag = true;
0370: }
0371:
0372: return flag;
0373: }
0374:
0375: public Object getFieldProperty(String id) {
0376: Object obj = null;
0377:
0378: try {
0379: Class cls = this .getClass();
0380:
0381: try {
0382: Field field = cls.getField(id);
0383:
0384: obj = field.get(this );
0385: } catch (Exception ex) {
0386: this .logDebug(ex);
0387:
0388: Method mth = cls.getMethod("get" + id, null);
0389:
0390: obj = mth.invoke(this , null);
0391: }
0392:
0393: } catch (Exception ex) {
0394: this .logDebug(ex);
0395: }
0396:
0397: return obj;
0398: }
0399:
0400: public List getTokenListProperty(String id) {
0401:
0402: int index = id.indexOf("{");
0403:
0404: String tokenString = this .getProperty(id.substring(0,
0405: index != -1 ? index : id.length()));
0406:
0407: ArrayList list = new ArrayList();
0408:
0409: String delim = index != -1 ? id.substring(id.indexOf("{") + 1,
0410: id.indexOf("}")) : ",";
0411:
0412: StringTokenizer tokens = new StringTokenizer(tokenString, delim);
0413:
0414: Object token = null;
0415:
0416: while (tokens.hasMoreElements()) {
0417:
0418: token = tokens.nextToken();
0419:
0420: token = this .resolveKeyword(token);
0421:
0422: list.add(token);
0423: }
0424:
0425: return list;
0426: }
0427:
0428: public String[] getStringArrayProperty(String id) {
0429: List tokens = this .getTokenListProperty(id);
0430:
0431: int size = tokens.size();
0432:
0433: String[] tokenArray = new String[size];
0434:
0435: for (int i = 0; i < size; i++) {
0436: tokenArray[i] = (String) tokens.get(i);
0437: }
0438:
0439: return tokenArray;
0440: }
0441:
0442: public Class[] getClassArrayProperty(String id) throws Exception {
0443: List tokens = this .getTokenListProperty(id);
0444:
0445: int size = tokens.size();
0446:
0447: Class[] tokenArray = new Class[size];
0448:
0449: for (int i = 0; i < size; i++) {
0450: tokenArray[i] = Class.forName((String) tokens.get(i));
0451: }
0452:
0453: return tokenArray;
0454: }
0455:
0456: public Object[] getObjectArrayProperty(String id) {
0457: List tokens = this .getTokenListProperty(id);
0458:
0459: int size = tokens.size();
0460:
0461: Object[] tokenArray = new Object[size];
0462:
0463: for (int i = 0; i < size; i++) {
0464: tokenArray[i] = tokens.get(i);
0465: }
0466:
0467: return tokenArray;
0468: }
0469:
0470: public List getActiveProperties() {
0471: ArrayList list = new ArrayList();
0472:
0473: ArrayList props = new ArrayList(this .properties.values());
0474:
0475: Object prop = null;
0476:
0477: int size = props.size();
0478:
0479: Configuration parent = (Configuration) this
0480: .getPropertyAsObject("$parent");
0481: Configuration root = (Configuration) this
0482: .getPropertyAsObject("$template");
0483: ;
0484:
0485: for (int i = 0; i < size; i++) {
0486: prop = props.get(i);
0487:
0488: if (prop instanceof Configuration
0489: && ((Configuration) prop)
0490: .hasProperty("property-name")
0491: && !prop.equals(root) && !prop.equals(parent)) {
0492: list.add(prop);
0493: }
0494: }
0495:
0496: return list;
0497: }
0498:
0499: public Object getPropertyAsObject(Object ids) {
0500: Object obj = null;
0501:
0502: String def = null;
0503:
0504: String[] props = null;
0505:
0506: if (ids instanceof String[]) {
0507: props = (String[]) ids;
0508: } else {
0509:
0510: String propertyKey = ids.toString();
0511:
0512: if (propertyKey.indexOf("^") != -1) {
0513:
0514: def = propertyKey
0515: .substring(propertyKey.indexOf("^") + 1);
0516:
0517: propertyKey = propertyKey.substring(0, propertyKey
0518: .indexOf("^"));
0519: }
0520:
0521: props = propertyKey.split("\\|");
0522: }
0523:
0524: String id = null;
0525:
0526: for (int i = 0; i < props.length && obj == null; i++) {
0527: obj = this .properties.get((id = props[i]));
0528: }
0529:
0530: obj = (obj instanceof String) ? this .resolveKeyword(obj)
0531: : ((obj instanceof Configuration)
0532: && !id.startsWith("$") ? ((Configuration) obj)
0533: .getObjectValue() : obj);
0534:
0535: if (("".equals(obj) || obj == null) && def != null) {
0536:
0537: obj = def;
0538: }
0539:
0540: return obj;
0541: }
0542:
0543: public HashMap getProperties() {
0544: return (HashMap) this .properties.clone();
0545: }
0546:
0547: public Collection getKeys() {
0548: return this .properties.keySet();
0549: }
0550:
0551: public Collection getValues() {
0552: return this .properties.values();
0553: }
0554:
0555: public void setProperties(HashMap properties) {
0556: this .properties = properties;
0557: }
0558:
0559: protected Object resolveKeyword(Object key) {
0560: Object value = null;
0561: String keyword = key.toString();
0562:
0563: if (keyword != null) {
0564:
0565: String[] strs = keyword.split("[\\$\\{]|[\\}]");
0566:
0567: if (strs.length > 1) {
0568: for (int i = 0; i < strs.length; i++) {
0569:
0570: if ("".equals(strs[i])) {
0571: continue;
0572: }
0573:
0574: try {
0575:
0576: String propertyValue = System
0577: .getProperty(strs[i]);
0578:
0579: keyword = Tools.replaceString(keyword, "${"
0580: + strs[i] + "}",
0581: propertyValue == null ? "${" + strs[i]
0582: + "}" : propertyValue);
0583: } catch (Exception ex) {
0584: this .logError(ex);
0585: }
0586: }
0587: }
0588:
0589: strs = keyword.split("^\\'|[\\'][+][\\']|\\'$");
0590:
0591: if (strs.length > 1) {
0592: Object obj = null;
0593:
0594: for (int i = 0; i < strs.length; i++) {
0595:
0596: if ("".equals(strs[i])) {
0597: continue;
0598: }
0599:
0600: obj = this .resolveKeyword(strs[i]);
0601:
0602: try {
0603: keyword = Tools.replaceString(keyword, "'"
0604: + strs[i] + "'", obj != null ? Tools
0605: .replaceString(Tools.replaceString(obj
0606: .toString(), "\\n", "\n"),
0607: "\\t", "\t") : "");
0608: } catch (Exception ex) {
0609: this .logError(ex);
0610: }
0611: }
0612: }
0613:
0614: if (keyword.startsWith("$") && !keyword.equals("$")) {
0615:
0616: value = this .getPropertyAsObject(keyword.substring(1,
0617: keyword.length()));
0618: } else if (keyword.startsWith(".") && !keyword.equals(".")) {
0619:
0620: value = this .getFieldProperty(keyword.substring(1,
0621: keyword.length()));
0622: }
0623:
0624: if (value == null) {
0625: value = keyword;
0626: }
0627:
0628: }
0629:
0630: return value;
0631: }
0632:
0633: public Document getXmlDocumentPropertyAsFile(String id) {
0634:
0635: Document obj = null;
0636:
0637: try {
0638:
0639: obj = new SAXBuilder().build(this .getFileProperty(id));
0640: } catch (Exception ex) {
0641: this .logError(ex);
0642: }
0643:
0644: return obj;
0645: }
0646:
0647: public Document getXmlDocumentPropertyAsValue(String id) {
0648: Document obj = null;
0649:
0650: try {
0651:
0652: obj = new SAXBuilder().build(new StringReader(this
0653: .getProperty(id)));
0654: } catch (Exception ex) {
0655: this .logError(ex);
0656: }
0657:
0658: return obj;
0659: }
0660:
0661: public void log(Object obj) {
0662:
0663: Level l = (Level) this .getPropertyAsObject("log-level");
0664:
0665: this .logCustom(this .logger, obj, l != null ? l : Level.INFO);
0666: }
0667:
0668: public void log(Object obj, Level def) {
0669:
0670: Level l = (Level) this .getPropertyAsObject("log-level");
0671:
0672: this .logCustom(this .logger, obj, l != null ? l : def);
0673: }
0674:
0675: public void logError(Object obj) {
0676: this .log(obj, Level.ERROR);
0677: }
0678:
0679: public void logFatal(Object obj) {
0680: this .log(obj, Level.FATAL);
0681: }
0682:
0683: public void logDebug(Object obj) {
0684: this .log(obj, Level.DEBUG);
0685: }
0686:
0687: public void logWarn(Object obj) {
0688: this .log(obj, Level.WARN);
0689: }
0690:
0691: public void logCustom(Object obj, Level l) {
0692: this .logCustom(this .logger, obj, l);
0693: }
0694:
0695: public void logCustom(String id, Object obj) {
0696: this .logCustom(Logger.getLogger(id), obj, Level.INFO);
0697: }
0698:
0699: public void logCustom(String id, Object obj, Level l) {
0700: this .logCustom(Logger.getLogger(id), obj, l);
0701: }
0702:
0703: public void logCustom(Logger logger, Object obj, Level l) {
0704: if (obj instanceof Exception) {
0705: obj = BASE.getValueFromObject(obj);
0706: }
0707:
0708: boolean pass = true;
0709:
0710: if (this .hasProperty("log-class")) {
0711: String methodName = ServerUtilities
0712: .getExecutingMethodName(3);
0713:
0714: String cls = this .getProperty("log-class");
0715:
0716: pass = BASE.partialMatch(cls, methodName);
0717: }
0718:
0719: if (this .hasProperty("log-regex") && obj != null) {
0720: pass = BASE.partialMatch(this .getProperty("log-regex"), obj
0721: .toString());
0722: }
0723:
0724: if (pass) {
0725: logger.log(l, obj);
0726: }
0727: }
0728:
0729: public Object createObject(Configuration source, Object[] inputs,
0730: String defaultClass) throws Exception {
0731: IPage config = source.getConfig();
0732: Class[] classes = new Class[inputs.length];
0733: for (int i = 0; i < inputs.length; i++) {
0734: if (inputs[i] != null) {
0735: classes[i] = inputs[i].getClass();
0736: } else {
0737: classes[i] = null;
0738: }
0739: }
0740: Class cls = source.getClass();
0741:
0742: Object obj = null;
0743: Constructor c = null;
0744: try {
0745: c = cls.getConstructor(classes);
0746: obj = c.newInstance(inputs);
0747: } catch (Exception ex) {
0748: cls = AXCompiler.loadClass(defaultClass);
0749: c = cls.getConstructor(classes);
0750: obj = c.newInstance(inputs);
0751: }
0752:
0753: try {
0754: /*Method m = cls.getMethod("setConfig", new Class[]{AbstractPage.class});
0755: m.invoke(obj, new Object[]{this.getConfig()});*/
0756:
0757: ((Configuration) obj).setCompiledName(this .compiledName);
0758: ((Configuration) obj).setConfig(this .getConfig());
0759: ((Configuration) obj).merge((Element) this );
0760: } catch (Exception ex) {
0761: this .logError(ex);
0762: }
0763:
0764: return obj;
0765: }
0766:
0767: public void addElement(Configuration element) {
0768:
0769: if (element.hasProperty("property-name")) {
0770: this .properties.put(element.getProperty("property-name"),
0771: element);
0772: } else {
0773: //this.elements.add(element);
0774: //this.elementsCount = this.elements.size();
0775:
0776: String id = element.getId();
0777:
0778: if (id != null && !"".equals(id)) {
0779:
0780: this .elementsMap.put(element.getId(), element);
0781: }
0782:
0783: this .addContent((Element) element);
0784: }
0785: }
0786:
0787: public void removeElement(Configuration element) {
0788: Configuration c = null;
0789: try {
0790: //c = (Configuration)this.elements.remove(this.elements.indexOf(element));
0791: //this.elementsCount = this.elements.size();
0792: super .removeContent(element);
0793: } catch (Exception ex) {
0794: }
0795: }
0796:
0797: public Configuration removeElement(String id) {
0798: Configuration c = null;
0799: String cId = null;
0800: if (id != null && !"".equals(id)) {
0801: int size = this .getCount();
0802: for (int i = 0; i < size; i++) {
0803: c = this .getElement(i);
0804: if (c != null) {
0805: cId = c.getId();
0806: if (id.equals(cId)) {
0807: this .removeElement(c);
0808: break;
0809: }
0810: }
0811: c = null;
0812: cId = null;
0813: }
0814: }
0815:
0816: return c;
0817: }
0818:
0819: public Configuration getElement(int index) {
0820: return (Configuration) this .getElements(false).get(index);
0821: }
0822:
0823: public Configuration getElement(String xpathExp) {
0824: Configuration c = null;
0825:
0826: try {
0827:
0828: XPath xpath = new JDOMXPath(xpathExp);
0829:
0830: c = (Configuration) xpath.selectSingleNode(this );
0831:
0832: } catch (Exception ex) {
0833: this .logError(ex);
0834: }
0835:
0836: return c;
0837: }
0838:
0839: public boolean hasChildren() {
0840: return !this .getChildren().isEmpty();
0841:
0842: //return !(this.hasProperty(Configuration.XPATH_CHILDREN) ? this.hasChildren(this.getProperty(Configuration.XPATH_CHILDREN)) : this.getChildren().isEmpty());
0843: }
0844:
0845: public boolean hasChildren(String xpathExp) {
0846: boolean flag = false;
0847:
0848: try {
0849:
0850: XPath xpath = new JDOMXPath(xpathExp);
0851:
0852: flag = xpath.selectNodes(this ).isEmpty();
0853:
0854: } catch (Exception ex) {
0855: this .logError(ex);
0856: }
0857:
0858: return flag;
0859: }
0860:
0861: public int getCount() {
0862: //return this.hasProperty(Configuration.XPATH_CHILDREN) ? this.getCount(this.getProperty(Configuration.XPATH_CHILDREN)) : this.getChildren().size();
0863:
0864: return this .getChildren().size();
0865: }
0866:
0867: public int getCount(String xpathExp) {
0868:
0869: int count = -1;
0870:
0871: try {
0872:
0873: XPath xpath = new JDOMXPath(xpathExp);
0874:
0875: count = xpath.selectNodes(this ).size();
0876:
0877: } catch (Exception ex) {
0878: this .logError(ex);
0879: }
0880:
0881: return count;
0882: }
0883:
0884: public boolean hasValue() {
0885: boolean flag = false;
0886: Object value = this .getPropertyAsObject("value");
0887: if (value != null) {
0888: if (value instanceof String && !"".equals((String) value)) {
0889: flag = true;
0890: } else if (!(value instanceof String)) {
0891: flag = true;
0892: }
0893: }
0894:
0895: return flag;
0896: }
0897:
0898: public Configuration next() {
0899: Configuration element = null;
0900: if (this .nextIndex < this .getCount()) {
0901: //element = (Configuration)this.elements.get(this.nextIndex++);
0902: element = this .getElement(this .nextIndex++);
0903: }
0904:
0905: return element;
0906: }
0907:
0908: public boolean hasNextElement() {
0909: boolean flag = false;
0910: if (this .nextIndex < this .getCount()) {
0911: flag = true;
0912: }
0913:
0914: return flag;
0915: }
0916:
0917: public void reset() {
0918: this .setProperty("value", "");
0919: int size = this .getCount();
0920: for (int i = 0; i < size; i++) {
0921: this .getElement(i).reset();
0922: }
0923: }
0924:
0925: public void setValue(Object value) {
0926: this .addProperty("value", value);
0927: }
0928:
0929: public void setProperty(String id, Object value) {
0930: this .addProperty(id, value);
0931: }
0932:
0933: public List parseElements(List source, boolean clone) {
0934: int size = this .getCount();//this.elements.size();
0935:
0936: List list = new ArrayList();
0937:
0938: if (clone) {
0939:
0940: for (int i = 0; i < size; i++) {
0941: Configuration c = (Configuration) source.get(i);//this.elements.get(i);
0942: list.add((Configuration) c.clone());
0943: }
0944: } else {
0945: list = source;
0946: }
0947:
0948: return list;
0949: }
0950:
0951: public List getElements(boolean clone) {
0952: //return this.hasProperty(Configuration.XPATH_CHILDREN) ? this.getElements(this.getProperty(Configuration.XPATH_CHILDREN),clone, true) : this.parseElements(this.getChildren(), clone);
0953:
0954: return this .parseElements(this .getChildren(), clone);
0955: }
0956:
0957: public List getElements() {
0958: return this .getElements(true);
0959: }
0960:
0961: public List getElements(String xpathExp, boolean clone, boolean drop) {
0962:
0963: List list = null;
0964:
0965: try {
0966:
0967: XPath xpath = new JDOMXPath(xpathExp);
0968:
0969: list = this .parseElements(xpath.selectNodes(this ), clone);
0970:
0971: /*if (drop && (!this.hasProperty(Configuration.DROP_CHILDREN) || this.getBooleanProperty(Configuration.DROP_CHILDREN))) {
0972: int size = list.size();
0973:
0974: Configuration c = null;
0975:
0976: for (int i=0; i<size; i++) {
0977: c = (Configuration)list.get(i);
0978:
0979: c.setProperty(Configuration.OWNER, this);
0980: }
0981: }*/
0982:
0983: } catch (Exception ex) {
0984: this .logError(ex);
0985: }
0986:
0987: return list;
0988: }
0989:
0990: public Configuration getOwner() {
0991: return (Configuration) this
0992: .getPropertyAsObject(Configuration.OWNER);
0993: }
0994:
0995: public boolean isOwner(Configuration parent) {
0996: Configuration owner = this .getOwner();
0997:
0998: return owner == null || owner.equals(parent);
0999: }
1000:
1001: public boolean hasProperOwner() {
1002: Object obj = this .getParent();
1003:
1004: return obj instanceof Document || parent == null
1005: || this .isOwner(parent);
1006: }
1007:
1008: public List getElements(String xpathExp) {
1009: return this .getElements(xpathExp, false, true);
1010: }
1011:
1012: public void setElements(List elements) {
1013: int size = elements.size();
1014:
1015: Configuration element = null;
1016:
1017: for (int i = 0; i < size; i++) {
1018: element = (Configuration) elements.get(i);
1019:
1020: this .addElement(element);
1021:
1022: //this.addContent(element);
1023:
1024: element = null;
1025: }
1026:
1027: }
1028:
1029: private Configuration getParentElement() {
1030: return this .parent;
1031: }
1032:
1033: public Object newInstance(Object root, Object parent) {
1034: return this .clone();
1035: }
1036:
1037: public Object clone() {
1038: Configuration c = new Configuration(this .id,
1039: (HashMap) this .properties.clone());
1040: try {
1041: c.setConfig(this .config);
1042: } catch (Exception ex) {
1043: }
1044:
1045: return c;
1046: }
1047:
1048: public String getCompiledName() {
1049: return this .compiledName;
1050: }
1051:
1052: /**
1053: * @return Returns the parentLoggerId.
1054: */
1055: public String getParentLoggerId() {
1056: return parentLoggerId;
1057: }
1058:
1059: /**
1060: * @param parentLoggerId The parentLoggerId to set.
1061: */
1062: public void setParentLoggerId(String parentLoggerId) {
1063: this .parentLoggerId = parentLoggerId;
1064: }
1065:
1066: public Object getObjectValue() {
1067: return null;
1068: }
1069:
1070: public boolean document(StringBuffer buffer) throws Exception {
1071: Document doc = new Document();
1072:
1073: Element root = new Element(this .getClass().getName());
1074: Element interfaces = new Element("interface-list");
1075: Element extend = new Element("superclass-list");
1076: Element fields = new Element("field-list");
1077: Element constructors = new Element("constructor-list");
1078: Element methods = new Element("method-list");
1079: Element properties = new Element("property-list");
1080:
1081: root.addContent(interfaces);
1082: root.addContent(extend);
1083: root.addContent(fields);
1084: root.addContent(methods);
1085: root.addContent(properties);
1086:
1087: //doc.setRootElement(root);
1088:
1089: Method[] mList = this .getClass().getMethods();
1090:
1091: Method m = null;
1092:
1093: Element method = null;
1094: Element exceptions = null;
1095: Element exception = null;
1096: Element parameters = null;
1097: Element parameter = null;
1098:
1099: Class[] excs = null;
1100: Class[] params = null;
1101:
1102: for (int i = 0; i < mList.length; i++) {
1103: m = mList[i];
1104:
1105: method = new Element("method");
1106:
1107: exceptions = new Element("exception-list");
1108: parameters = new Element("parameter-list");
1109:
1110: method.addContent(exceptions);
1111: method.addContent(parameters);
1112:
1113: method.setAttribute(new Attribute("name", m.getName()));
1114: method.setAttribute(new Attribute("return-type", m
1115: .getReturnType().getName()));
1116: method.setAttribute(new Attribute("declaring-class", m
1117: .getDeclaringClass().getName()));
1118: method.setAttribute(new Attribute("accessible", String
1119: .valueOf(m.isAccessible())));
1120:
1121: params = m.getParameterTypes();
1122:
1123: for (int j = 0; j < params.length; j++) {
1124: parameter = new Element("parameter");
1125:
1126: parameter.setAttribute(new Attribute("type", params[j]
1127: .getName()));
1128:
1129: parameters.addContent(parameter);
1130: }
1131:
1132: excs = m.getParameterTypes();
1133:
1134: for (int j = 0; j < excs.length; j++) {
1135: exception = new Element("exception");
1136:
1137: parameter.setAttribute(new Attribute("type", excs[j]
1138: .getName()));
1139:
1140: exceptions.addContent(exception);
1141: }
1142:
1143: methods.addContent(method);
1144: }
1145:
1146: Field[] fList = this .getClass().getFields();
1147:
1148: Field f = null;
1149:
1150: Element field = null;
1151:
1152: Element fieldValue = null;
1153:
1154: Object obj = null;
1155:
1156: for (int i = 0; i < fList.length; i++) {
1157: f = fList[i];
1158:
1159: obj = f.get(this );
1160:
1161: field = new Element("field");
1162:
1163: field.setAttribute(new Attribute("name", f.getName()));
1164: field.setAttribute(new Attribute("declaring-class", f
1165: .getDeclaringClass().getName()));
1166: field.setAttribute(new Attribute("type", f.getType()
1167: .getName()));
1168:
1169: if (obj != null) {
1170:
1171: fieldValue = new Element("value");
1172:
1173: fieldValue.setAttribute("type", obj.getClass()
1174: .getName());
1175:
1176: fieldValue.setText(obj.toString());
1177: }
1178:
1179: fields.addContent(field);
1180: }
1181:
1182: Class[] iList = null;
1183:
1184: Element iface = null;
1185:
1186: Element sup = null;
1187:
1188: Class cls = this .getClass();
1189:
1190: do {
1191:
1192: iList = cls.getInterfaces();
1193:
1194: for (int i = 0; i < iList.length; i++) {
1195: iface = new Element("interface");
1196:
1197: iface.setAttribute("type", iList[i].getName());
1198:
1199: interfaces.addContent(iface);
1200: }
1201:
1202: if (!cls.equals(this .getClass())) {
1203: sup = new Element("superclass");
1204:
1205: sup.setAttribute("type", cls.getName());
1206:
1207: extend.addContent(sup);
1208: }
1209:
1210: } while (!(cls = cls.getSuperclass()).equals(Object.class));
1211:
1212: Constructor[] cList = this .getClass().getConstructors();
1213:
1214: Element cons = null;
1215:
1216: Constructor c = null;
1217:
1218: for (int i = 0; i < cList.length; i++) {
1219:
1220: c = cList[i];
1221:
1222: cons = new Element("constructor");
1223:
1224: parameters = new Element("parameters");
1225:
1226: exceptions = new Element("exceptions");
1227:
1228: cons.addContent(parameters);
1229: cons.addContent(exceptions);
1230:
1231: cons.setAttribute("declaring-class", c.getDeclaringClass()
1232: .getName());
1233:
1234: params = c.getParameterTypes();
1235:
1236: for (int j = 0; j < params.length; j++) {
1237: parameter = new Element("parameter");
1238:
1239: parameter.setAttribute(new Attribute("type", params[j]
1240: .getName()));
1241:
1242: parameters.addContent(parameter);
1243: }
1244:
1245: excs = c.getParameterTypes();
1246:
1247: for (int j = 0; j < excs.length; j++) {
1248: exception = new Element("exception");
1249:
1250: parameter.setAttribute(new Attribute("type", excs[j]
1251: .getName()));
1252:
1253: exceptions.addContent(exception);
1254: }
1255:
1256: constructors.addContent(cons);
1257: }
1258:
1259: Element property = null;
1260:
1261: Iterator iter = this .properties.keySet().iterator();
1262:
1263: Object key = null;
1264:
1265: Object value = null;
1266:
1267: while (iter.hasNext()) {
1268: key = iter.next();
1269:
1270: if (key != null && key instanceof String) {
1271: property = new Element("property");
1272:
1273: value = this .properties.get(key);
1274:
1275: property.setAttribute("name", (String) key);
1276:
1277: if (value != null) {
1278:
1279: property.setAttribute("type", value.getClass()
1280: .getName());
1281:
1282: property.setText(value.toString());
1283: }
1284:
1285: properties.addContent(property);
1286: }
1287: }
1288:
1289: String document = null;
1290:
1291: boolean flag = false;
1292:
1293: try {
1294:
1295: TransformerFactory transformerFactory = org.apache.xalan.processor.TransformerFactoryImpl
1296: .newInstance();
1297: Transformer transformer = transformerFactory
1298: .newTransformer(new StreamSource(BASE.getFile("/"
1299: + Tools.replaceString(this .getClass()
1300: .getName(), ".", "/") + ".xsl",
1301: this .getClass())));
1302:
1303: StringWriter writer = new StringWriter();
1304:
1305: transformer.transform(new JDOMSource(doc),
1306: new StreamResult(writer));
1307:
1308: document = writer.toString();
1309:
1310: flag = true;
1311:
1312: } catch (Exception ex) {
1313: this .logDebug(ex);
1314:
1315: document = new XMLOutputter().outputString(doc);
1316: }
1317:
1318: buffer.append(document);
1319:
1320: return flag;
1321:
1322: //return false;
1323: }
1324:
1325: /**
1326: * @param string
1327: * @return
1328: */
1329: public Properties getPropertiesProperty(String string)
1330: throws java.io.FileNotFoundException, java.io.IOException {
1331: Properties props = new Properties();
1332:
1333: props.load(this .getFileInputStreamProperty(string));
1334:
1335: return props;
1336: }
1337:
1338: public java.io.FileInputStream getFileInputStreamProperty(
1339: String string) throws java.io.FileNotFoundException {
1340: return new java.io.FileInputStream(this .getFileProperty(string));
1341: }
1342:
1343: public java.io.FileOutputStream getFileOutputStreamProperty(
1344: String string) throws java.io.FileNotFoundException {
1345: return new java.io.FileOutputStream(this
1346: .getFileProperty(string));
1347: }
1348:
1349: public void addObserver(Object observer) {
1350: this .observers.add(observer);
1351: }
1352:
1353: public void setChanged() {
1354: this .stateChanged = this .stateChanged ? false : true;
1355: }
1356:
1357: public int countObservers() {
1358: return this .observers.size();
1359: }
1360:
1361: public void notifyObservers(Object parameter) throws Exception {
1362:
1363: int size = this .countObservers();
1364:
1365: Object obj = null;
1366:
1367: for (int i = 0; i < size; i++) {
1368: if ((obj = this .observers.get(i)) instanceof Observer) {
1369: ((ConfigurationObserver) obj).update(this , parameter);
1370: }
1371: }
1372: }
1373:
1374: public void deleteObserver(Object obj) {
1375: this .observers.remove(obj);
1376: }
1377:
1378: public void merge(Element elm) throws Exception {
1379:
1380: String name = elm.getName();
1381:
1382: this .setName(name != null ? name : this .getId());
1383:
1384: this .setNamespace(elm.getNamespace());
1385:
1386: List list = elm.getAttributes();
1387:
1388: int size = list.size();
1389:
1390: Attribute source = null;
1391:
1392: Attribute target = null;
1393:
1394: for (int i = 0; i < size; i++) {
1395:
1396: source = (Attribute) list.get(i);
1397:
1398: target = new Attribute(source.getName(), source.getValue());
1399:
1400: this .setAttribute(target);
1401:
1402: source = null;
1403: target = null;
1404: }
1405: }
1406:
1407: public String toString() {
1408: return new StringBuffer("<").append(this .getName())
1409: .append("/>").toString();
1410: }
1411:
1412: public AbstractComponentSchema getSchema() {
1413: return ComponentSchemaManager.getInstance().getSchema(this);
1414: }
1415: }
|