001: /*
002: * This file is part of PFIXCORE.
003: *
004: * PFIXCORE is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU Lesser General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * PFIXCORE is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public License
015: * along with PFIXCORE; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: */
019:
020: package de.schlund.pfixxml;
021:
022: import java.util.Properties;
023:
024: import javax.xml.transform.dom.DOMResult;
025:
026: import org.w3c.dom.Document;
027: import org.w3c.dom.Element;
028:
029: import de.schlund.pfixcore.oxm.Marshaller;
030: import de.schlund.pfixcore.oxm.impl.MarshallerFactory;
031: import de.schlund.pfixxml.util.Xml;
032: import de.schlund.util.statuscodes.StatusCode;
033:
034: /**
035: * @author jtl
036: *
037: *
038: */
039:
040: public class ResultDocument {
041:
042: public static final String PFIXCORE_NS = "http://www.schlund.de/pustefix/core";
043: public static final String IXSL_NS = "http://www.w3.org/1999/XSL/Transform";
044:
045: protected Element formresult;
046: protected Element formvalues;
047: protected Element formerrors;
048: protected Element formhiddenvals;
049: protected Document doc;
050: protected SPDocument spdoc;
051:
052: public ResultDocument() {
053:
054: spdoc = new SPDocument();
055: doc = Xml.createDocument();
056: spdoc.setDocument(doc);
057: formresult = doc.createElement("formresult");
058: formresult.setAttribute("xmlns:pfx", PFIXCORE_NS);
059: formresult.setAttribute("serial", "" + spdoc.getTimestamp());
060: doc.appendChild(formresult);
061:
062: formvalues = doc.createElement("formvalues");
063: formresult.appendChild(formvalues);
064: formerrors = doc.createElement("formerrors");
065: formresult.appendChild(formerrors);
066: formhiddenvals = doc.createElement("formhiddenvals");
067: formresult.appendChild(formhiddenvals);
068: }
069:
070: public void addUsedNamespace(String prefix, String uri) {
071: formresult.setAttribute("xmlns:" + prefix, uri);
072: }
073:
074: public SPDocument getSPDocument() {
075: return spdoc;
076: }
077:
078: public void setSPDocument(SPDocument spdoc) {
079: this .spdoc = spdoc;
080: }
081:
082: public Element getRootElement() {
083: return formresult;
084: }
085:
086: public void addValue(String name, String value) {
087: if (value == null)
088: return;
089: Element param = doc.createElement("param");
090: param.setAttribute("name", name);
091: param.appendChild(doc.createTextNode(value));
092: formvalues.appendChild(param);
093: }
094:
095: public void addHiddenValue(String name, String value) {
096: if (value == null) {
097: return;
098: }
099: Element param = doc.createElement("hidden");
100: param.setAttribute("name", name);
101: param.appendChild(doc.createTextNode(value));
102: formhiddenvals.appendChild(param);
103: }
104:
105: public void addStatusCode(Properties props, StatusCode code,
106: String[] args, String level, String field) {
107: Element elem = ResultDocument.createIncludeFromStatusCode(doc,
108: props, code, args);
109: Element param = doc.createElement("error");
110: param.setAttribute("name", field);
111: param.appendChild(elem);
112: if (level != null) {
113: param.setAttribute("level", level);
114: }
115: formerrors.appendChild(param);
116: }
117:
118: // -----------------------------------------------------------------
119:
120: public Element createNode(String name) {
121: return createSubNode(formresult, name);
122: }
123:
124: public Element createSubNode(Element el, String name) {
125: Element node = doc.createElement(name);
126: el.appendChild(node);
127: return node;
128: }
129:
130: public static Element addTextChild(Element element, String name,
131: String text) {
132: Document owner = element.getOwnerDocument();
133: if (text == null) {
134: return null;
135: }
136:
137: Element tmp = owner.createElement(name);
138: tmp.appendChild(owner.createTextNode(text));
139: element.appendChild(tmp);
140: return tmp;
141: }
142:
143: public Element createIncludeFromStatusCode(Properties props,
144: StatusCode code) {
145: return createIncludeFromStatusCode(doc, props, code, null);
146: }
147:
148: public Element createIncludeFromStatusCode(Properties props,
149: StatusCode code, String[] args) {
150: return createIncludeFromStatusCode(doc, props, code, args);
151: }
152:
153: public static Element createIncludeFromStatusCode(Document thedoc,
154: Properties props, StatusCode code, String[] args) {
155: String incfile = code.getStatusCodePath().getRelativePath();
156: String part = code.getStatusCodeId();
157: Element include = thedoc.createElementNS(
158: ResultDocument.PFIXCORE_NS, "pfx:include");
159: include.setAttribute("href", incfile);
160: include.setAttribute("part", part);
161: if (args != null) {
162: for (int i = 0; i < args.length; i++) {
163: Element arg = thedoc.createElementNS(
164: ResultDocument.PFIXCORE_NS, "pfx:arg");
165: arg.setAttribute("value", args[i]);
166: include.appendChild(arg);
167: }
168: }
169: return include;
170: }
171:
172: public static Element addTextIncludeChild(Element element,
173: String name, String incfile, String part) {
174: Document owner = element.getOwnerDocument();
175:
176: Element include = owner.createElementNS(PFIXCORE_NS,
177: "pfx:include");
178: include.setAttribute("href", incfile);
179: include.setAttribute("part", part);
180:
181: Element tmp = owner.createElement(name);
182: tmp.appendChild(include);
183: element.appendChild(tmp);
184: return tmp;
185: }
186:
187: public static Element addObject(Element element, Object object) {
188: Marshaller marshaller = MarshallerFactory.getSharedMarshaller();
189: DOMResult result = new DOMResult(element);
190: marshaller.marshal(object, result);
191: return element;
192: }
193:
194: public static Element addObject(Element element, String name,
195: Object object) {
196: Document owner = element.getOwnerDocument();
197: Element tmp = owner.createElement(name);
198: element.appendChild(tmp);
199: addObject(tmp, object);
200: return tmp;
201: }
202:
203: }
|