ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dona...@apache.org
Subject cvs commit: jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/components/builder/ati ATIProjectBuilder.java AtiModelBuilder.java ReactorPIHandler.java StopParsingException.java
Date Tue, 21 May 2002 00:28:12 GMT
donaldp     02/05/20 17:28:12

  Added:       container/src/java/org/apache/myrmidon/components/builder/ati
                        ATIProjectBuilder.java AtiModelBuilder.java
                        ReactorPIHandler.java StopParsingException.java
  Log:
  Start moving ati into subpackage.
  
  Revision  Changes    Path
  1.1                  jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/components/builder/ati/ATIProjectBuilder.java
  
  Index: ATIProjectBuilder.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 org.apache.myrmidon.components.builder.DefaultProjectBuilder;
  import org.apache.myrmidon.interfaces.builder.ModelBuilder;
  
  /**
   * Default implementation to construct project from a build file.
   *
   * @author <a href="mailto:peter@apache.org">Peter Donald</a>
   * @version $Revision: 1.1 $ $Date: 2002/05/21 00:28:12 $
   * @ant.type type="project-builder" name="ati"
   */
  public class ATIProjectBuilder
      extends DefaultProjectBuilder
  {
      protected ModelBuilder getModelBuilder()
      {
          return new AtiModelBuilder();
      }
  }
  
  
  
  1.1                  jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/components/builder/ati/AtiModelBuilder.java
  
  Index: AtiModelBuilder.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 javax.xml.transform.Transformer;
  import javax.xml.transform.TransformerFactory;
  import javax.xml.transform.sax.SAXResult;
  import javax.xml.transform.stream.StreamSource;
  import org.apache.avalon.excalibur.i18n.ResourceManager;
  import org.apache.avalon.excalibur.i18n.Resources;
  import org.apache.myrmidon.api.metadata.ModelElement;
  import org.apache.myrmidon.api.metadata.ModelException;
  import org.apache.myrmidon.interfaces.builder.ModelBuilder;
  import org.apache.myrmidon.interfaces.model.ModelElementSaxHandler;
  import org.xml.sax.SAXException;
  import org.xml.sax.XMLReader;
  
  /**
   * A simple ModelBuilder that will build a {@link ModelElement}
   * tree from an XML source. It will also run an XSL transformation
   * if the xsl-stylesheet Processing instruction is present.
   *
   * @author <a href="mailto:peter@apache.org">Peter Donald</a>
   * @version $Revision: 1.1 $ $Date: 2002/05/21 00:28:12 $
   *
   * @ant.type type="model-builder" name="ati"
   */
  public class AtiModelBuilder
      implements ModelBuilder
  {
      private final static Resources REZ =
          ResourceManager.getPackageResources( AtiModelBuilder.class );
  
      public ModelElement build( final String systemID )
          throws ModelException
      {
          try
          {
              final ModelElementSaxHandler contentHandler = new ModelElementSaxHandler();
              final SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
              final SAXParser saxParser = saxParserFactory.newSAXParser();
              final XMLReader reader = saxParser.getXMLReader();
              reader.setFeature( "http://xml.org/sax/features/validation", false );
  
              final String stylesheet =
                  determineStylesheet( reader, systemID );
  
              Transformer transformer = null;
              if( null != stylesheet )
              {
                  final TransformerFactory factory = TransformerFactory.newInstance();
                  final String stylesheetURI = getStylesheet( stylesheet, systemID );
                  transformer = factory.newTransformer( new StreamSource( stylesheetURI )
);
              }
  
              if( null == transformer )
              {
                  reader.setContentHandler( contentHandler );
                  reader.parse( systemID );
              }
              else
              {
                  final SAXResult result = new SAXResult( contentHandler );
                  transformer.transform( new StreamSource( systemID ), result );
              }
              return contentHandler.getModel();
          }
          catch( final Exception e )
          {
              final String message = e.getMessage();
              throw new ModelException( message, e );
          }
      }
  
      /**
       * 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 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 );
              }
          }
          return stylesheet;
      }
  
      /**
       * 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 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 org.xml.sax.SAXException
       */
      private 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;
      }
  }
  
  
  
  1.1                  jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/components/builder/ati/ReactorPIHandler.java
  
  Index: ReactorPIHandler.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.util.ArrayList;
  import org.xml.sax.Attributes;
  import org.xml.sax.SAXException;
  import org.xml.sax.helpers.DefaultHandler;
  
  /**
   * Handler that reacts to PIs before first element.
   * Have to do it this way as there doesn't seem to be a *safe* way
   * of redirecting content handlers at runtime while using transformers.
   *
   * @author <a href="mailto:peter@apache.org">Peter Donald</a>
   * @version $Revision: 1.1 $ $Date: 2002/05/21 00:28:12 $
   */
  public class ReactorPIHandler
      extends DefaultHandler
  {
      private ArrayList m_targets = new ArrayList();
      private ArrayList m_data = new ArrayList();
  
      public int getPICount()
      {
          return m_targets.size();
      }
  
      public String getTarget( final int index )
      {
          return (String)m_targets.get( index );
      }
  
      public String getData( final int index )
      {
          return (String)m_data.get( index );
      }
  
      public void processingInstruction( final String target, final String data )
          throws SAXException
      {
          m_targets.add( target );
          m_data.add( data );
      }
  
      public void startElement( final String uri,
                                final String localName,
                                final String qName,
                                final Attributes atts )
          throws SAXException
      {
          //Workaround to stop SAX pipeline
          throw new StopParsingException();
      }
  }
  
  
  
  1.1                  jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/components/builder/ati/StopParsingException.java
  
  Index: StopParsingException.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 org.xml.sax.SAXException;
  
  /**
   * Dummy exception to stop parsing "safely".
   *
   * @author <a href="mailto:peter@apache.org">Peter Donald</a>
   * @version $Revision: 1.1 $ $Date: 2002/05/21 00:28:12 $
   */
  public class StopParsingException
      extends SAXException
  {
      public StopParsingException()
      {
          super( "" );
      }
  }
  
  
  

--
To unsubscribe, e-mail:   <mailto:ant-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:ant-dev-help@jakarta.apache.org>


Mime
View raw message