You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ant.apache.org by ad...@apache.org on 2002/05/18 06:05:58 UTC

cvs commit: jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/interfaces/model ModelElementSaxHandler.java ModelElementUtil.java

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:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: cvs commit: jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/interfaces/model ModelElementSaxHandler.java ModelElementUtil.java

Posted by Peter Donald <pe...@apache.org>.
On Sat, 18 May 2002 14:45, Adam Murdoch wrote:
> Exposing too much?  It's just a couple of utility classes:  A SAX
> ContentHandler that assembles a ModelElement, and a couple of static
> methods.

yep. But thats basically the same as we have over in avalon land wrt 
DefaultConfigurationBuilder. We started out with a simple version of that but 
quite a few times we have had to change it and it has been ugly the way we 
did it (but we needed to do it that way to keep backwards compatability). 

> That's not to say there shouldn't be a ModelBuilder service, but even if
> there were it would not be appropriate for everything.  We'd still need
> some util stuff somewhere.  Maybe on ModelElement itself (the non-SAX
> stuff, at least).

The clone tree could probably go on the ModelElement. I will stick it on 
unless you get to it first ;)

-- 
Cheers,

Peter Donald


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: cvs commit: jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/interfaces/model ModelElementSaxHandler.java ModelElementUtil.java

Posted by Adam Murdoch <ad...@apache.org>.
On Sat, 18 May 2002 14:16, Peter Donald wrote:
> On Sat, 18 May 2002 14:05, adammurdoch@apache.org wrote:
> > 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.
>
> I owuld rather have a ModelBuilder service/interface that happened to use
> the ModelElement*.java and just expose the interface. I think this change
> is probably exposing waaay too much outside of container :)

Exposing too much?  It's just a couple of utility classes:  A SAX 
ContentHandler that assembles a ModelElement, and a couple of static methods.

That's not to say there shouldn't be a ModelBuilder service, but even if there 
were it would not be appropriate for everything.  We'd still need some util 
stuff somewhere.  Maybe on ModelElement itself (the non-SAX stuff, at least).  

Whatever.  Moving this stuff was just a quick fix.  I don't particularly care 
where it ends up.

-- 
Adam

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: cvs commit: jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/interfaces/model ModelElementSaxHandler.java ModelElementUtil.java

Posted by Peter Donald <pe...@apache.org>.
On Sat, 18 May 2002 14:05, adammurdoch@apache.org wrote:
> 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.

I owuld rather have a ModelBuilder service/interface that happened to use the 
ModelElement*.java and just expose the interface. I think this change is 
probably exposing waaay too much outside of container :)

-- 
Cheers,

Peter Donald


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>