donaldp 2002/06/08 20:55:48
Modified: container/src/java/org/apache/myrmidon/components/builder
DefaultProjectBuilder.java
TransformingProjectBuilder.java
container/src/java/org/apache/myrmidon/components/builder/ati
ATIProjectBuilder.java
Added: container/src/java/org/apache/myrmidon/components/builder/ati
XMLBuilderUtil.java
Removed: container/src/java/org/apache/myrmidon/components/builder/ati
AtiModelBuilder.java
Log:
Refactor ati builder to reuse Default/Transforming ProjectBuidlers.
Revision Changes Path
1.70 +4 -3
jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/components/builder/DefaultProjectBuilder.java
Index: DefaultProjectBuilder.java
===================================================================
RCS file:
/home/cvs/jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/components/builder/DefaultProjectBuilder.java,v
retrieving revision 1.69
retrieving revision 1.70
diff -u -r1.69 -r1.70
--- DefaultProjectBuilder.java 9 Jun 2002 03:34:46 -0000 1.69
+++ DefaultProjectBuilder.java 9 Jun 2002 03:55:48 -0000 1.70
@@ -34,7 +34,7 @@
* Default implementation to construct project from a build file.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Peter Donald</a>
- * @version $Revision: 1.69 $ $Date: 2002/06/09 03:34:46 $
+ * @version $Revision: 1.70 $ $Date: 2002/06/09 03:55:48 $
*
* @ant.type type="project-builder" name="ant2"
*/
@@ -117,7 +117,8 @@
{
// Check for cached project
final String systemID = extractURL( file );
- final ModelElement model = getModelBuilder().build( systemID );
+ final ModelElement model =
+ getModelBuilder( systemID ).build( systemID );
// Tranform the resulting model
transformProject( model );
@@ -158,7 +159,7 @@
*
* @return the ModelBuilder
*/
- protected ModelBuilder getModelBuilder()
+ protected ModelBuilder getModelBuilder( final String systemID )
{
return m_modelBuilder;
}
1.8 +2 -2
jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/components/builder/TransformingProjectBuilder.java
Index: TransformingProjectBuilder.java
===================================================================
RCS file:
/home/cvs/jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/components/builder/TransformingProjectBuilder.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- TransformingProjectBuilder.java 21 May 2002 00:29:51 -0000 1.7
+++ TransformingProjectBuilder.java 9 Jun 2002 03:55:48 -0000 1.8
@@ -13,14 +13,14 @@
* A Project Builder which performs an XSL transformation on a project.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Darrell DeBoer</a>
- * @version $Revision: 1.7 $ $Date: 2002/05/21 00:29:51 $
+ * @version $Revision: 1.8 $ $Date: 2002/06/09 03:55:48 $
*
* @ant.type type="project-builder" name="ant-transform"
*/
public class TransformingProjectBuilder
extends DefaultProjectBuilder
{
- protected ModelBuilder getModelBuilder()
+ protected ModelBuilder getModelBuilder( final String systemID )
{
return new TransformingModelBuilder();
}
1.2 +14 -3
jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/components/builder/ati/ATIProjectBuilder.java
Index: ATIProjectBuilder.java
===================================================================
RCS file:
/home/cvs/jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/components/builder/ati/ATIProjectBuilder.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- ATIProjectBuilder.java 21 May 2002 00:28:12 -0000 1.1
+++ ATIProjectBuilder.java 9 Jun 2002 03:55:48 -0000 1.2
@@ -7,21 +7,32 @@
*/
package org.apache.myrmidon.components.builder.ati;
+import org.apache.myrmidon.components.builder.DefaultModelBuilder;
import org.apache.myrmidon.components.builder.DefaultProjectBuilder;
+import org.apache.myrmidon.components.builder.TransformingModelBuilder;
import org.apache.myrmidon.interfaces.builder.ModelBuilder;
/**
* Default implementation to construct project from a build file.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Peter Donald</a>
- * @version $Revision: 1.1 $ $Date: 2002/05/21 00:28:12 $
+ * @version $Revision: 1.2 $ $Date: 2002/06/09 03:55:48 $
* @ant.type type="project-builder" name="ati"
*/
public class ATIProjectBuilder
extends DefaultProjectBuilder
{
- protected ModelBuilder getModelBuilder()
+ protected ModelBuilder getModelBuilder( final String systemID )
{
- return new AtiModelBuilder();
+ final String stylesheet =
+ XMLBuilderUtil.determineStylesheet( systemID );
+ if( null != stylesheet )
+ {
+ return new TransformingModelBuilder( stylesheet );
+ }
+ else
+ {
+ return new DefaultModelBuilder();
+ }
}
}
1.1
jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/components/builder/ati/XMLBuilderUtil.java
Index: XMLBuilderUtil.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE.txt file.
*/
package org.apache.myrmidon.components.builder.ati;
import java.io.IOException;
import java.net.URL;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.apache.avalon.excalibur.i18n.ResourceManager;
import org.apache.avalon.excalibur.i18n.Resources;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
/**
* Utility class to guess the stylesheet to use based on stylesheet
* PIs.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Peter Donald</a>
* @version $Revision: 1.1 $ $Date: 2002/06/09 03:55:48 $
*/
class XMLBuilderUtil
{
private final static Resources REZ =
ResourceManager.getPackageResources( XMLBuilderUtil.class );
/**
* Private constructor to block instantiation.
*/
private XMLBuilderUtil()
{
}
/**
* Read the start of the systemID and determine if there
* is any xsl-stylesheet Processing Instructions in header.
* If there is return the URI of styelsheet.
*
* @param systemID the file to read
* @return the xsl strylesheet if Processing instruction in header
*/
public static String determineStylesheet( final String systemID )
{
try
{
final SAXParserFactory saxParserFactory =
SAXParserFactory.newInstance();
final SAXParser saxParser = saxParserFactory.newSAXParser();
final XMLReader reader = saxParser.getXMLReader();
reader.setFeature( "http://xml.org/sax/features/validation",
false );
return determineStylesheet( reader, systemID );
}
catch( Exception e )
{
return null;
}
}
/**
* Read the start of the systemID and determine if there
* is any xsl-stylesheet Processing Instructions in header.
*
* @param reader the reader to use
* @param systemID the file to read
* @return the xsl strylesheet if Processing instruction in header
* @throws IOException on failure to read file
* @throws SAXException if file malformed
*/
private static String determineStylesheet( final XMLReader reader,
final String systemID )
throws IOException, SAXException
{
final ReactorPIHandler reactorHandler = new ReactorPIHandler();
reader.setContentHandler( reactorHandler );
try
{
reader.parse( systemID );
}
catch( final StopParsingException spe )
{
//Ignore me
}
String stylesheet = null;
final int size = reactorHandler.getPICount();
for( int i = 0; i < size; i++ )
{
final String target = reactorHandler.getTarget( i );
if( target.equals( "xsl-stylesheet" ) )
{
if( null != stylesheet )
{
final String message =
REZ.getString( "ati.two.stylesheet.pis" );
throw new SAXException( message );
}
stylesheet = reactorHandler.getData( i );
}
}
if( null == stylesheet )
{
return null;
}
else
{
return getStylesheet( stylesheet, systemID );
}
}
/**
* Retrieve the absolute URI for stylesheet
* based on specified URI and location of original
* xml file.
*
* @param data the specified URI
* @param baseSource the base location of xml
* @return the absolute URI
* @throws SAXException if malformed URI is supplied
*/
private static String getStylesheet( final String data,
final String baseSource )
throws SAXException
{
final String[] stylesheet = parseAttribute( data );
if( !stylesheet[ 0 ].equals( "href" ) )
{
final String message = REZ.getString( "ati.style.error" );
throw new SAXException( message );
}
try
{
return new URL( new URL( baseSource ), stylesheet[ 1 ]
).toString();
}
catch( final Exception e )
{
final String message =
REZ.getString( "ati.loading-style.error",
stylesheet[ 1 ],
e );
throw new SAXException( message );
}
}
/**
* Parse attributes out of a PI.
*
* @param data the data to parse
* @return an array of string with first element
* holding key and second holding value
* @throws SAXException
*/
private static String[] parseAttribute( final String data )
throws SAXException
{
//name="value"
int index = data.indexOf( '=' );
if( -1 == index )
{
final String message = REZ.getString(
"ati.attribue-expected.error", data );
throw new SAXException( message );
}
final int size = data.length();
if( '\"' != data.charAt( index + 1 ) ||
'\"' != data.charAt( size - 1 ) ||
size - 1 == index )
{
final String message =
REZ.getString( "ati.attribue-unquoted.error", data.substring(
0, index ) );
throw new SAXException( message );
}
final String[] result = new String[ 2 ];
result[ 0 ] = data.substring( 0, index );
result[ 1 ] = data.substring( index + 2, size - 1 );
return result;
}
}
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>