001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.transformation.helpers;
018:
019: import org.apache.avalon.framework.parameters.Parameters;
020: import org.apache.excalibur.source.SourceParameters;
021: import org.xml.sax.Attributes;
022: import org.xml.sax.SAXException;
023:
024: import java.util.Iterator;
025:
026: /**
027: * This class records SAX Events and generates Parameters from them
028: * The xml is flat and consists of elements which all have exactly one text node:
029: * <parone>value_one<parone>
030: * <partwo>value_two<partwo>
031: *
032: * @author <a href="mailto:cziegeler@s-und-n.de">Carsten Ziegeler</a>
033: * @version CVS $Id: ParametersRecorder.java 433543 2006-08-22 06:22:54Z crossley $
034: */
035: public final class ParametersRecorder extends NOPRecorder {
036:
037: private SourceParameters parameters;
038: private String key;
039: private StringBuffer buffer;
040:
041: /**
042: * If source is null a new Parameters object is created
043: * Otherwise they are joined.
044: */
045: public ParametersRecorder() {
046: super ();
047: this .parameters = new SourceParameters();
048: }
049:
050: public SourceParameters getParameters(Parameters source) {
051: if (source != null) {
052: String[] names = source.getNames();
053: // Iterator names = source.getParameterNames();
054: if (names != null) {
055: String currentParameterName;
056: for (int i = 0; i < names.length; i++) {
057: currentParameterName = names[i];
058: // while (names.hasNext() == true) {
059: // currentParameterName = (String)names.next();
060: this .parameters.setParameter(currentParameterName,
061: source.getParameter(currentParameterName,
062: ""));
063: }
064: }
065: }
066: return parameters;
067: }
068:
069: public SourceParameters getParameters(SourceParameters source) {
070: if (source != null) {
071: Iterator iter = source.getParameterNames();
072: Iterator valuesIter;
073: String value, parName;
074: while (iter.hasNext() == true) {
075: parName = (String) iter.next();
076: valuesIter = source.getParameterValues(parName);
077: while (valuesIter.hasNext() == true) {
078: value = (String) valuesIter.next();
079: this .parameters.setParameter(parName, value);
080: }
081: }
082: }
083: return parameters;
084: }
085:
086: public void startElement(String namespace, String name, String raw,
087: Attributes attr) throws SAXException {
088: if (this .key == null) {
089: this .key = name;
090: this .buffer = new StringBuffer();
091: }
092: }
093:
094: public void endElement(String namespace, String name, String raw)
095: throws SAXException {
096: if (this .key != null && this .key.equals(name) == true) {
097: String value = this .buffer.toString().trim();
098: if (value.length() > 0) {
099: 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 {
108: if (this .key != null && this .buffer != null) {
109: String value = new String(ary, start, length).trim();
110: if (value.length() > 0) {
111: buffer.append(value);
112: } else {
113: buffer.append(' ');
114: }
115: }
116: }
117:
118: }
|