KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > transformation > SourcePropsWritingTransformer


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.transformation;
17
18 import java.io.IOException JavaDoc;
19 import java.util.Map JavaDoc;
20
21 import org.apache.avalon.framework.parameters.Parameters;
22 import org.apache.cocoon.ProcessingException;
23 import org.apache.cocoon.components.source.InspectableSource;
24 import org.apache.cocoon.components.source.helpers.SourceProperty;
25 import org.apache.cocoon.environment.SourceResolver;
26 import org.apache.excalibur.source.Source;
27 import org.w3c.dom.Element JavaDoc;
28 import org.w3c.dom.DocumentFragment JavaDoc;
29 import org.w3c.dom.Node JavaDoc;
30 import org.w3c.dom.NodeList JavaDoc;
31 import org.xml.sax.Attributes JavaDoc;
32 import org.xml.sax.SAXException JavaDoc;
33
34 /**
35  * This transformer allows you to set and remove properties on an InspectableSource.
36  *
37 * <p>Input XML document example:</p>
38  * <pre>
39  * &lt;page&gt;
40  * ...
41  * &lt;source:patch xmlns:source="http://apache.org/cocoon/propwrite/1.0"&gt;
42  * &lt;source:source&gt;webdav://localhost/webdav/step1/repo/contentA.xml&lt;/source:source&gt;
43  * &lt;source:set&gt;
44  * &lt;myns:author xmlns:myns="meta"&gt;me&lt;/myns:author&gt;
45  * &lt;/source:set&gt;
46  * &lt;source:remove&gt;
47  * &lt;myns:title xmlns:myns="meta"/&gt;
48  * &lt;/source:remove&gt;
49  * &lt;/source:patch&gt;
50  * ...
51  * &lt;/page&gt;
52  * </pre>
53  *
54  * @author <a HREF="mailto:gcasper@s-und-n.de">Guido Casper</a>
55  * @version $Id: SourcePropsWritingTransformer.java 168366 2005-05-05 18:23:18Z vgritsenko $
56  */

