You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by si...@apache.org on 2012/04/23 17:58:44 UTC
svn commit: r1329308 - in /cocoon/cocoon3/trunk/cocoon-sax/src:
main/java/org/apache/cocoon/sax/util/SAXEventsBuilder.java
test/java/org/apache/cocoon/sax/util/SAXEventsBuilderTestCase.java
Author: simonetripodi
Date: Mon Apr 23 15:58:43 2012
New Revision: 1329308
URL: http://svn.apache.org/viewvc?rev=1329308&view=rev
Log:
added a simple yet powerful SAX ContentHandler wrapper to assist manual SAX events generation
Added:
cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/util/SAXEventsBuilder.java (with props)
cocoon/cocoon3/trunk/cocoon-sax/src/test/java/org/apache/cocoon/sax/util/SAXEventsBuilderTestCase.java (with props)
Added: cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/util/SAXEventsBuilder.java
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/util/SAXEventsBuilder.java?rev=1329308&view=auto
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/util/SAXEventsBuilder.java (added)
+++ cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/util/SAXEventsBuilder.java Mon Apr 23 15:58:43 2012
@@ -0,0 +1,168 @@
+/*
+ * 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.apache.cocoon.xml.sax.AttributesImpl;
+import org.xml.sax.ContentHandler;
+import org.xml.sax.SAXException;
+
+public class SAXEventsBuilder
+{
+
+ private static final String EMPTY_NS = "";
+
+ private static final String CDATA_TYPE = "CDATA";
+
+ public static SAXEventsBuilder newDocument( ContentHandler contentHandler )
+ throws SAXException
+ {
+ contentHandler.startDocument();
+ return wrap( contentHandler );
+ }
+
+ public static SAXEventsBuilder wrap( ContentHandler contentHandler )
+ {
+ return new SAXEventsBuilder( null, null, null, null, contentHandler );
+ }
+
+ public static <T> Attribute attribute( String name, T value )
+ {
+ return attribute( EMPTY_NS, name, name, CDATA_TYPE, value );
+ }
+
+ public static <T> Attribute attribute( String uri, String localName, String qName, String type, T value )
+ {
+ return new Attribute( uri, localName, qName, type, String.valueOf( value ) );
+ }
+
+ private final SAXEventsBuilder previous;
+
+ protected final String uri;
+
+ protected final String localName;
+
+ protected final String qName;
+
+ protected final ContentHandler contentHandler;
+
+ /**
+ * Hidden constructor, this class cannot be instantiated earlier;
+ */
+ private SAXEventsBuilder( SAXEventsBuilder previous, String uri, String localName, String qName, ContentHandler contentHandler )
+ {
+ this.previous = previous;
+ this.uri = uri;
+ this.localName = localName;
+ this.qName = qName;
+ this.contentHandler = contentHandler;
+ }
+
+ public SAXEventsBuilder start( String localName, Attribute...attributes )
+ throws SAXException
+ {
+ return start( EMPTY_NS, localName, localName, attributes );
+ }
+
+ public SAXEventsBuilder start( String uri, String localName, String qName, Attribute...attributes )
+ throws SAXException
+ {
+ AttributesImpl atts = new AttributesImpl();
+
+ for ( Attribute attribute : attributes )
+ {
+ atts.addAttribute( attribute.uri, attribute.getLocalName(), attribute.getqName(), attribute.getType(), attribute.getValue() );
+ }
+
+ contentHandler.startElement( uri, localName, qName, atts );
+ return new SAXEventsBuilder( this, uri, localName, qName, contentHandler );
+ }
+
+ public <T> SAXEventsBuilder body( T elementBody )
+ throws SAXException
+ {
+ if ( elementBody == null )
+ {
+ return this;
+ }
+ String elementStringBody = String.valueOf( elementBody );
+ contentHandler.characters( elementStringBody.toCharArray(), 0, elementStringBody.length() );
+ return this;
+ }
+
+ public SAXEventsBuilder end()
+ throws SAXException
+ {
+ contentHandler.endElement( uri, localName, qName );
+ return previous;
+ }
+
+ public void endDocument()
+ throws SAXException
+ {
+ contentHandler.endDocument();
+ }
+
+ public static final class Attribute
+ {
+
+ private final String uri;
+
+ private final String localName;
+
+ private final String qName;
+
+ private final String type;
+
+ private final String value;
+
+ private Attribute( String uri, String localName, String qName, String type, String value )
+ {
+ this.uri = uri;
+ this.localName = localName;
+ this.qName = qName;
+ this.type = type;
+ this.value = value;
+ }
+
+ public String getUri()
+ {
+ return uri;
+ }
+
+ public String getLocalName()
+ {
+ return localName;
+ }
+
+ public String getqName()
+ {
+ return qName;
+ }
+
+ public String getType()
+ {
+ return type;
+ }
+
+ public String getValue()
+ {
+ return value;
+ }
+
+ }
+
+}
Propchange: cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/util/SAXEventsBuilder.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/util/SAXEventsBuilder.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/util/SAXEventsBuilder.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: cocoon/cocoon3/trunk/cocoon-sax/src/test/java/org/apache/cocoon/sax/util/SAXEventsBuilderTestCase.java
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-sax/src/test/java/org/apache/cocoon/sax/util/SAXEventsBuilderTestCase.java?rev=1329308&view=auto
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-sax/src/test/java/org/apache/cocoon/sax/util/SAXEventsBuilderTestCase.java (added)
+++ cocoon/cocoon3/trunk/cocoon-sax/src/test/java/org/apache/cocoon/sax/util/SAXEventsBuilderTestCase.java Mon Apr 23 15:58:43 2012
@@ -0,0 +1,71 @@
+/*
+ * 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 static junit.framework.Assert.assertTrue;
+import static org.apache.cocoon.sax.util.SAXEventsBuilder.newDocument;
+
+import java.io.ByteArrayOutputStream;
+
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.sax.SAXTransformerFactory;
+import javax.xml.transform.sax.TransformerHandler;
+import javax.xml.transform.stream.StreamResult;
+
+import org.custommonkey.xmlunit.Diff;
+import org.junit.Test;
+
+public final class SAXEventsBuilderTestCase
+{
+
+ @Test
+ public void pomAlike()
+ throws Exception
+ {
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+
+ SAXTransformerFactory transformerFactory = (SAXTransformerFactory) TransformerFactory.newInstance();
+ TransformerHandler transformerHandler = transformerFactory.newTransformerHandler();
+ transformerHandler.setResult( new StreamResult( baos ) );
+
+ newDocument( transformerHandler )
+ .start( "project" )
+ .start( "modelVersion" ).body( "4.0.0" ).end()
+ .start( "groupId" ).body( "org.apache.cocoon.sax" ).end()
+ .start( "artifactId" ).body( "cocoon-sax" ).end()
+ .start( "version" ).body( "3.0.0-beta-1" ).end()
+ .start( "dependencies" )
+ .start( "dependency" )
+ .start( "groupId" ).body( "org.apache.cocoon.pipeline" ).end()
+ .start( "artifactId" ).body( "cocoon-pipeline" ).end()
+ .end()
+ .start( "dependency" )
+ .start( "groupId" ).body( "org.apache.cocoon" ).end()
+ .start( "artifactId" ).body( "cocoon-xml" ).end()
+ .end()
+ .end()
+ .end()
+ .endDocument();
+
+ String expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><project><modelVersion>4.0.0</modelVersion><groupId>org.apache.cocoon.sax</groupId><artifactId>cocoon-sax</artifactId><version>3.0.0-beta-1</version><dependencies><dependency><groupId>org.apache.cocoon.pipeline</groupId><artifactId>cocoon-pipeline</artifactId></dependency><dependency><groupId>org.apache.cocoon</groupId><artifactId>cocoon-xml</artifactId></dependency></dependencies></project>";
+ String actual = new String(baos.toByteArray());
+
+ Diff diff = new Diff(expected, actual);
+ assertTrue( "SAX wrapper didn't work as expected " + diff, diff.identical() );
+ }
+
+}
Propchange: cocoon/cocoon3/trunk/cocoon-sax/src/test/java/org/apache/cocoon/sax/util/SAXEventsBuilderTestCase.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: cocoon/cocoon3/trunk/cocoon-sax/src/test/java/org/apache/cocoon/sax/util/SAXEventsBuilderTestCase.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: cocoon/cocoon3/trunk/cocoon-sax/src/test/java/org/apache/cocoon/sax/util/SAXEventsBuilderTestCase.java
------------------------------------------------------------------------------
svn:mime-type = text/plain