Author: vsiveton
Date: Sat Aug  4 03:37:23 2007
New Revision: 562686

URL: http://svn.apache.org/viewvc?view=rev&rev=562686
Log:
o added abstract classes for text/xml Parser and Sink to encapsulate read/write 
concerns

Added:
    
maven/doxia/doxia/trunk/doxia-core/src/main/java/org/apache/maven/doxia/parser/AbstractTextParser.java
   (with props)
    
maven/doxia/doxia/trunk/doxia-core/src/main/java/org/apache/maven/doxia/parser/AbstractXmlParser.java
   (with props)
    
maven/doxia/doxia/trunk/doxia-core/src/main/java/org/apache/maven/doxia/sink/AbstractTextSink.java
   (with props)
    
maven/doxia/doxia/trunk/doxia-core/src/main/java/org/apache/maven/doxia/sink/AbstractXmlSink.java
   (with props)

Added: 
maven/doxia/doxia/trunk/doxia-core/src/main/java/org/apache/maven/doxia/parser/AbstractTextParser.java
URL: 
http://svn.apache.org/viewvc/maven/doxia/doxia/trunk/doxia-core/src/main/java/org/apache/maven/doxia/parser/AbstractTextParser.java?view=auto&rev=562686
==============================================================================
--- 
maven/doxia/doxia/trunk/doxia-core/src/main/java/org/apache/maven/doxia/parser/AbstractTextParser.java
 (added)
+++ 
maven/doxia/doxia/trunk/doxia-core/src/main/java/org/apache/maven/doxia/parser/AbstractTextParser.java
 Sat Aug  4 03:37:23 2007
@@ -0,0 +1,37 @@
+package org.apache.maven.doxia.parser;
+
+/*
+ * 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.
+ */
+
+import org.apache.maven.doxia.markup.TextMarkup;
+
+/**
+ * An abstract class that defines some convenience methods for 
<code>Text</code> parsers.
+ *
+ * @author <a href="mailto:[EMAIL PROTECTED]">Vincent Siveton</a>
+ * @version $Id$
+ * @since 1.0
+ */
+public abstract class AbstractTextParser
+    extends AbstractParser
+    implements TextMarkup
+{
+
+}
+

Propchange: 
maven/doxia/doxia/trunk/doxia-core/src/main/java/org/apache/maven/doxia/parser/AbstractTextParser.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
maven/doxia/doxia/trunk/doxia-core/src/main/java/org/apache/maven/doxia/parser/AbstractTextParser.java
------------------------------------------------------------------------------
    svn:keywords = "Author Date Id Revision"

Added: 
maven/doxia/doxia/trunk/doxia-core/src/main/java/org/apache/maven/doxia/parser/AbstractXmlParser.java
URL: 
http://svn.apache.org/viewvc/maven/doxia/doxia/trunk/doxia-core/src/main/java/org/apache/maven/doxia/parser/AbstractXmlParser.java?view=auto&rev=562686
==============================================================================
--- 
maven/doxia/doxia/trunk/doxia-core/src/main/java/org/apache/maven/doxia/parser/AbstractXmlParser.java
 (added)
+++ 
maven/doxia/doxia/trunk/doxia-core/src/main/java/org/apache/maven/doxia/parser/AbstractXmlParser.java
 Sat Aug  4 03:37:23 2007
