001: /*
002: * Copyright 2001-2006 C:1 Financial Services GmbH
003: *
004: * This software is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License Version 2.1, as published by the Free Software Foundation.
007: *
008: * This software is distributed in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
011: * Lesser General Public License for more details.
012: *
013: * You should have received a copy of the GNU Lesser General Public
014: * License along with this library; if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
016: */
017:
018: package de.finix.contelligent.action;
019:
020: import java.io.IOException;
021: import java.io.StringReader;
022: import java.util.Collection;
023: import java.util.Collections;
024:
025: import javax.xml.parsers.DocumentBuilder;
026: import javax.xml.parsers.DocumentBuilderFactory;
027:
028: import org.w3c.dom.Node;
029: import org.xml.sax.InputSource;
030:
031: import de.finix.contelligent.CallData;
032: import de.finix.contelligent.ComponentManager;
033: import de.finix.contelligent.DomComponent;
034: import de.finix.contelligent.components.Folder;
035: import de.finix.contelligent.content.Content;
036: import de.finix.contelligent.content.ContentProvider;
037: import de.finix.contelligent.content.StringContent;
038: import de.finix.contelligent.exception.ContelligentException;
039: import de.finix.contelligent.logging.LoggingService;
040:
041: public class Parameter2XML extends Folder implements DomComponent,
042: ContentProvider, StringContent {
043:
044: final static org.apache.log4j.Logger log = LoggingService
045: .getLogger(Parameter2XML.class);
046:
047: private String productionPolicy;
048:
049: private Node lastNode = null;
050:
051: private String lastValue = "*newly initialised*";
052:
053: private DocumentBuilderFactory fac;
054:
055: private Parameter2XMLPolicy policy;
056:
057: public Parameter2XML() {
058: super ();
059: fac = DocumentBuilderFactory.newInstance();
060: }
061:
062: public void setProductionPolicy(String policy) {
063: this .productionPolicy = policy;
064: }
065:
066: public String getProductionPolicy() {
067: return this .productionPolicy;
068: }
069:
070: public Node getNode(CallData callData) throws ContelligentException {
071: String value = null;
072: try {
073: log
074: .debug("starting building the node from getValue() with factory for builder "
075: + fac);
076: DocumentBuilder builder = fac.newDocumentBuilder();
077: log.debug("document builder found " + builder);
078: value = renderComponent(this , callData);
079: log.debug("returned the value " + value);
080:
081: Node node = null;
082:
083: if (value.equals(lastValue)) {
084: node = lastNode;
085: log.debug("reused node " + value);
086: } else {
087: node = builder.parse(new InputSource(new StringReader(
088: value)));
089: lastNode = node;
090: lastValue = value;
091: log.debug("did build the node " + value);
092: }
093: return node;
094: } catch (org.xml.sax.SAXException se) {
095: throw new ContelligentException(
096: "problems with parsing the value " + value, se);
097: } catch (java.io.IOException ioe) {
098: throw new ContelligentException(
099: "io problems with a bad desease ", ioe);
100: } catch (javax.xml.parsers.ParserConfigurationException pce) {
101: throw new ContelligentException(
102: "parser configuration problems", pce);
103: }
104: }
105:
106: public Content getContent() {
107: return this ;
108: }
109:
110: public Object getContentValue(CallData callData)
111: throws IOException, ContelligentException {
112: return getString(callData);
113: }
114:
115: public long lastModified(CallData callData) throws IOException {
116: return -1;
117: }
118:
119: public Collection getSensitiveCategories() {
120: return Collections.EMPTY_SET;
121: }
122:
123: /**
124: * generate a xml representation of the requested download items
125: */
126: public String getString(CallData callData)
127: throws ContelligentException, IOException {
128: Parameter2XMLPolicy p = getPolicy();
129: if (p == null) {
130: return "";
131: } else {
132: return p.produceXML(callData, getObject(callData));
133: }
134: }
135:
136: protected Object getObject(CallData callData) {
137: ComponentManager manager = ctx.getCallManager();
138: Parameter parent = (Parameter) manager.parentContainer(this );
139:
140: if (parent == null) {
141: return null;
142: } else {
143: return parent.getObject(callData);
144: }
145: }
146:
147: protected Parameter2XMLPolicy getPolicy() {
148: if (policy == null) {
149: policy = createPolicy();
150: }
151: return policy;
152: }
153:
154: private Parameter2XMLPolicy createPolicy() {
155: try {
156: Class clazz = Class.forName(productionPolicy);
157:
158: return (Parameter2XMLPolicy) clazz.newInstance();
159: } catch (ClassNotFoundException e) {
160: log.warn("Could not loadType policy class "
161: + productionPolicy);
162: } catch (InstantiationException e) {
163: log.warn("Could not create an instance of class "
164: + productionPolicy + ":" + e.getMessage());
165: } catch (IllegalAccessException e) {
166: log.warn("Could not create an instance of class "
167: + productionPolicy + ":" + e.getMessage());
168: } catch (ClassCastException e) {
169: log
170: .warn("Policy class "
171: + productionPolicy
172: + " does not implement interface XMLProductionPolicy");
173: }
174: return null;
175: }
176: }
|