ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From adammurd...@apache.org
Subject cvs commit: jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/interfaces/model ModelElementSaxHandler.java ModelElementUtil.java
Date Sat, 18 May 2002 04:05:58 GMT
adammurdoch    02/05/17 21:05:58

  Modified:    container/src/java/org/apache/myrmidon/components/builder
                        DefaultProjectBuilder.java
  Added:       container/src/java/org/apache/myrmidon/interfaces/model
                        ModelElementSaxHandler.java ModelElementUtil.java
  Removed:     container/src/java/org/apache/myrmidon/components/builder
                        ModelElementSaxHandler.java ModelElementUtil.java
  Log:
  Move the ModelElement util stuff to interfaces, so that it's visible outside the container
classloader.
  
  Revision  Changes    Path
  1.51      +2 -1      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.50
  retrieving revision 1.51
  diff -u -r1.50 -r1.51
  --- DefaultProjectBuilder.java	17 May 2002 07:41:27 -0000	1.50
  +++ DefaultProjectBuilder.java	18 May 2002 04:05:57 -0000	1.51
  @@ -26,6 +26,7 @@
   import org.apache.myrmidon.interfaces.oldmodel.Dependency;
   import org.apache.myrmidon.interfaces.oldmodel.Project;
   import org.apache.myrmidon.interfaces.oldmodel.Target;
  +import org.apache.myrmidon.interfaces.model.ModelElementSaxHandler;
   import org.xml.sax.ContentHandler;
   import org.xml.sax.XMLReader;
   
  @@ -33,7 +34,7 @@
    * Default implementation to construct project from a build file.
    *
    * @author <a href="mailto:peter@apache.org">Peter Donald</a>
  - * @version $Revision: 1.50 $ $Date: 2002/05/17 07:41:27 $
  + * @version $Revision: 1.51 $ $Date: 2002/05/18 04:05:57 $
    *
    * @ant.type type="project-builder" name="ant2"
    */
  
  
  
  1.1                  jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/interfaces/model/ModelElementSaxHandler.java
  
  Index: ModelElementSaxHandler.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.interfaces.model;
  
  import org.xml.sax.helpers.DefaultHandler;
  import org.xml.sax.Attributes;
  import org.xml.sax.SAXException;
  import org.xml.sax.Locator;
  import org.apache.myrmidon.api.metadata.ModelElement;
  import org.apache.avalon.excalibur.i18n.ResourceManager;
  import org.apache.avalon.excalibur.i18n.Resources;
  import java.util.ArrayList;
  
  /**
   * A SAX content handler that assembles a ModelElement.
   *
   * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a>
   * @version $Revision: 1.1 $ $Date: 2002/05/18 04:05:58 $
   */
  public class ModelElementSaxHandler
      extends DefaultHandler
  {
      private static final Resources REZ =
          ResourceManager.getPackageResources( ModelElementSaxHandler.class );
  
      private ModelElement m_model;
      private ModelElement m_element;
      private Locator m_locator;
      private final ArrayList m_stack = new ArrayList();
      private final StringBuffer m_content = new StringBuffer();
  
      /**
       * Returns the model built by this handler.
       */
      public ModelElement getModel()
      {
          return m_model;
      }
  
      public void setDocumentLocator( final Locator locator )
      {
          m_locator = locator;
      }
  
      public void startElement( final String namespaceUri,
                                final String localName,
                                final String qName,
                                final Attributes attributes )
          throws SAXException
      {
          // Check for mixed model
          if( hasContent() )
          {
              final String message = REZ.getString( "saxhandler.mixedmodel.error",
                                                    getLocation() );
              throw new SAXException( message );
          }
  
          // Create the new element
          final ModelElement element = new ModelElement( qName, getLocation() );
          if( m_element == null )
          {
              m_model = element;
          }
          else
          {
              m_stack.add( 0, m_element );
          }
          m_element = element;
  
          // Set attributes
          final int count = attributes.getLength();
          for( int i = 0; i < count; i++ )
          {
              final String name = attributes.getQName( i );
              final String value = attributes.getValue( i );
              m_element.setAttribute( name, value );
          }
      }
  
      public void endElement( final String namespaceUri,
                              final String localName,
                              final String qName )
          throws SAXException
      {
          // Set text content, if any
          if( hasContent() )
          {
              // TODO: should not trim content here; needs to be moved up
              m_element.setContent( m_content.toString().trim() );
          }
          m_content.setLength( 0 );
  
          // Pop parent element off stack
          if( m_stack.size() > 0 )
          {
              final ModelElement parent = (ModelElement)m_stack.remove( 0 );
              parent.addChild( m_element );
              m_element = parent;
           }
          else
          {
              m_element = null;
          }
      }
  
      public void characters( final char[] chars,
                              final int offset,
                              final int count )
          throws SAXException
      {
          m_content.append( chars, offset, count );
  
          // Check for mixed model.  Should really check before appending, but
          // what can you do ...
          if( m_element.getChildCount() > 0 && hasContent() )
          {
              final String message = REZ.getString( "saxhandler.mixedmodel.error",
                                                    getLocation() );
              throw new SAXException( message );
          }
      }
  
      /**
       * Returns true if the text content bufer contains any non-whitespace chars.
       * @return
       */
      private boolean hasContent()
      {
          final int count = m_content.length();
          for( int i = 0; i < count; i++ )
          {
              if( !Character.isWhitespace( m_content.charAt( i ) ) )
              {
                  return true;
              }
          }
          return false;
      }
  
      /**
       * Builds a descriptive string for the current location.
       */
      private String getLocation()
      {
          return m_locator.getSystemId() + ':' + m_locator.getLineNumber();
      }
  
  }
  
  
  
  1.1                  jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/interfaces/model/ModelElementUtil.java
  
  Index: ModelElementUtil.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.interfaces.model;
  
  import javax.xml.parsers.SAXParser;
  import javax.xml.parsers.SAXParserFactory;
  import org.apache.myrmidon.api.metadata.ModelElement;
  import org.xml.sax.XMLReader;
  
  /**
   * Utility methods for dealing with {@link org.apache.myrmidon.api.metadata.ModelElement}
objects.
   *
   * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a>
   * @version $Revision: 1.1 $ $Date: 2002/05/18 04:05:58 $
   */
  public class ModelElementUtil
  {
      /**
       * Loads a model from an XML document.
       *
       * @param systemId The URL to load the model from
       * @return The loaded model.
       */
      public static ModelElement loadModel( final String systemId )
          throws Exception
      {
          final ModelElementSaxHandler contentHandler = new ModelElementSaxHandler();
          final SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
          final SAXParser saxParser = saxParserFactory.newSAXParser();
          final XMLReader parser = saxParser.getXMLReader();
          parser.setFeature( "http://xml.org/sax/features/namespace-prefixes", false );
          parser.setFeature( "http://xml.org/sax/features/namespaces", false );
          //parser.setFeature( "http://xml.org/sax/features/validation", false );
  
          parser.setContentHandler( contentHandler );
          parser.parse( systemId );
  
          return contentHandler.getModel();
      }
  
      /**
       * Makes a shallow copy of a model.
       */
      public static ModelElement copyModel( final ModelElement model )
      {
          final ModelElement newElement =
              new ModelElement( model.getName(), model.getLocation() );
  
          final String[] attributes = model.getAttributeNames();
          for( int i = 0; i < attributes.length; i++ )
          {
              final String name = attributes[ i ];
              final String value = model.getAttribute( name );
              newElement.setAttribute( name, value );
          }
  
          final ModelElement[] children = model.getChildren();
          for( int i = 0; i < children.length; i++ )
          {
              newElement.addChild( children[ i ] );
          }
  
          newElement.setContent( model.getContent() );
  
          return newElement;
      }
  }
  
  
  

--
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