001: package org.enhydra.util.formfaces;
002:
003: import java.io.StringReader;
004:
005: import javax.xml.parsers.DocumentBuilderFactory;
006:
007: import org.enhydra.util.ResponsePostProcessor;
008: import org.enhydra.xml.io.OutputOptions;
009: import org.w3c.dom.Node;
010: import org.w3c.dom.NodeList;
011: import org.xml.sax.InputSource;
012:
013: import com.lutris.logging.LogChannel;
014: import com.lutris.logging.Logger;
015: import com.lutris.util.Config;
016:
017: public class FormFacesResponsePostProcessor implements
018: ResponsePostProcessor {
019:
020: /*
021: * Name of the post processor
022: */
023: private String name = null;
024:
025: /**
026: * LogChannel used by ResponsePostProcessor
027: */
028: private LogChannel logChannel;
029:
030: /*
031: * Name of the post processor supported mime type
032: */
033: public static final String mimeType = "text/xhtml+xforms";
034:
035: /*
036: * Name of the post processor generated mime type
037: */
038: public static final String outMimeType = "text/html";
039:
040: /*
041: * FormFaces Response Post Processor specific parameters
042: */
043: private static final String SCRIPT_PATH_PARAM_NAME = "ScriptPath";
044:
045: private static final String DEFAULT_SCRIPT_PATH_PARAM_VALUE = "formfaces.js";
046:
047: private static final String CSS_PATH_PARAM_NAME = "CssPath";
048:
049: private static final String DEFAULT_CSS_PATH_PARAM_VALUE = null;
050:
051: private String scriptPath = null;
052:
053: private String cssPath = null;
054:
055: public FormFacesResponsePostProcessor() {
056: super ();
057: }
058:
059: public FormFacesResponsePostProcessor(LogChannel logChannel) {
060: super ();
061: this .logChannel = logChannel;
062: }
063:
064: public void configure(Config config) {
065: try {
066: scriptPath = config.getString(SCRIPT_PATH_PARAM_NAME,
067: DEFAULT_SCRIPT_PATH_PARAM_VALUE);
068:
069: cssPath = config.getString(CSS_PATH_PARAM_NAME,
070: DEFAULT_CSS_PATH_PARAM_VALUE);
071:
072: if (logChannel != null) {
073: logChannel.write(Logger.DEBUG, "FormFaces Processor ("
074: + name + ") - Initialized successfully!");
075: }
076: } catch (Exception fe) {
077: if (logChannel != null) {
078: logChannel
079: .write(
080: Logger.WARNING,
081: "FormFaces Processor ("
082: + name
083: + ") - Initialized with default configuration settings!",
084: fe);
085: }
086:
087: // Applying default values
088: scriptPath = DEFAULT_SCRIPT_PATH_PARAM_VALUE;
089: cssPath = DEFAULT_CSS_PATH_PARAM_VALUE;
090: }
091: }
092:
093: public Node process(OutputOptions oo, Node document) {
094: try {
095: if (logChannel != null) {
096: logChannel
097: .write(
098: Logger.DEBUG,
099: "FormFaces Post Processor ("
100: + name
101: + ") - Processing parameters (if any)!");
102: }
103:
104: if (oo.getFreeformOption(SCRIPT_PATH_PARAM_NAME) != null)
105: scriptPath = (String) oo
106: .getFreeformOption(SCRIPT_PATH_PARAM_NAME);
107:
108: if (logChannel != null) {
109: logChannel.write(Logger.DEBUG,
110: "FormFaces Post Processor (" + name
111: + ") - Generating output!");
112: }
113:
114: Node head = findHeadNode(document);
115:
116: if (head != null) {
117: DocumentBuilderFactory dbf = DocumentBuilderFactory
118: .newInstance();
119: dbf.setNamespaceAware(true);
120:
121: Node script = dbf.newDocumentBuilder()
122: .parse(
123: new InputSource(new StringReader(
124: "<script type=\"text/javascript\" src=\""
125: + scriptPath
126: + "\"></script>")));
127: head.appendChild(head.getOwnerDocument().importNode(
128: script.getFirstChild(), true));
129: if (cssPath != null) {
130: Node css = dbf.newDocumentBuilder().parse(
131: new InputSource(new StringReader(
132: "<link rel=\"stylesheet\" type=\"text/css\" href=\""
133: + cssPath + "\" />")));
134: head.appendChild(head.getOwnerDocument()
135: .importNode(css.getFirstChild(), true));
136: }
137: }
138:
139: oo.setEnableXHTMLCompatibility(true);
140: oo.setUseAposEntity(false);
141:
142: oo.setMIMEType("text/html");
143:
144: return document;
145: } catch (Exception xfce) {
146: xfce.printStackTrace();
147: }
148:
149: // Failed to additionally process this document.
150: // Therefore, we chose to return original one!
151: return document;
152: }
153:
154: public byte[] process(byte[] buteArray, String mimeEncoding,
155: String mimeType) {
156: return buteArray;
157: }
158:
159: public void setName(String name) {
160: this .name = name;
161: }
162:
163: public String getName() {
164: return name;
165: }
166:
167: public boolean shouldProcess(String mimeType) {
168: if (mimeType != null
169: && mimeType
170: .startsWith(FormFacesResponsePostProcessor.mimeType))
171: return true;
172: return false;
173: }
174:
175: public void setLogChannel(LogChannel logChannel) {
176: this .logChannel = logChannel;
177: }
178:
179: public LogChannel getLogChannel() {
180: return logChannel;
181: }
182:
183: public String getOutputMimeType() {
184: return this .outMimeType;
185: }
186:
187: /**
188: * Method returns HEAD node if it is found in source node subtree
189: *
190: * @param document
191: * To search for HEAD node in
192: * @return HEAD node
193: */
194: private Node findHeadNode(Node sourceNode) {
195: if (sourceNode != null) {
196: NodeList nl = sourceNode.getChildNodes();
197: if (nl != null) {
198: for (int i = 0; i < nl.getLength(); i++) {
199: if ("HEAD".equalsIgnoreCase(nl.item(i)
200: .getNodeName())) {
201: return nl.item(i);
202: } else {
203: Node temp = findHeadNode(nl.item(i));
204: if (temp != null) {
205: return temp;
206: }
207: }
208: }
209: }
210: }
211: return null;
212: }
213:
214: public Object clone() {
215: FormFacesResponsePostProcessor ffrpp = new FormFacesResponsePostProcessor(
216: this.getLogChannel());
217: ffrpp.scriptPath = this.scriptPath;
218: ffrpp.cssPath = this.cssPath;
219: return ffrpp;
220: }
221:
222: }
|