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 Fri, 12 Mar 2004 21:18:18 GMT
ericvas     2004/03/12 13:18:18

  Modified:    v2/src/newstore2/org/apache/xmlbeans/impl/newstore2 Cur.java
                        Cursor.java Saver.java
               v2/test/src/erictest EricTest.java
  Log:
  More saver and cursor
  
  Revision  Changes    Path
  1.17      +19 -1     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.16
  retrieving revision 1.17
  diff -u -r1.16 -r1.17
  --- Cur.java	11 Mar 2004 19:50:29 -0000	1.16
  +++ Cur.java	12 Mar 2004 21:18:18 -0000	1.17
  @@ -276,7 +276,9 @@
   
       void setName ( QName name )
       {
  -        assert isNormal() && _xobj != null && _pos == 0 && (_xobj.isElem()
|| _xobj.isAttr());
  +        assert isNormal() && _xobj != null && _pos == 0;
  +        assert _xobj.isElem() || _xobj.isAttr() || _xobj.isProcinst();
  +        
           assert name != null;
           _xobj._name = name;
           
  @@ -395,6 +397,21 @@
           return toParent( true );
       }
       
  +    boolean hasParent ( )
  +    {
  +        assert isNormal() && _xobj != null;
  +
  +        if (_pos >= 1 && _pos <= _xobj.posEnd())
  +            return true;
  +
  +        assert _pos == 0 || _xobj._parent != null;
  +
  +        if (_xobj._parent != null)
  +            return true;
  +        
  +        return false;
  +    }
  +    
       boolean toParent ( boolean raw )
       {
           assert isNormal() && _xobj != null;
  @@ -1609,6 +1626,7 @@
           final boolean isRoot      ( ) { return kind() == ROOT; }
           final boolean isAttr      ( ) { return kind() == ATTR; }
           final boolean isElem      ( ) { return kind() == ELEM; }
  +        final boolean isProcinst  ( ) { return kind() == PROCINST; }
           final boolean isContainer ( ) { return kindIsContainer( kind() ); }
           
           boolean isNormalAttr ( ) { return isAttr() && !isXmlns(); }
  
  
  
  1.8       +114 -20   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.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- Cursor.java	11 Mar 2004 19:50:29 -0000	1.7
  +++ Cursor.java	12 Mar 2004 21:18:18 -0000	1.8
  @@ -204,20 +204,62 @@
       // change from a phantom ref to a soft/weak ref so I can know what
       // to do when I dequeue from the old q.
       
  +    public QName _getName ( )
  +    {
  +        // TODO - consider taking this out of the gateway
  +        return _cur.getName();
  +    }
  +    
  +    public void _setName ( QName name )
  +    {
  +        if (name == null)
  +            throw new IllegalArgumentException( "Name is null" );
  +
  +        switch ( _cur.kind() )
  +        {
  +        case ELEM :
  +        case ATTR :
  +        {
  +            validateLocalName( name.getLocalPart() );
  +            break;
  +        }
  +                    
  +        case PROCINST :
  +        {
  +            validatePrefix( name.getLocalPart() );
  +
  +            if (name.getNamespaceURI().length() > 0)
  +                throw new IllegalArgumentException( "Procinst name must have no URI" );
  +            
  +            if (name.getPrefix().length() > 0)
  +                throw new IllegalArgumentException( "Procinst name must have no prefix"
);
  +
  +            break;
  +        }
  +
  +        default :
  +            throw
  +                new IllegalStateException(
  +                    "Can set name on element, atrtribute and procinst only" );
  +        }
  +
  +        _cur.setName( name );
  +    }
  +    
       public TokenType _currentTokenType ( )
       {
           assert isValid();
           
           switch ( _cur.kind() )
           {
  -        case   ROOT   : return TokenType.STARTDOC;
  -        case - ROOT   : return TokenType.ENDDOC;
  -        case   ELEM   : return TokenType.START;
  -        case - ELEM   : return TokenType.END;
  -        case   TEXT   : return TokenType.TEXT;
  -        case ATTR     : return _cur.isXmlns() ? TokenType.NAMESPACE : TokenType.ATTR;
  -        case COMMENT  : return TokenType.COMMENT;
  -        case PROCINST : return TokenType.PROCINST;
  +        case   ROOT     : return TokenType.STARTDOC;
  +        case - ROOT     : return TokenType.ENDDOC;
  +        case   ELEM     : return TokenType.START;
  +        case - ELEM     : return TokenType.END;
  +        case   TEXT     : return TokenType.TEXT;
  +        case   ATTR     : return _cur.isXmlns() ? TokenType.NAMESPACE : TokenType.ATTR;
  +        case   COMMENT  : return TokenType.COMMENT;
  +        case   PROCINST : return TokenType.PROCINST;
   
           default :
               throw new IllegalStateException();
  @@ -536,16 +578,6 @@
           throw new RuntimeException( "Not implemented" );
       }
       
  -    public QName _getName ( )
  -    {
  -        throw new RuntimeException( "Not implemented" );
  -    }
  -    
  -    public void _setName ( QName name )
  -    {
  -        throw new RuntimeException( "Not implemented" );
  -    }
  -    
       public String _namespaceForPrefix ( String prefix )
       {
           throw new RuntimeException( "Not implemented" );
  @@ -603,12 +635,74 @@
       
       public boolean _toNextSibling ( )
       {
  -        throw new RuntimeException( "Not implemented" );
  +        assert isValid();
  +
  +        if (!_cur.hasParent())
  +            return false;
  +
  +        Cur c = tempCur();
  +        
  +        int k = c.kind();
  +
  +        if (k == ATTR)
  +            c.toParent();
  +        else if (k == ELEM)
  +        {
  +            c.toEnd();
  +            c.next();
  +        }
  +
  +        while ( (k = c.kind()) > 0 && k != ELEM )
  +            c.next();
  +
  +        if (k == ELEM)
  +            _cur.moveToCur( c );
  +
  +        c.release();
  +
  +        return k == ELEM;
       }
       
       public boolean _toPrevSibling ( )
       {
  -        throw new RuntimeException( "Not implemented" );
  +        assert isValid();
  +
  +        if (!_cur.hasParent())
  +            return false;
  +
  +        Cur c = tempCur();
  +
  +        boolean moved = false;
  +        
  +        int k = c.kind();
  +
  +        if (k != ATTR)
  +        {
  +            for ( ; ; )
  +            {
  +                if (!c.prev())
  +                    break;
  +
  +                k = c.kind();
  +
  +                if (k == ROOT || k == ELEM)
  +                    break;
  +
  +                if (c.kind() == -ELEM)
  +                {
  +                    c.toParent();
  +
  +                    _cur.moveToCur( c );
  +                    moved = true;
  +
  +                    break;
  +                }
  +            }
  +        }
  +
  +        c.release();
  +
  +        return moved;
       }
       
       public boolean _toLastChild ( )
  
  
  
  1.17      +70 -103   xml-xmlbeans/v2/src/newstore2/org/apache/xmlbeans/impl/newstore2/Saver.java
  
  Index: Saver.java
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/src/newstore2/org/apache/xmlbeans/impl/newstore2/Saver.java,v
  retrieving revision 1.16
  retrieving revision 1.17
  diff -u -r1.16 -r1.17
  --- Saver.java	12 Mar 2004 02:17:18 -0000	1.16
  +++ Saver.java	12 Mar 2004 21:18:18 -0000	1.17
  @@ -44,25 +44,23 @@
       static final int PROCINST = Cur.PROCINST;
       static final int TEXT     = Cur.TEXT;
   
  -    protected abstract boolean emitContainer (
  -        SaveCur c, QName name, ArrayList attrNames, ArrayList attrValues );
  -    
  -    protected abstract void emitFinish    ( SaveCur c, QName name );
  -    protected abstract void emitText      ( SaveCur c );
  -    protected abstract void emitComment   ( SaveCur c );
  -    protected abstract void emitProcinst  ( SaveCur c );
  +    protected abstract boolean emitElement ( SaveCur c, ArrayList attrNames, ArrayList
attrValues );
  +    protected abstract void emitFinish     ( SaveCur c );
  +    protected abstract void emitText       ( SaveCur c );
  +    protected abstract void emitComment    ( SaveCur c );
  +    protected abstract void emitProcinst   ( SaveCur c );
   
       protected void syntheticNamespace ( String prefix, String uri, boolean considerDefault
) { }
   
       Saver ( Cur c, XmlOptions options )
       {
           options = XmlOptions.maskNull( options );
  +        
  +        _cur = createSaveCur( c, options );
   
           _locale = c._locale;
           _version = _locale.version();
   
  -        _cur = createSaveCur( c, options  );
  -
           _namespaceStack = new ArrayList();
           _uriMap = new HashMap();
           _prefixMap = new HashMap();
  @@ -70,8 +68,8 @@
           _attrNames = new ArrayList();
           _attrValues = new ArrayList ();
   
  -        // Stops the synthesis of this namspace and make for better
  -        // roundtripping 
  +        // Define implicit xml prefixed namespace
  +        
           addMapping( "xml", Locale._xml1998Uri );
   
           if (options.hasOption( XmlOptions.SAVE_IMPLICIT_NAMESPACES ))
  @@ -114,7 +112,7 @@
               _suggestedPrefixes = (Map) options.get( XmlOptions.SAVE_SUGGESTED_PREFIXES);
       }
   
  -    private SaveCur createSaveCur ( Cur c, XmlOptions options )
  +    private static SaveCur createSaveCur ( Cur c, XmlOptions options )
       {
           QName synthName = (QName) options.get( XmlOptions.SAVE_SYNTHETIC_DOCUMENT_ELEMENT
);
   
  @@ -244,7 +242,7 @@
           return cur;
       }
   
  -    private void positionToInner ( Cur c, Cur start, Cur end )
  +    private static void positionToInner ( Cur c, Cur start, Cur end )
       {
           assert c.isContainer();
   
  @@ -371,12 +369,16 @@
   
           switch ( _cur.kind() )
           {
  -            case   ROOT : case   ELEM : { _skipContainer = processContainer(); break; }
  -            case - ROOT : case - ELEM : { processFinish(); _postPop = true;    break; }
  -            case TEXT                 : { emitText( _cur );                    break; }
  -            case COMMENT              : { emitComment( _cur );                 break; }
  -            case PROCINST             : { emitProcinst( _cur );                break; }
  +            case   ELEM     : { _skipContainer = processElement();   break; }
  +            case - ELEM     : { emitFinish( _cur ); _postPop = true; break; }
  +            case   TEXT     : { emitText( _cur );                    break; }
  +            case   COMMENT  : { emitComment( _cur );                 break; }
  +            case   PROCINST : { emitProcinst( _cur );                break; }
   
  +            case   ROOT :
  +            case - ROOT :
  +                break;
  +                
               default : throw new RuntimeException( "Unexpected kind" );
           }
   
  @@ -385,15 +387,12 @@
           return true;
       }
   
  -    private final boolean processContainer ( )
  +    private final boolean processElement ( )
       {
  -        assert _cur.isContainer();
  -        assert !_cur.isRoot() || _cur.getName() == null;
  +        assert _cur.isElem() && _cur.getName() != null;
   
           QName name = _cur.getName();
   
  -        String nameUri = name == null ? null : name.getNamespaceURI();
  -        
           // TODO - check for doctype to save out here
   
           ;
  @@ -402,7 +401,7 @@
           // which has no namespace, then we must make sure that pushing
           // the mappings causes the default namespace to be empty
   
  -        boolean ensureDefaultEmpty = name != null && nameUri.length() == 0;
  +        boolean ensureDefaultEmpty = name.getNamespaceURI().length() == 0;
   
           pushMappings( _cur, ensureDefaultEmpty );
   
  @@ -417,8 +416,7 @@
   
           // 1) The element name (not for starts)
   
  -        if (name != null)
  -            ensureMapping( nameUri, null, !ensureDefaultEmpty, false );
  +        ensureMapping( name.getNamespaceURI(), name.getPrefix(), !ensureDefaultEmpty, false
);
   
           //
           //
  @@ -449,7 +447,7 @@
   
                   _attrValues.add( _cur.getAttrValue() );
                   
  -                ensureMapping( attrName.getNamespaceURI(), null, false, true );
  +                ensureMapping( attrName.getNamespaceURI(), attrName.getPrefix(), false,
true );
               }
           }
           
  @@ -460,33 +458,24 @@
           // we've computed.  Basically, I'm making sure the pre-computed
           // namespaces are mapped on the first container which has a name.
   
  -        if (_preComputedNamespaces != null && name != null)
  +        if (_preComputedNamespaces != null)
           {
               for ( Iterator i = _preComputedNamespaces.keySet().iterator() ; i.hasNext()
; )
               {
                   String uri = (String) i.next();
  +                String prefix = (String) _preComputedNamespaces.get( uri );
  +                boolean considerDefault = prefix.length() == 0 && !ensureDefaultEmpty;
                   
  -                ensureMapping(
  -                    uri, null,
  -                    _preComputedNamespaces.get( uri ) != null && !ensureDefaultEmpty,
false );
  +                ensureMapping( uri, prefix, considerDefault, false );
               }
   
               // Set to null so we do this once at the top
               _preComputedNamespaces = null;
           }
   
  -        return emitContainer( _cur, name, _attrNames, _attrValues );
  +        return emitElement( _cur, _attrNames, _attrValues );
       }
   
  -    private final void processFinish ( )
  -    {
  -        QName name = _cur.getName();
  -        
  -        emitFinish( _cur, name );
  -        
  -        _postPop = true;
  -    }
  -    
       //
       // Layout of namespace stack:
       //
  @@ -728,7 +717,6 @@
           boolean considerCreatingDefault, boolean mustHavePrefix )
       {
           assert uri != null;
  -        assert candidatePrefix == null || candidatePrefix.length() > 0;
   
           // Can be called for no-namespaced things
   
  @@ -749,6 +737,9 @@
           //  4) ns#++
           //
           
  +        if (candidatePrefix != null && candidatePrefix.length() == 0)
  +            candidatePrefix = null;
  +        
           if (candidatePrefix == null || !tryPrefix( candidatePrefix ))
           {
               if (_suggestedPrefixes != null &&
  @@ -832,13 +823,13 @@
           protected void syntheticNamespace (
               String prefix, String uri, boolean considerCreatingDefault )
           {
  -            _synthNamespaces.put( uri, considerCreatingDefault ? "useDefault" : null );
  +            _synthNamespaces.put( uri, considerCreatingDefault ? "" : prefix );
           }
           
  -        protected boolean emitContainer (
  -            SaveCur c, QName name, ArrayList attrNames, ArrayList attrValues ) { return
false; }
  +        protected boolean emitElement (
  +            SaveCur c, ArrayList attrNames, ArrayList attrValues ) { return false; }
           
  -        protected void emitFinish    ( SaveCur c, QName name ) { }
  +        protected void emitFinish    ( SaveCur c ) { }
           protected void emitText      ( SaveCur c ) { }
           protected void emitComment   ( SaveCur c ) { }
           protected void emitProcinst  ( SaveCur c ) { }
  @@ -869,51 +860,42 @@
               }
           }
           
  -        protected boolean emitContainer (
  -            SaveCur c, QName name, ArrayList attrNames, ArrayList attrValues )
  +        protected boolean emitElement (
  +            SaveCur c, ArrayList attrNames, ArrayList attrValues )
           {
  -            boolean skipElem = false;
  -            
  -            if (c.isElem())
  -            {
  -                emitContainerHelper( name, false, attrNames, attrValues );
  +            assert c.isElem();
   
  -                assert c.isContainer();
  +            emit( '<' );
  +            emitName( c.getName() );
   
  -                if (!c.hasChildren() && !c.hasText())
  -                {
  -                    emit( '/' );
  -                    skipElem = true;
  -                }
  -                
  -                emit( '>' );
  +            if (saveNamespacesFirst())
  +                emitNamespacesHelper();
  +
  +            for ( int i = 0 ; i < attrNames.size() ; i++ )
  +                emitAttrHelper( (QName) attrNames.get( i ), (String) attrValues.get( i
) );
  +
  +            if (!saveNamespacesFirst())
  +                emitNamespacesHelper();
  +
  +            if (!c.hasChildren() && !c.hasText())
  +            {
  +                emit( "/>" );
  +                return true;
               }
               else
               {
  -                assert c.isRoot();
  -
  -                if (name != null)
  -                    emitContainerHelper( name, true, attrNames, attrValues );
  +                emit( '>' );
  +                return false;
               }
  -
  -            return skipElem;
  -        }
  -        
  -        protected void emitFinish ( SaveCur c, QName name )
  -        {
  -            if (name != null)
  -                emitFinishHelper( name );
           }
           
  -        protected void emitFinishHelper ( QName name )
  +        protected void emitFinish ( SaveCur c )
           {
  -            assert name != null;
  -            
               emit( "</" );
  -            emitName( name );
  +            emitName( c.getName() );
               emit( '>' );
           }
  -
  +        
           protected void emitXmlns ( String prefix, String uri )
           {
               assert prefix != null;
  @@ -956,27 +938,6 @@
               emit( '"' );
           }
   
  -        private void emitContainerHelper (
  -            QName name, boolean close, ArrayList attrNames, ArrayList attrValues )
  -        {
  -            assert name != null;
  -
  -            emit( '<' );
  -            emitName( name );
  -
  -            if (saveNamespacesFirst())
  -                emitNamespacesHelper();
  -
  -            for ( int i = 0 ; i < attrNames.size() ; i++ )
  -                emitAttrHelper( (QName) attrNames.get( i ), (String) attrValues.get( i
) );
  -
  -            if (!saveNamespacesFirst())
  -                emitNamespacesHelper();
  -
  -            if (close)
  -                emit( '>' );
  -        }
  -        
           protected void emitText ( SaveCur c )
           {
               assert c.isText();
  @@ -1042,7 +1003,11 @@
   
               if (uri.length() != 0)
               {
  -                String prefix = getUriMapping( uri );
  +                String prefix = name.getPrefix();
  +                String mappedUri = getNamespaceForPrefix( prefix );
  +
  +                if (mappedUri == null || !mappedUri.equals( uri ))
  +                    prefix = getUriMapping( uri );
   
                   if (prefix.length() > 0)
                   {
  @@ -2212,9 +2177,11 @@
                       spaces( _sb, _newLine.length(), _prettyOffset + _prettyIndent * _depth
);
                   }
   
  -                if (prevKind != ROOT && !isLeaf)
  +                if (!isLeaf)
                   {
  -                    _sb.append( _newLine );
  +                    if (prevKind != ROOT)
  +                        _sb.append( _newLine );
  +                    
                       int d = k < 0 ? _depth - 1 : _depth;
                       spaces( _sb, _sb.length(), _prettyOffset + _prettyIndent * d );
                   }
  @@ -2317,7 +2284,7 @@
   
       private Map     _suggestedPrefixes;
       private boolean _useDefaultNamespace;
  -    private HashMap _preComputedNamespaces;
  +    private Map     _preComputedNamespaces;
       private boolean _saveNamespacesFirst;
   
       private ArrayList _attrNames;
  
  
  
  1.35      +29 -37    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.34
  retrieving revision 1.35
  diff -u -r1.34 -r1.35
  --- EricTest.java	12 Mar 2004 00:15:34 -0000	1.34
  +++ EricTest.java	12 Mar 2004 21:18:18 -0000	1.35
  @@ -106,37 +106,39 @@
   {
       public static void main ( String[] args ) throws Exception
       {
  -//        XmlOptions innerOptions = new XmlOptions();
  -//        innerOptions.setSaveInner();
  +        XmlOptions innerOptions = new XmlOptions();
  +        innerOptions.setSaveInner();
           
           XmlOptions prettyOptions = new XmlOptions();
           prettyOptions.setSavePrettyPrint();
  +        prettyOptions.setSavePrettyPrintOffset( 8 );
   
  -//        XmlOptions synthOptions = new XmlOptions();
  -//        synthOptions.setSaveSyntheticDocumentElement( new QName( "synth" ) );
  -        
  -//        XmlObject x = XmlObject.Factory.parse( "<a x='y'><b>33<c>11</c>22</b></a>"
);
  -//        XmlCursor cx = x.newCursor();
  -//        cx.save( System.out, prettyOptions );
  -
  -        Document doc = Public2.parse( new FileInputStream( new File( "c:\\ee.xml" ) ),
null );
  -        
  -        System.out.println( Public2.save( doc, prettyOptions ) );
  -        
  -//        XmlCursor c = Public2.newStore();
  -//
  -//        c.toNextToken();
  -//
  -//        c.beginElement( "foo" );
  -//        c.insertNamespace( "ns1", "ns1.com" );
  -//        c.insertAttributeWithValue( "x", "y" );
  -//        c.insertElementWithText( "bar", "text" );
  -//        c.insertElementWithText( "bar2", "text" );
  -//
  -//        c.toStartDoc();
  -//        Public2.dump( c );
  +        XmlOptions synthOptions = new XmlOptions();
  +        synthOptions.setSaveSyntheticDocumentElement( new QName( "synth" ) );
  +
  +        XmlCursor c = Public2.newStore();
  +
  +        c.toNextToken();
  +
  +        c.beginElement( "foo" );
  +        c.insertNamespace( "", "empty.com" );
  +        c.insertAttributeWithValue( "x", "y" );
  +        c.insertElementWithText( "bar", "hohoho", "text" );
  +        c.toPrevSibling();
  +        QName name = c.getName();
  +        c.setName( new QName( name.getNamespaceURI(), name.getLocalPart(), "nsss" ) );
  +        c.toNextSibling();
  +        c.insertElementWithText( "bar2", "text" );
   
  -/*
  +        c.toStartDoc();
  +        Public2.dump( c );
  +
  +        c.toStartDoc();
  +        c.toNextToken();
  +        c.toNextToken();
  +        c.toNextToken();
  +        System.out.println( "Attr: " + c.xmlText( prettyOptions ) );
  +        
           c.toStartDoc();
           System.out.println( "Rename document: " + c.xmlText( synthOptions ) );
           
  @@ -150,7 +152,7 @@
           
           c.toStartDoc();
           c.toNextToken();
  -        System.out.println( "Doc Elem: " + c.xmlText() );
  +        System.out.println( "Doc Elem: " + c.xmlText( prettyOptions ) );
           
           c.toStartDoc();
           System.out.println( "Whole doc: " + c.xmlText() );
  @@ -164,12 +166,6 @@
           c.toNextToken();
           c.toNextToken();
           c.toNextToken();
  -        System.out.println( "Attr: " + c.xmlText() );
  -        
  -        c.toStartDoc();
  -        c.toNextToken();
  -        c.toNextToken();
  -        c.toNextToken();
           c.toNextToken();
           System.out.println( "Nested elem: " + c.xmlText() );
           
  @@ -203,10 +199,6 @@
           c.toNextToken();
           c.toNextToken();
           System.out.println( "Rename Text: " + c.xmlText( synthOptions ) );
  -        
  -        // TODO - set saving of inner of elem with attrs, does the old
  -        // saver save out the attributes or just hte content?
  -*/
   
   //        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