You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by th...@apache.org on 2011/09/15 14:28:11 UTC
svn commit: r1171073 - in
/cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax:
AbstractSAXTransformer.java util/AttributeTypes.java
util/AttributesImpl.java util/ImmutableAttributesImpl.java util/XMLUtils.java
Author: thorsten
Date: Thu Sep 15 12:28:11 2011
New Revision: 1171073
URL: http://svn.apache.org/viewvc?rev=1171073&view=rev
Log:
Migrating util classes from cocoon2.2 after cleaning them from avalon so that they are usable in c3. This classes are used by a lot of c2.2 components.
Added:
cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/util/AttributeTypes.java (with props)
cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/util/AttributesImpl.java (with props)
cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/util/ImmutableAttributesImpl.java (with props)
Modified:
cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/AbstractSAXTransformer.java
cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/util/XMLUtils.java
Modified: cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/AbstractSAXTransformer.java
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/AbstractSAXTransformer.java?rev=1171073&r1=1171072&r2=1171073&view=diff
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/AbstractSAXTransformer.java (original)
+++ cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/AbstractSAXTransformer.java Thu Sep 15 12:28:11 2011
@@ -22,6 +22,8 @@ import java.util.List;
import org.apache.cocoon.pipeline.ProcessingException;
import org.apache.cocoon.sax.util.SAXConsumerAdapter;
import org.apache.cocoon.xml.sax.SAXBuffer;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.Locator;
@@ -29,6 +31,8 @@ import org.xml.sax.SAXException;
public abstract class AbstractSAXTransformer extends AbstractSAXProducer implements SAXConsumer {
+ private static final Logger LOG =
+ LoggerFactory.getLogger(AbstractSAXTransformer.class);
private List<String[]> namespaces = new ArrayList<String[]>();
public void characters(char[] ch, int start, int length) throws SAXException {
@@ -78,7 +82,22 @@ public abstract class AbstractSAXTransfo
this.getSAXConsumer().endPrefixMapping(prefix);
}
+ /**
+ * Find prefix mapping for the given namespace URI.
+ * @return Prefix mapping or null if no prefix defined
+ */
+ protected String findPrefixMapping(String uri) {
+ final int l = this.namespaces.size();
+ for (int i = 0; i < l; i++) {
+ String[] prefixAndUri = (String[]) this.namespaces.get(i);
+ if (prefixAndUri[1].equals(uri)) {
+ return prefixAndUri[0];
+ }
+ }
+ return null;
+ }
+
public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
this.getSAXConsumer().ignorableWhitespace(ch, start, length);
}
@@ -192,4 +211,58 @@ public abstract class AbstractSAXTransfo
this.getSAXConsumer().endPrefixMapping(prefixAndUri[0]);
}
}
+ /**
+ * Start recording of a text.
+ * No events forwarded, and all characters events
+ * are collected into a string.
+ */
+ public void startTextRecording()
+ throws SAXException {
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("Start text recording");
+ }
+ setRecorder(new TextRecorder());
+ sendStartPrefixMapping();
+ }
+
+ /**
+ * Stop recording of text and return the recorded information.
+ * @return The String, trimmed.
+ */
+ public String endTextRecording()
+ throws SAXException {
+ sendEndPrefixMapping();
+
+ TextRecorder recorder = (TextRecorder) removeRecorder();
+ String text = recorder.getText();
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("End text recording. Text=" + text);
+ }
+ return text;
+ }
+
+ @SuppressWarnings("serial")
+ class TextRecorder extends SAXBuffer{
+
+ /**
+ * Buffer collecting all character events.
+ */
+ private StringBuffer buffer;
+
+ public TextRecorder() {
+ super();
+ this.buffer = new StringBuffer();
+ }
+
+ public void characters(char ary[], int start, int length) {
+ this.buffer.append(ary, start, length);
+ }
+
+ /**
+ * @return Recorded text so far, trimmed.
+ */
+ public String getText() {
+ return this.buffer.toString().trim();
+ }
+ }
}
Added: cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/util/AttributeTypes.java
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/util/AttributeTypes.java?rev=1171073&view=auto
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/util/AttributeTypes.java (added)
+++ cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/util/AttributeTypes.java Thu Sep 15 12:28:11 2011
@@ -0,0 +1,35 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cocoon.sax.util;
+
+public interface AttributeTypes {
+ String CDATA = "CDATA";
+ String ENTITY = "ENTITY";
+ String ENTITIES = "ENTITIES";
+ String ID = "ID";
+ String IDREF = "IDREF";
+ String IDREFS = "IDREFS";
+ String NAME = "NAME";
+ String NAMES = "NAMES";
+ String NMTOKEN = "NMTOKEN";
+ String NMTOKENS = "NMTOKENS";
+ String NOTATION = "NOTATION";
+ String NUMBER = "NUMBER";
+ String NUMBERS = "NUMBERS";
+ String NUTOKEN = "NUTOKEN";
+ String NUTOKENS = "NUTOKENS";
+}
Propchange: cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/util/AttributeTypes.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/util/AttributesImpl.java
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/util/AttributesImpl.java?rev=1171073&view=auto
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/util/AttributesImpl.java (added)
+++ cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/util/AttributesImpl.java Thu Sep 15 12:28:11 2011
@@ -0,0 +1,114 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cocoon.sax.util;
+
+import org.xml.sax.Attributes;
+
+/**
+ * A helper Class creating SAX Attributes
+ *
+ * @version $Id: AttributesImpl.java 587751 2007-10-24 02:41:36Z vgritsenko $
+ */
+public class AttributesImpl extends org.xml.sax.helpers.AttributesImpl {
+
+ /**
+ * Constructor
+ */
+ public AttributesImpl() {
+ super();
+ }
+
+ /**
+ * Constructor
+ */
+ public AttributesImpl(Attributes attr) {
+ super(attr);
+ }
+
+ /**
+ * Add an attribute of type CDATA with empty Namespace to the end of the list.
+ *
+ * <p>For the sake of speed, this method does no checking
+ * to see if the attribute is already in the list: that is
+ * the responsibility of the application.</p>
+ *
+ * @param localName The local name.
+ * @param value The attribute value.
+ */
+ public void addCDATAAttribute(String localName, String value) {
+ addAttribute("", localName, localName, AttributeTypes.CDATA, value);
+ }
+
+ /**
+ * Add an attribute of type CDATA with the namespace to the end of the list.
+ *
+ * <p>For the sake of speed, this method does no checking
+ * to see if the attribute is already in the list: that is
+ * the responsibility of the application.</p>
+ *
+ * @param namespace The namespace.
+ * @param localName The local name.
+ * @param value The attribute value.
+ */
+ public void addCDATAAttribute(String namespace, String localName, String value) {
+ addAttribute(namespace, localName, localName, AttributeTypes.CDATA, value);
+ }
+
+ /**
+ * Add an attribute of type CDATA to the end of the list.
+ *
+ * <p>For the sake of speed, this method does no checking
+ * to see if the attribute is already in the list: that is
+ * the responsibility of the application.</p>
+ *
+ * @param uri The Namespace URI, or the empty string if
+ * none is available or Namespace processing is not
+ * being performed.
+ * @param localName The local name, or the empty string if
+ * Namespace processing is not being performed.
+ * @param qName The qualified (prefixed) name, or the empty string
+ * if qualified names are not available.
+ * @param value The attribute value.
+ */
+ public void addCDATAAttribute(String uri,
+ String localName,
+ String qName,
+ String value) {
+ addAttribute(uri, localName, qName, AttributeTypes.CDATA, value);
+ }
+
+ /**
+ * Remove an attribute
+ */
+ public void removeAttribute(String localName) {
+ final int index = this.getIndex(localName);
+ if ( index != -1 ) {
+ this.removeAttribute(index);
+ }
+ }
+
+ /**
+ * Remove an attribute
+ */
+ public void removeAttribute(String uri, String localName) {
+ final int index = this.getIndex(uri, localName);
+ if ( index != -1 ) {
+ this.removeAttribute(index);
+ }
+ }
+}
+
\ No newline at end of file
Propchange: cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/util/AttributesImpl.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/util/ImmutableAttributesImpl.java
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/util/ImmutableAttributesImpl.java?rev=1171073&view=auto
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/util/ImmutableAttributesImpl.java (added)
+++ cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/util/ImmutableAttributesImpl.java Thu Sep 15 12:28:11 2011
@@ -0,0 +1,75 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cocoon.sax.util;
+
+import org.xml.sax.Attributes;
+
+/**
+ * Immutable attributes
+ *
+ * @version $Id: ImmutableAttributesImpl.java 587751 2007-10-24 02:41:36Z vgritsenko $
+ */
+public class ImmutableAttributesImpl extends AttributesImpl {
+
+ public ImmutableAttributesImpl() {
+ super();
+ }
+
+ public ImmutableAttributesImpl(Attributes attrs) {
+ super(attrs);
+ }
+
+ public void clear() {
+ throw new UnsupportedOperationException("immutable attributes");
+ }
+
+ public void removeAttribute(int index) {
+ throw new UnsupportedOperationException("immutable attributes");
+ }
+
+ public void setLocalName(int index, String localName) {
+ throw new UnsupportedOperationException("immutable attributes");
+ }
+
+ public void setQName(int index, String qName) {
+ throw new UnsupportedOperationException("immutable attributes");
+ }
+
+ public void setType(int index, String type) {
+ throw new UnsupportedOperationException("immutable attributes");
+ }
+
+ public void setURI(int index, String uri) {
+ throw new UnsupportedOperationException("immutable attributes");
+ }
+
+ public void setValue(int index, String value) {
+ throw new UnsupportedOperationException("immutable attributes");
+ }
+
+ public void setAttributes(Attributes atts) {
+ throw new UnsupportedOperationException("immutable attributes");
+ }
+
+ public void setAttribute(int index, String uri, String localName, String qName, String type, String value) {
+ throw new UnsupportedOperationException("immutable attributes");
+ }
+
+ public void addAttribute(String uri, String localName, String qName, String type, String value) {
+ throw new UnsupportedOperationException("immutable attributes");
+ }
+}
Propchange: cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/util/ImmutableAttributesImpl.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/util/XMLUtils.java
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/util/XMLUtils.java?rev=1171073&r1=1171072&r2=1171073&view=diff
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/util/XMLUtils.java (original)
+++ cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/util/XMLUtils.java Thu Sep 15 12:28:11 2011
@@ -36,6 +36,7 @@ import org.apache.cocoon.pipeline.util.U
import org.apache.cocoon.sax.SAXConsumer;
import org.apache.cocoon.xml.sax.EmbeddedSAXPipe;
import org.apache.cocoon.xml.sax.SAXBuffer;
+import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
@@ -51,7 +52,10 @@ public abstract class XMLUtils {
private static final SAXTransformerFactory SAX_TRANSFORMER_FACTORY = (SAXTransformerFactory) TransformerFactory
.newInstance();
-
+ /**
+ * Empty attributes immutable object.
+ */
+ public static final Attributes EMPTY_ATTRIBUTES = new ImmutableAttributesImpl();
/**
* Serialize the content of a {@link SAXBuffer} into an {@link OutputStream}.
*