xmlbeans-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From eric...@apache.org
Subject cvs commit: xml-xmlbeans/v2/test/src/erictest EricTest.java
Date Thu, 04 Mar 2004 23:56:21 GMT
ericvas     2004/03/04 15:56:21

  Modified:    v2/src/newstore2/org/apache/xmlbeans/impl/newstore2 Cur.java
                        Cursor.java Public2.java
               v2/test/src/erictest EricTest.java
  Log:
  More XmlCursor implemented
  
  Revision  Changes    Path
  1.15      +57 -0     xml-xmlbeans/v2/src/newstore2/org/apache/xmlbeans/impl/newstore2/Cur.java
  
  Index: Cur.java
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/src/newstore2/org/apache/xmlbeans/impl/newstore2/Cur.java,v
  retrieving revision 1.14
  retrieving revision 1.15
  diff -u -r1.14 -r1.15
  --- Cur.java	3 Mar 2004 22:56:38 -0000	1.14
  +++ Cur.java	4 Mar 2004 23:56:21 -0000	1.15
  @@ -508,6 +508,19 @@
           return true;
       }
       
  +    boolean toLastAttr ( )
  +    {
  +        assert _xobj != null && isNormal() && _pos == 0;
  +
  +        if (!toFirstAttr())
  +            return false;
  +
  +        while ( toNextAttr() )
  +            ;
  +
  +        return true;
  +    }
  +    
       boolean toNextAttr ( )
       {
           assert _xobj != null && isNormal() && _pos == 0 && isAttr();
  @@ -574,6 +587,49 @@
           assert false;
       }
       
  +    boolean prev ( )
  +    {
  +        assert isNormal();
  +
  +        if (_xobj.isRoot() && _pos == 0)
  +            return false;
  +        
  +        Xobj x = getDenormal();
  +        int  p = _posTemp;
  +
  +        assert p != 0;
  +
  +        int pa = x.posAfter();
  +
  +        if (p > pa)
  +            p = pa;
  +        else if (p == pa)
  +        {
  +            if (x.isAttr() && x._cchAfter > 0)
  +            {
  +                x = x.ensureParent();
  +                p = 0;
  +            }
  +            else
  +                p = pa - 1;
  +        }
  +        else if (p == pa - 1)
  +        {
  +            p = x._cchValue > 0 ? 1 : 0;
  +        }
  +        else if (p > 1)
  +            p = 1;
  +        else
  +        {
  +            assert p == 1;
  +            p = 0;
  +        }
  +        
  +        set( getNormal( x, p ), _posTemp );
  +
  +        return true;
  +    }
  +    
       boolean next ( )
       {
           assert isNormal();
  @@ -1196,6 +1252,7 @@
       private Xobj getDenormal ( )
       {
           assert _xobj != null && isNormal();
  +        assert !_xobj.isRoot() || _pos > 0;
           
           Xobj x = _xobj;
           int  p = _pos;
  
  
  
  1.5       +170 -52   xml-xmlbeans/v2/src/newstore2/org/apache/xmlbeans/impl/newstore2/Cursor.java
  
  Index: Cursor.java
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/src/newstore2/org/apache/xmlbeans/impl/newstore2/Cursor.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- Cursor.java	3 Mar 2004 23:46:50 -0000	1.4
  +++ Cursor.java	4 Mar 2004 23:56:21 -0000	1.5
  @@ -15,6 +15,8 @@
   
   package org.apache.xmlbeans.impl.newstore2;
   
  +import java.io.PrintStream;
  +
   import javax.xml.namespace.QName;
   
   import javax.xml.stream.XMLStreamReader;
  @@ -27,6 +29,8 @@
   import org.apache.xmlbeans.XmlOptions;
   import org.apache.xmlbeans.XmlDocumentProperties;
   
  +import org.apache.xmlbeans.impl.common.XMLChar;
  +
   import java.util.Map;
   import java.util.Collection;
   
  @@ -61,12 +65,32 @@
           _cur = c.weakCur( this );
       }
   
  -    private boolean isValid ( )
  +    private static boolean isValid ( Cur c )
       {
  -        // TODO - make sure we're not in attr/pi/comment, etc ...
  +        int k = c.kind();
  +        
  +        if (k == TEXT || k < 0)
  +        {
  +            c.push();
  +            
  +            if (c.toParentRaw())
  +            {
  +                int pk = c.kind();
  +
  +                if (pk == COMMENT || pk == PROCINST || pk == ATTR)
  +                    return false;
  +            }
  +                
  +            c.pop();
  +        }
           
           return true;
       }
  +    
  +    private boolean isValid ( )
  +    {
  +        return isValid( _cur );
  +    }
   
       Locale locale ( )
       {
  @@ -78,11 +102,94 @@
           return _cur.tempCur();
       }
   
  +    public void dump ( PrintStream o )
  +    {
  +        _cur.dump( o );
  +    }
  +    
       public void dump ( )
       {
  -        _cur.dump();
  +        dump( System.out );
  +    }
  +
  +    static void validateLocalName ( QName name )
  +    {
  +        if (name == null)
  +            throw new IllegalArgumentException( "QName is null" );
  +
  +        validateLocalName( name.getLocalPart() );
  +    }
  +
  +    static void validateLocalName ( String name )
  +    {
  +        if (name == null)
  +            throw new IllegalArgumentException( "Name is null" );
  +
  +        if (name.length() == 0)
  +            throw new IllegalArgumentException( "Name is empty" );
  +
  +        if (!XMLChar.isValidNCName( name ))
  +            throw new IllegalArgumentException( "Name is not valid" );
  +    }
  +
  +    static void validatePrefix ( String name )
  +    {
  +        if (name == null)
  +            throw new IllegalArgumentException( "Prefix is null" );
  +
  +        if (name.length() == 0)
  +            throw new IllegalArgumentException( "Prefix is empty" );
  +
  +        if (Locale.beginsWithXml( name ))
  +            throw new IllegalArgumentException( "Prefix begins with 'xml'" );
  +
  +        if (!XMLChar.isValidNCName( name ))
  +            throw new IllegalArgumentException( "Prefix is not valid" );
  +    }
  +
  +    private static void complain ( String msg )
  +    {
  +        throw new IllegalArgumentException( msg );
       }
   
  +    static void insert ( Cur thisStuff, Cur there )
  +    {
  +        assert isValid( thisStuff );
  +        assert isValid( there );
  +
  +        int thisKind = thisStuff.kind();
  +
  +        if (thisKind < 0)
  +            complain( "Can't move/copy/insert an end token." );
  +
  +        if (thisKind == ROOT)
  +            complain( "Can't move/copy/insert a whole document." );
  +
  +        int thereKind = there.kind();
  +
  +        if (thereKind == ROOT)
  +            complain( "Can't insert before the start of the document." );
  +
  +        if (thereKind == ATTR && thisKind != TEXT)
  +            complain( "Can only insert attributes before other attributes." );
  +
  +        if (thisKind == ATTR)
  +        {
  +            there.push();
  +            there.prev();
  +            int prevKind = there.kind();
  +            there.pop();
  +
  +            if (prevKind != -ELEM || prevKind != -ROOT || prevKind != -ATTR)
  +                complain( "Can only insert attributes before other attributes." );
  +        }
  +
  +        if (thisKind == TEXT)
  +            thisStuff.moveChars( there, -1 );
  +        else
  +            thisStuff.moveNode( there );
  +    }
  +    
       //
       //
       //
  @@ -148,13 +255,39 @@
           }
           
           default :
  -            _cur.next();
  +        {
  +            if (!_cur.next())
  +                return TokenType.NONE;
  +                        
               break;
           }
  +        }
   
           return _currentTokenType();
       }
   
  +    public TokenType _toPrevToken ( )
  +    {
  +        assert isValid();
  +
  +        if (!_cur.prev())
  +        {
  +            if (_cur.kind() == ATTR)
  +                _cur.toParent();
  +            else
  +                return TokenType.NONE;
  +        }
  +
  +        int k = _cur.kind();
  +
  +        if (k == -COMMENT || k == -PROCINST)
  +            _cur.toParent();
  +        else if (_cur.isContainer())
  +            _cur.toLastAttr();
  +        
  +        return _currentTokenType();
  +    }
  +    
       public XmlCursor _newCursor ( )
       {
           return new Cursor( _cur );
  @@ -491,11 +624,6 @@
           throw new RuntimeException( "Not implemented" );
       }
       
  -    public TokenType _toPrevToken ( )
  -    {
  -        throw new RuntimeException( "Not implemented" );
  -    }
  -    
       public TokenType _toFirstContentToken ( )
       {
           throw new RuntimeException( "Not implemented" );
  @@ -638,7 +766,8 @@
       
       public void _toStartDoc ( )
       {
  -        throw new RuntimeException( "Not implemented" );
  +        while ( _cur.toParent() )
  +            ;
       }
       
       public void _toEndDoc ( )
  @@ -755,54 +884,43 @@
       {
           throw new RuntimeException( "Not implemented" );
       }
  -    
  -    public void _insertElement ( QName name )
  -    {
  -        throw new RuntimeException( "Not implemented" );
  -    }
  -    
  -    public void _insertElement ( String localName )
  -    {
  -        throw new RuntimeException( "Not implemented" );
  -    }
  -    
  -    public void _insertElement ( String localName, String uri )
  -    {
  -        throw new RuntimeException( "Not implemented" );
  -    }
  -    
  -    public void _beginElement ( QName name )
  -    {
  -        assert isValid();
   
  -        _insertElement( name );
  -        _toPrevToken();
  -    }
  -    
  -    public void _beginElement ( String localName )
  -    {
  -        throw new RuntimeException( "Not implemented" );
  -    }
  +    //
  +    // Inserting elements
  +    //
       
  -    public void _beginElement ( String localName, String uri )
  -    {
  -        throw new RuntimeException( "Not implemented" );
  -    }
  +    public void _insertElement         ( String localName                          ) {
_insertElementWithText( localName, null, null ); }
  +    public void _insertElement         ( String localName, String uri              ) {
_insertElementWithText( localName, uri, null ); }
  +    public void _insertElement         ( QName  name                               ) {
_insertElementWithText( name, null ); }
  +    public void _beginElement          ( String localName                          ) {
_insertElementWithText( localName, null, null ); _toPrevToken(); }
  +    public void _beginElement          ( String localName, String uri              ) {
_insertElementWithText( localName, uri ); _toPrevToken(); }
  +    public void _beginElement          ( QName  name                               ) {
_insertElementWithText( name, null ); _toPrevToken(); }
  +    public void _insertElementWithText ( String localName, String text             ) {
_insertElementWithText( localName, null, text ); }
  +    public void _insertElementWithText ( String localName, String uri, String text ) {
_insertElementWithText( _locale.makeQName( uri, localName ), text ); }
       
       public void _insertElementWithText ( QName name, String text )
       {
  -        throw new RuntimeException( "Not implemented" );
  -    }
  -    
  -    public void _insertElementWithText ( String localName, String text )
  -    {
  -        throw new RuntimeException( "Not implemented" );
  -    }
  -    
  -    public void _insertElementWithText ( String localName, String uri, String text )
  -    {
  -        throw new RuntimeException( "Not implemented" );
  +        validateLocalName( name.getLocalPart() );
  +
  +        Cur c = _locale.tempCur();
  +
  +        c.createElement( name );
  +
  +        if (text != null && text.length() > 0)
  +        {
  +            c.next();
  +            c.insertChars( text, 0, text.length() );
  +            c.toParent();
  +        }
  +
  +        insert( c, _cur );
  +
  +        c.release();
       }
  +
  +    //
  +    //
  +    //
       
       public void _insertAttribute ( String localName )
       {
  
  
  
  1.7       +33 -6     xml-xmlbeans/v2/src/newstore2/org/apache/xmlbeans/impl/newstore2/Public2.java
  
  Index: Public2.java
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/src/newstore2/org/apache/xmlbeans/impl/newstore2/Public2.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- Public2.java	3 Mar 2004 22:56:38 -0000	1.6
  +++ Public2.java	4 Mar 2004 23:56:21 -0000	1.7
  @@ -205,18 +205,45 @@
           d.dump( o );
       }
   
  -    public static void dump ( Dom d )
  +    public static void dump ( PrintStream o, Node n )
       {
  -        dump( System.out, d );
  +        dump( o, (Dom) n );
       }
   
  -    public static void dump ( Node n )
  +    public static void dump ( PrintStream o, XmlCursor c )
       {
  -        dump( System.out, n );
  +        ((Cursor) c).dump( o );
       }
   
  -    public static void dump ( PrintStream o, Node n )
  +    public static void dump ( Dom  d )      { dump( System.out, d ); }
  +    public static void dump ( Node n )      { dump( System.out, n ); }
  +    public static void dump ( XmlCursor c ) { dump( System.out, c ); }
  +
  +    public static void test ( Node n )
       {
  -        dump( o, (Dom) n );
  +        Dom d = (Dom) n;
  +        
  +        Cur c = d.locale().permCur();
  +        c.moveToDom( d );
  +
  +        c.dump();
  +        
  +        for ( ; ; )
  +        {
  +            if (!c.next())
  +                break;
  +            
  +            c.dump();
  +        }
  +
  +        for ( ; ; )
  +        {
  +            if (!c.prev())
  +                break;
  +            
  +            c.dump();
  +        }
  +
  +        c.release();
       }
   }
  
  
  
  1.29      +11 -6     xml-xmlbeans/v2/test/src/erictest/EricTest.java
  
  Index: EricTest.java
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/test/src/erictest/EricTest.java,v
  retrieving revision 1.28
  retrieving revision 1.29
  diff -u -r1.28 -r1.29
  --- EricTest.java	3 Mar 2004 23:46:50 -0000	1.28
  +++ EricTest.java	4 Mar 2004 23:56:21 -0000	1.29
  @@ -106,17 +106,22 @@
   {
       public static void main ( String[] args ) throws Exception
       {
  -        Document doc = Public2.parse( "<a/>" );
  +        XmlCursor c = Public2.newStore();
   
  -        XmlCursor c = Public2.getCursor( doc );
  -        
  -//        XmlCursor c = Public2.newStore();
  +        c.toNextToken();
   
  -        System.out.println( c.xmlText() );
  +        c.beginElement( "foo" );
  +        c.beginElement( "bar" );
  +        c.beginElement( "baz" );
   
           c.toNextToken();
  +        c.toNextToken();
  +        
  +        c.beginElement( "bar2" );
   
  -        c.beginElement( "foo" );
  +        c.toStartDoc();
  +
  +        System.out.println( c.xmlText() );
   
   
   //        Document doc = Public2.parse( "<a/>" );
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: xmlbeans-cvs-unsubscribe@xml.apache.org
For additional commands, e-mail: xmlbeans-cvs-help@xml.apache.org


Mime
View raw message