@@ -0,0 +1,135 @@
+package org.apache.maven.doxia.parser;
+
+/*
+ * 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.
+ */
+
+import java.io.IOException;
+import java.io.Reader;
+
+import org.apache.maven.doxia.macro.MacroExecutionException;
+import org.apache.maven.doxia.markup.XmlMarkup;
+import org.apache.maven.doxia.sink.Sink;
+import org.codehaus.plexus.util.xml.pull.MXParser;
+import org.codehaus.plexus.util.xml.pull.XmlPullParser;
+import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
+
+/**
+ * An abstract class that defines some convenience methods for 
<code>XML</code> parsers.
+ *
+ * @author <a href="mailto:[EMAIL PROTECTED]">Vincent Siveton</a>
+ * @version $Id$
+ * @since 1.0
+ */
+public abstract class AbstractXmlParser
+    extends AbstractParser
+    implements XmlMarkup
+{
+    /** [EMAIL PROTECTED] */
+    public void parse( Reader source, Sink sink )
+        throws ParseException
+    {
+        try
+        {
+            XmlPullParser parser = new MXParser();
+
+            parser.setInput( source );
+
+            parseXml( parser, sink );
+        }
+        catch ( XmlPullParserException ex )
+        {
+            throw new ParseException( "Error parsing the model: " + 
ex.getMessage(), ex );
+        }
+        catch ( MacroExecutionException ex )
+        {
+            throw new ParseException( "Macro execution failed: " + 
ex.getMessage(), ex );
+        }
+    }
+
+    /**
+     * Parse the model from the XmlPullParser into the given sink.
+     *
+     * @param parser A parser.
+     * @param sink the sink to receive the events.
+     * @throws XmlPullParserException if there's a problem parsing the model
+     * @throws MacroExecutionException if there's a problem executing a macro
+     */
+    private void parseXml( XmlPullParser parser, Sink sink )
+        throws XmlPullParserException, MacroExecutionException
+    {
+        int eventType = parser.getEventType();
+
+        while ( eventType != XmlPullParser.END_DOCUMENT )
+        {
+            if ( eventType == XmlPullParser.START_TAG )
+            {
+                handleStartTag( parser, sink );
+            }
+            else if ( eventType == XmlPullParser.END_TAG )
+            {
+                handleEndTag( parser, sink );
+            }
+            else if ( eventType == XmlPullParser.TEXT )
+            {
+                handleText( parser, sink );
+            }
+
+            try
+            {
+                eventType = parser.next();
+            }
+            catch ( IOException io )
+            {
+                throw new XmlPullParserException( "IOException: " + 
io.getMessage(), parser, io );
+            }
+        }
+    }
+
+    /**
+     * Goes through the possible start tags.
+     *
+     * @param parser A parser.
+     * @param sink the sink to receive the events.
+     * @throws XmlPullParserException if there's a problem parsing the model
+     * @throws MacroExecutionException if there's a problem executing a macro
+     */
+    protected abstract void handleStartTag( XmlPullParser parser, Sink sink )
+        throws XmlPullParserException, MacroExecutionException;
+
+    /**
+     * Goes through the possible end tags.
+     *
+     * @param parser A parser.
+     * @param sink the sink to receive the events.
+     * @throws XmlPullParserException if there's a problem parsing the model
+     * @throws MacroExecutionException if there's a problem executing a macro
+     */
+    protected abstract void handleEndTag( XmlPullParser parser, Sink sink )
+        throws XmlPullParserException, MacroExecutionException;
+
+    /**
+     * Handles text events.
+     *
+     * @param parser A parser.
+     * @param sink the sink to receive the events.
+     * @throws XmlPullParserException if there's a problem parsing the model
+     */
+    protected abstract void handleText( XmlPullParser parser, Sink sink )
+        throws XmlPullParserException;
+}

Propchange: 
maven/doxia/doxia/trunk/doxia-core/src/main/java/org/apache/maven/doxia/parser/AbstractXmlParser.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
maven/doxia/doxia/trunk/doxia-core/src/main/java/org/apache/maven/doxia/parser/AbstractXmlParser.java
------------------------------------------------------------------------------
    svn:keywords = "Author Date Id Revision"

Added: 
maven/doxia/doxia/trunk/doxia-core/src/main/java/org/apache/maven/doxia/sink/AbstractTextSink.java
URL: 
http://svn.apache.org/viewvc/maven/doxia/doxia/trunk/doxia-core/src/main/java/org/apache/maven/doxia/sink/AbstractTextSink.java?view=auto&rev=562686
==============================================================================
--- 
maven/doxia/doxia/trunk/doxia-core/src/main/java/org/apache/maven/doxia/sink/AbstractTextSink.java
 (added)
+++ 
maven/doxia/doxia/trunk/doxia-core/src/main/java/org/apache/maven/doxia/sink/AbstractTextSink.java
 Sat Aug  4 03:37:23 2007
@@ -0,0 +1,36 @@
+package org.apache.maven.doxia.sink;
+
+/*
+ * 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.
+ */
+
+import org.apache.maven.doxia.markup.TextMarkup;
+
+/**
+ * An abstract <code>Sink</code> for text markup syntax.
+ *
+ * @author <a href="mailto:[EMAIL PROTECTED]">Vincent Siveton</a>
+ * @version $Id$
+ */
+public abstract class AbstractTextSink
+    extends SinkAdapter
+    implements TextMarkup
+{
+
+}
+

Propchange: 
maven/doxia/doxia/trunk/doxia-core/src/main/java/org/apache/maven/doxia/sink/AbstractTextSink.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
maven/doxia/doxia/trunk/doxia-core/src/main/java/org/apache/maven/doxia/sink/AbstractTextSink.java
------------------------------------------------------------------------------
    svn:keywords = "Author Date Id Revision"

Added: 
maven/doxia/doxia/trunk/doxia-core/src/main/java/org/apache/maven/doxia/sink/AbstractXmlSink.java
URL: 
http://svn.apache.org/viewvc/maven/doxia/doxia/trunk/doxia-core/src/main/java/org/apache/maven/doxia/sink/AbstractXmlSink.java?view=auto&rev=562686
==============================================================================
--- 
maven/doxia/doxia/trunk/doxia-core/src/main/java/org/apache/maven/doxia/sink/AbstractXmlSink.java
 (added)
+++ 
maven/doxia/doxia/trunk/doxia-core/src/main/java/org/apache/maven/doxia/sink/AbstractXmlSink.java
 Sat Aug  4 03:37:23 2007
