001: package de.schlund.pfixcore.generator;
002:
003: import java.io.StringWriter;
004: import java.lang.reflect.Method;
005: import java.util.HashMap;
006: import java.util.Map;
007:
008: import javax.xml.parsers.DocumentBuilder;
009: import javax.xml.parsers.DocumentBuilderFactory;
010: import javax.xml.transform.OutputKeys;
011: import javax.xml.transform.Transformer;
012: import javax.xml.transform.TransformerFactory;
013: import javax.xml.transform.dom.DOMSource;
014: import javax.xml.transform.stream.StreamResult;
015:
016: import org.apache.log4j.Logger;
017: import org.w3c.dom.Document;
018: import org.w3c.dom.Element;
019:
020: import de.schlund.pfixcore.generator.annotation.Caster;
021: import de.schlund.pfixcore.generator.annotation.PostCheck;
022: import de.schlund.pfixcore.generator.annotation.PreCheck;
023: import de.schlund.pfixcore.generator.annotation.Property;
024: import de.schlund.pfixxml.util.Xml;
025: import de.schlund.pfixxml.util.XsltVersion;
026:
027: /**
028: * @author mleidig@schlund.de
029: */
030: public class IWrapperInfo {
031:
032: private final static Logger LOG = Logger
033: .getLogger(IWrapperInfo.class);
034: private static DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
035: .newInstance();
036: private static Map<Class<?>, Document> docCache = new HashMap<Class<?>, Document>();
037:
038: public static Document getDocument(
039: Class<? extends IWrapper> iwrpClass, XsltVersion xsltVersion) {
040: synchronized (iwrpClass) {
041: Document iwrpDoc = null;
042: synchronized (docCache) {
043: iwrpDoc = docCache.get(iwrpClass);
044: }
045: if (iwrpDoc == null) {
046: try {
047: DocumentBuilder db = docBuilderFactory
048: .newDocumentBuilder();
049: Document doc = db.newDocument();
050: Element root = doc.createElement("iwrapper");
051: root.setAttribute("class", iwrpClass.getName());
052: doc.appendChild(root);
053: IWrapper iw = iwrpClass.newInstance();
054: iw.init("dummy");
055: IWrapperParamDefinition[] defs = iw
056: .gimmeAllParamDefinitions();
057: for (IWrapperParamDefinition def : defs) {
058: Element elem = doc.createElement("param");
059: root.appendChild(elem);
060: elem.setAttribute("name", def.getName());
061: elem.setAttribute("type", def.getType());
062: elem.setAttribute("occurrence", def
063: .getOccurance());
064: elem.setAttribute("frequency", def
065: .getFrequency());
066: String getterName = "get" + def.getName();
067: Method getter = null;
068: try {
069: getter = iwrpClass.getMethod(getterName,
070: new Class[0]);
071: } catch (NoSuchMethodException x) {
072: LOG.warn("Getter not found: " + getterName);
073: }
074: IWrapperParamCaster caster = def.getCaster();
075: if (caster != null) {
076: Element casterElem = doc
077: .createElement("caster");
078: casterElem.setAttribute("class", caster
079: .getClass().getName());
080: elem.appendChild(casterElem);
081: if (getter != null) {
082: Caster casterAnno = getter
083: .getAnnotation(Caster.class);
084: if (casterAnno != null) {
085: Property[] propAnnos = casterAnno
086: .properties();
087: for (Property propAnno : propAnnos) {
088: Element propElem = doc
089: .createElement("property");
090: casterElem
091: .appendChild(propElem);
092: propElem.setAttribute("name",
093: propAnno.name());
094: propElem.setAttribute("value",
095: propAnno.value());
096: }
097: }
098: }
099: }
100: IWrapperParamPreCheck[] preChecks = def
101: .getPreChecks();
102: if (preChecks.length > 0) {
103: IWrapperParamPreCheck preCheck = preChecks[0];
104: Element preElem = doc
105: .createElement("precheck");
106: elem.appendChild(preElem);
107: preElem.setAttribute("class", preCheck
108: .getClass().getName());
109: if (getter != null) {
110: PreCheck preAnno = getter
111: .getAnnotation(PreCheck.class);
112: if (preAnno != null) {
113: Property[] propAnnos = preAnno
114: .properties();
115: for (Property propAnno : propAnnos) {
116: Element propElem = doc
117: .createElement("property");
118: preElem.appendChild(propElem);
119: propElem.setAttribute("name",
120: propAnno.name());
121: propElem.setAttribute("value",
122: propAnno.value());
123: }
124: }
125: }
126: }
127: IWrapperParamPostCheck[] postChecks = def
128: .getPostChecks();
129: if (postChecks.length > 0) {
130: IWrapperParamPostCheck postCheck = postChecks[0];
131: Element postElem = doc
132: .createElement("postcheck");
133: elem.appendChild(postElem);
134: postElem.setAttribute("class", postCheck
135: .getClass().getName());
136: if (getter != null) {
137: PostCheck postAnno = getter
138: .getAnnotation(PostCheck.class);
139: if (postAnno != null) {
140: Property[] propAnnos = postAnno
141: .properties();
142: for (Property propAnno : propAnnos) {
143: Element propElem = doc
144: .createElement("property");
145: postElem.appendChild(propElem);
146: propElem.setAttribute("name",
147: propAnno.name());
148: propElem.setAttribute("value",
149: propAnno.value());
150: }
151: }
152: }
153: }
154: }
155: if (LOG.isDebugEnabled()) {
156: TransformerFactory tf = TransformerFactory
157: .newInstance();
158: Transformer t = tf.newTransformer();
159: t.setOutputProperty(OutputKeys.INDENT, "yes");
160: StringWriter writer = new StringWriter();
161: t.transform(new DOMSource(doc),
162: new StreamResult(writer));
163: LOG.debug(writer.toString());
164: }
165: iwrpDoc = Xml.parse(xsltVersion, doc);
166: synchronized (docCache) {
167: docCache.put(iwrpClass, iwrpDoc);
168: }
169: } catch (Exception x) {
170: LOG.error("Can't get IWrapper information for "
171: + iwrpClass.getName(), x);
172: throw new RuntimeException(
173: "Can't get IWrapper information for "
174: + iwrpClass.getName(), x);
175: }
176: }
177: return iwrpDoc;
178: }
179: }
180:
181: }
|