57 public class SourcePropsWritingTransformer extends AbstractSAXTransformer {
58
59     public static final String JavaDoc SPWT_URI = "http://apache.org/cocoon/propwrite/1.0";
60
61     /** incoming elements */
62     public static final String JavaDoc PATCH_ELEMENT = "patch";
63     public static final String JavaDoc SOURCE_ELEMENT = "source";
64     public static final String JavaDoc SET_ELEMENT = "set";
65     public static final String JavaDoc REMOVE_ELEMENT = "remove";
66
67     /** The current state */
68     private static final int STATE_OUTSIDE = 0;
69     private static final int STATE_PATCH = 1;
70     private static final int STATE_SOURCE = 2;
71     private static final int STATE_SET = 3;
72     private static final int STATE_REMOVE = 4;
73
74     private int state;
75
76     /**
77      * Constructor
78      * Set the namespace
79      */

80     public SourcePropsWritingTransformer() {
81         super.defaultNamespaceURI = SPWT_URI;
82     }
83
84     public void recycle() {
85         this.state = STATE_OUTSIDE;
86         super.recycle();
87     }
88
89     /**
90      * Set the <code>SourceResolver</code>, objectModel <code>Map</code>,
91      * the source and sitemap <code>Parameters</code> used to process the request.
92      *
93      * @param resolver Source Resolver
94      * @param objectModel Object model
95      * @param src URI of the source attribute
96      * @param par Parameters for the transformer
97      */

98     public void setup(SourceResolver resolver, Map JavaDoc objectModel, String JavaDoc src, Parameters par)
99     throws ProcessingException, SAXException JavaDoc, IOException JavaDoc {
100         super.setup(resolver, objectModel, src, par);
101         this.state = STATE_OUTSIDE;
102     }
103
104     /**
105      * Receive notification of the beginning of an element.
106      *
107      * @param uri The Namespace URI, or the empty string if the element has no
108      * Namespace URI or if Namespace
109      * processing is not being performed.
110      * @param name The local name (without prefix), or the empty string if
111      * Namespace processing is not being performed.
112      * @param raw The raw XML 1.0 name (with prefix), or the empty string if
113      * raw names are not available.
114      * @param attr The attributes attached to the element. If there are no
115      * attributes, it shall be an empty Attributes object.
116      */

117     public void startTransformingElement(String JavaDoc uri, String JavaDoc name, String JavaDoc raw, Attributes JavaDoc attr)
118     throws SAXException JavaDoc, IOException JavaDoc, ProcessingException {
119         if (this.getLogger().isDebugEnabled()) {
120             this.getLogger().debug("BEGIN startTransformingElement uri=" + uri +
121                               ", name=" + name + ", raw=" + raw + ", attr=" + attr);
122         }
123         // Element: patch
124
if (this.state == STATE_OUTSIDE && name.equals(PATCH_ELEMENT)) {
125             this.state = STATE_PATCH;
126             this.stack.push("END");
127
128         // Element: source
129
} else if (this.state == STATE_PATCH && name.equals(SOURCE_ELEMENT)) {
130             this.state = STATE_SOURCE;
131             this.startTextRecording();
132
133         // Element: props
134
} else if (this.state == STATE_PATCH && name.equals(SET_ELEMENT)) {
135             this.state = STATE_SET;
136             this.startRecording();
137         } else if (this.state == STATE_PATCH && name.equals(REMOVE_ELEMENT)) {
138             this.state = STATE_REMOVE;
139             this.startRecording();
140         } else {
141             super.startTransformingElement(uri, name, raw, attr);
142         }
143
144         if (this.getLogger().isDebugEnabled() == true) {
145             this.getLogger().debug("END startTransformingElement");
146         }
147     }
148
149
150     /**
151      * Receive notification of the end of an element.
152      *
153      * @param uri The Namespace URI, or the empty string if the element has no
154      * Namespace URI or if Namespace
155      * processing is not being performed.
156      * @param name The local name (without prefix), or the empty string if
157      * Namespace processing is not being performed.
158      * @param raw The raw XML 1.0 name (with prefix), or the empty string if
159      * raw names are not available.
160      */

161     public void endTransformingElement(String JavaDoc uri, String JavaDoc name, String JavaDoc raw)
162     throws SAXException JavaDoc, IOException JavaDoc, ProcessingException {
163         if (this.getLogger().isDebugEnabled() == true) {
164             this.getLogger().debug("BEGIN endTransformingElement uri=" + uri +
165                               ", name=" + name +
166                               ", raw=" + raw);
167         }
168
169         // Element: patch
170
if ((this.state == STATE_PATCH && name.equals(PATCH_ELEMENT))) {
171             this.state = STATE_OUTSIDE;
172             String JavaDoc sourceName = null;
173             String JavaDoc tag = null;
174             DocumentFragment JavaDoc setfrag = null;
175             DocumentFragment JavaDoc removefrag = null;
176             do {
177                 tag = (String JavaDoc)this.stack.pop();
178                 if (tag.equals(SOURCE_ELEMENT)) {
179                     sourceName = (String JavaDoc)this.stack.pop();
180                 } else if (tag.equals(SET_ELEMENT)) {
181                     setfrag = (DocumentFragment JavaDoc)this.stack.pop();
182                 } else if (tag.equals(REMOVE_ELEMENT)) {
183                     removefrag = (DocumentFragment JavaDoc)this.stack.pop();
184                 }
185             } while ( !tag.equals("END") );
186             if (setfrag != null) {
187                 NodeList JavaDoc list = setfrag.getChildNodes();
188                 Node JavaDoc node = null;
189                 for (int i=0; i<list.getLength(); i++) {
190                     node = list.item(i);
191                     if (node instanceof Element JavaDoc) {
192                         this.setProperty(sourceName, (Element JavaDoc) node);
193                     }
194                 }
195             }
196             if (removefrag != null) {
197                 NodeList JavaDoc list = removefrag.getChildNodes();
198                 Node JavaDoc node = null;
199                 for (int i=0; i<list.getLength(); i++) {
200                     node = list.item(i);
201                     if (node instanceof Element JavaDoc) {
202                         this.removeProperty(sourceName, (Element JavaDoc) node);
203                     }
204                 }
205             }
206
207         // Element: source
208
} else if (this.state == STATE_SOURCE && name.equals(SOURCE_ELEMENT)) {
209             this.state = STATE_PATCH;
210             String JavaDoc sourceName = this.endTextRecording();
211             this.stack.push(sourceName);
212             this.stack.push(SOURCE_ELEMENT);
213
214         // Element: set
215
} else if (this.state == STATE_SET && name.equals(SET_ELEMENT)) {
216             this.state = STATE_PATCH;
217             this.stack.push(this.endRecording());
218             this.stack.push(SET_ELEMENT);
219
220         // Element: remove
221
} else if (this.state == STATE_REMOVE && name.equals(REMOVE_ELEMENT)) {
222             this.state = STATE_PATCH;
223             this.stack.push(this.endRecording());
224             this.stack.push(REMOVE_ELEMENT);
225
226         // default
227
} else {
228             super.endTransformingElement(uri, name, raw);
229         }
230
231         if (this.getLogger().isDebugEnabled() == true) {
232             this.getLogger().debug("END endTransformingElement");
233         }
234     }
235
236     private void setProperty(String JavaDoc src, Element JavaDoc element)
237     throws ProcessingException {
238         if (src != null && element != null) {
239             try {
240                 Source source = this.resolver.resolveURI(src);
241                 if (source instanceof InspectableSource) {
242                     SourceProperty property = new SourceProperty(element);
243                     ((InspectableSource)source).setSourceProperty(property);
244
245                 } else {
246                     getLogger().error("Cannot set properties on " + src +
247                                       ": not an inspectable source");
248                 }
249             } catch (Exception JavaDoc e) {
250                 throw new ProcessingException("Error setting properties on "+src, e);
251             }
252         } else {
253             getLogger().error("Error setting properties on "+src);
254         }
255     }
256
257     private void removeProperty(String JavaDoc src, Element JavaDoc element)
258     throws ProcessingException {
259
260         if (src != null && element != null) {
261             try {
262                 Source source = this.resolver.resolveURI(src);
263                 if (source instanceof InspectableSource) {
264                     ((InspectableSource)source).removeSourceProperty(
265                         element.getNamespaceURI(),element.getLocalName());
266
267                 } else {
268                     getLogger().error("Cannot remove properties on " + src +
269                                       ": not an inspectable source");
270                 }
271             } catch (Exception JavaDoc e) {
272                 throw new ProcessingException("Error removing properties on "+src, e);
273             }
274         } else {
275             getLogger().error("Error removing properties on "+src);
276         }
277     }
278 }
279
Popular Tags