@@ -0,0 +1,183 @@
+package org.apache.maven.doxia.sink;
+
+/*
+ * 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.
+ */
+
+import java.util.Enumeration;
+
+import javax.swing.text.AttributeSet;
+import javax.swing.text.MutableAttributeSet;
+import javax.swing.text.html.HTML.Tag;
+
+import org.apache.maven.doxia.markup.XmlMarkup;
+
+/**
+ * An abstract <code>Sink</code> for xml markup syntax.
+ *
+ * @author <a href="mailto:[EMAIL PROTECTED]">Vincent Siveton</a>
+ * @version $Id$
+ * @since 1.0
+ */
+public abstract class AbstractXmlSink
+    extends SinkAdapter
+    implements XmlMarkup
+{
+    /**
+     * Starts a Tag, for instance:
+     * <pre>
+     * &lt;tag&gt;
+     * </pre>
+     *
+     * @param t a non null tag
+     * @see #writeStartTag(Tag, MutableAttributeSet)
+     */
+    protected void writeStartTag ( Tag t )
+    {
+        writeStartTag ( t, null );
+    }
+
+    /**
+     * Starts a Tag with attributes, for instance:
+     * <pre>
+     * &lt;tag attName="attValue"&gt;
+     * </pre>
+     *
+     * @param t a non null tag
+     * @param att a set of attributes
+     * @see #writeStartTag(Tag, MutableAttributeSet, boolean)
+     */
+    protected void writeStartTag ( Tag t, MutableAttributeSet att )
+    {
+        writeStartTag ( t, att, false );
+    }
+
+    /**
+     * Starts a Tag with attributes, for instance:
+     * <pre>
+     * &lt;tag attName="attValue"&gt;
+     * </pre>
+     *
+     * @param t a non null tag
+     * @param att a set of attributes
+     * @param isSimpleTag boolean to write as a simple tag
+     */
+    protected void writeStartTag( Tag t, MutableAttributeSet att, boolean 
isSimpleTag )
+    {
+        if ( t == null )
+        {
+            throw new IllegalArgumentException( "A tag is required" );
+        }
+
+        StringBuffer sb = new StringBuffer();
+        sb.append( String.valueOf( LESS_THAN ) );
+        sb.append( t.toString() );
+
+        if ( att != null )
+        {
+            Enumeration names = att.getAttributeNames();
+
+            while ( names.hasMoreElements() )
+            {
+                Object key = names.nextElement();
+                Object value = att.getAttribute( key );
+
+                if ( value instanceof AttributeSet )
+                {
+                    // ignored
+                }
+                else
+                {
+                    sb.append( String.valueOf( SPACE ) ).append( 
key.toString() ).append( String.valueOf( EQUAL ) )
+                        .append( String.valueOf( QUOTE ) ).append( 
value.toString() ).append( String.valueOf( QUOTE ) );
+                }
+            }
+        }
+
+        if ( isSimpleTag )
+        {
+            sb.append( String.valueOf( SPACE ) ).append( String.valueOf( SLASH 
) );
+        }
+
+        sb.append( String.valueOf( GREATER_THAN ) );
+
+        if ( isSimpleTag )
+        {
+            sb.append( EOL );
+        }
+
+        write( sb.toString() );
+    }
+
+    /**
+     * Ends a Tag, for instance:
+     * <pre>
+     * &lt;/tag&gt;
+     * </pre>
+     *
+     * @param t a tag
+     */
+    protected void writeEndTag( Tag t )
+    {
+        StringBuffer sb = new StringBuffer();
+        sb.append( String.valueOf( LESS_THAN ) );
+        sb.append( String.valueOf( SLASH ) );
+        sb.append( t.toString() );
+        sb.append( String.valueOf( GREATER_THAN ) );
+
+        sb.append( EOL );
+
+        write( sb.toString() );
+    }
+
+    /**
+     * Starts a simple Tag, for instance:
+     * <pre>
+     * &lt;tag /&gt;
+     * </pre>
+     *
+     * @param t a non null tag
+     * @see #writeSimpleTag(Tag, MutableAttributeSet)
+     */
+    protected void writeSimpleTag( Tag t )
+    {
+        writeSimpleTag( t, null );
+    }
+
+    /**
+     * Starts a simple Tag with attributes, for instance:
+     * <pre>
+     * &lt;tag attName="attValue" /&gt;
+     * </pre>
+     *
+     * @param t a non null tag
+     * @param att a set of attributes
+     * @see #writeStartTag(Tag, MutableAttributeSet, boolean)
+     */
+    protected void writeSimpleTag ( Tag t, MutableAttributeSet att )
+    {
+        writeStartTag ( t, att, true );
+    }
+
+    /**
+     * TODO DOXIA-59 Need to uniform writing
+     *
+     * @param text the given text to write
+     */
+    protected abstract void write( String text );
+}

Propchange: 
maven/doxia/doxia/trunk/doxia-core/src/main/java/org/apache/maven/doxia/sink/AbstractXmlSink.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
maven/doxia/doxia/trunk/doxia-core/src/main/java/org/apache/maven/doxia/sink/AbstractXmlSink.java
------------------------------------------------------------------------------
    svn:keywords = "Author Date Id Revision"


Reply via email to