KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > transformation > helpers > ParametersRecorder


1 /*
2  * Copyright 1999-2004 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.helpers;
17
18 import org.apache.avalon.framework.parameters.Parameters;
19 import org.apache.excalibur.source.SourceParameters;
20 import org.xml.sax.Attributes JavaDoc;
21 import org.xml.sax.SAXException JavaDoc;
22
23 import java.util.Iterator JavaDoc;
24
25
26 /**
27  * This class records SAX Events and generates Parameters from them
28  * The xml is flat and consists of elements which all have exactly one text node:
29  * <parone>value_one<parone>
30  * <partwo>value_two<partwo>
31  *
32  * @author <a HREF="mailto:cziegeler@s-und-n.de">Carsten Ziegeler</a>
33  * @version CVS $Id: ParametersRecorder.java 30932 2004-07-29 17:35:38Z vgritsenko $
34 */

35 public final class ParametersRecorder
36 extends NOPRecorder {
37
38     private SourceParameters parameters;
39     private String JavaDoc key;
40     private StringBuffer JavaDoc buffer;
41
42     /**
43      * If source is null a new Parameters object is created
44      * Otherwise they are joined.
45      */

46     public ParametersRecorder() {
47         super();
48         this.parameters = new SourceParameters();
49     }
50
51     public SourceParameters getParameters(Parameters source) {
52         if (source != null) {
53             String JavaDoc[] names = source.getNames();
54 // Iterator names = source.getParameterNames();
55
if (names != null) {
56                 String JavaDoc currentParameterName;
57                 for(int i=0; i<names.length; i++) {
58                     currentParameterName = names[i];
59 // while (names.hasNext() == true) {
60
// currentParameterName = (String)names.next();
61
this.parameters.setParameter(currentParameterName, source.getParameter(currentParameterName, ""));
62                 }
63             }
64         }
65         return parameters;
66     }
67
68     public SourceParameters getParameters(SourceParameters source) {
69         if (source != null) {
70             Iterator JavaDoc iter = source.getParameterNames();
71             Iterator JavaDoc valuesIter;
72             String JavaDoc value, parName;
73             while (iter.hasNext() == true) {
74                 parName = (String JavaDoc)iter.next();
75                 valuesIter = source.getParameterValues(parName);
76                 while (valuesIter.hasNext() == true) {
77                     value = (String JavaDoc)valuesIter.next();
78                     this.parameters.setParameter(parName, value);
79                 }
80             }
81         }
82         return parameters;
83     }
84
85     public void startElement(String JavaDoc namespace, String JavaDoc name, String JavaDoc raw,
86                          Attributes JavaDoc attr)
87     throws SAXException JavaDoc {
88         if (this.key == null) {
89             this.key = name;
90             this.buffer = new StringBuffer JavaDoc();
91         }
92     }
93
94     public void endElement(String JavaDoc namespace, String JavaDoc name, String JavaDoc raw)
95     throws SAXException JavaDoc {
96         if (this.key != null && this.key.equals(name) == true) {
97             String JavaDoc value = this.buffer.toString().trim();
98             if (value.length() > 0) {
99                 this.parameters.setParameter(this.key, value);
100             }
101             this.buffer = null;
102             this.key = null;
103         }
104     }
105
106     public void characters(char ary[], int start, int length)
107     throws SAXException JavaDoc {
108         if (this.key != null && this.buffer != null) {
109             String JavaDoc value = new String JavaDoc(ary, start, length).trim();
110             if (value.length() > 0) {
111                 buffer.append(value);
112             } else {
113                 buffer.append(' ');
114             }
115         }
116     }
117
118 }
119
Popular Tags