Save This Page
Home » xmlbeans-2.5.0-src » org.apache.xmlbeans.impl » store » [javadoc | source]
    1   /*   Copyright 2004 The Apache Software Foundation
    2    *
    3    *   Licensed under the Apache License, Version 2.0 (the "License");
    4    *   you may not use this file except in compliance with the License.
    5    *   You may obtain a copy of the License at
    6    *
    7    *       http://www.apache.org/licenses/LICENSE-2.0
    8    *
    9    *   Unless required by applicable law or agreed to in writing, software
   10    *   distributed under the License is distributed on an "AS IS" BASIS,
   11    *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   12    *   See the License for the specific language governing permissions and
   13    *  limitations under the License.
   14    */
   15   
   16   package org.apache.xmlbeans.impl.store;
   17   
   18   import java.io.Reader;
   19   
   20   import java.util.HashMap;
   21   import java.util.Iterator;
   22   import java.util.ConcurrentModificationException;
   23   
   24   import javax.xml.namespace.NamespaceContext;
   25   import javax.xml.namespace.QName;
   26   
   27   import javax.xml.stream.XMLStreamReader;
   28   import javax.xml.stream.XMLStreamException;
   29   import javax.xml.stream.Location;
   30   
   31   import org.apache.xmlbeans.XmlOptions;
   32   import org.apache.xmlbeans.XmlLineNumber;
   33   import org.apache.xmlbeans.XmlDocumentProperties;
   34   
   35   import org.w3c.dom.Node;
   36   
   37   public class Jsr173
   38   {
   39       public static Node nodeFromStream ( XMLStreamReader xs )
   40       {
   41           if (!(xs instanceof Jsr173GateWay))
   42               return null;
   43   
   44           Jsr173GateWay gw = (Jsr173GateWay) xs;
   45   
   46           Locale l = gw._l;
   47                     
   48           if (l.noSync())         { l.enter(); try { return nodeFromStreamImpl( gw ); } finally { l.exit(); } }
   49           else synchronized ( l ) { l.enter(); try { return nodeFromStreamImpl( gw ); } finally { l.exit(); } }
   50           
   51       }
   52       
   53       public static Node nodeFromStreamImpl ( Jsr173GateWay gw )
   54       {
   55           Cur c = gw._xs.getStreamCur();
   56   
   57           return c.isNode() ? (Node) c.getDom() : (Node) null;
   58       }
   59   
   60       public static XMLStreamReader newXmlStreamReader ( Cur c, Object src, int off, int cch )
   61       {
   62           XMLStreamReaderBase xs = new XMLStreamReaderForString( c, src, off, cch );
   63           
   64           if (c._locale.noSync())
   65               return new UnsyncedJsr173( c._locale, xs );
   66           else
   67               return new SyncedJsr173( c._locale, xs );
   68       }
   69       
   70       public static XMLStreamReader newXmlStreamReader ( Cur c, XmlOptions options )
   71       {
   72           options = XmlOptions.maskNull( options );
   73           
   74           boolean inner = 
   75               options.hasOption( XmlOptions.SAVE_INNER ) &&
   76                   !options.hasOption( XmlOptions.SAVE_OUTER );
   77   
   78           XMLStreamReaderBase xs;
   79   
   80           int k = c.kind();
   81           
   82           if (k == Cur.TEXT || k < 0)
   83           {
   84               xs = new XMLStreamReaderForString( c, c.getChars( -1 ), c._offSrc, c._cchSrc );
   85           }
   86           else if (inner)
   87           {
   88               if (!c.hasAttrs() && !c.hasChildren())
   89                   xs = new XMLStreamReaderForString( c, c.getFirstChars(), c._offSrc, c._cchSrc );
   90               else
   91               {
   92                   assert c.isContainer();
   93                   xs = new XMLStreamReaderForNode( c, true );
   94               }
   95           }
   96           else
   97               xs = new XMLStreamReaderForNode( c, false );
   98           
   99           if (c._locale.noSync())
  100               return new UnsyncedJsr173( c._locale, xs );
  101           else
  102               return new SyncedJsr173( c._locale, xs );
  103       }
  104       
  105       //
  106       //
  107       //
  108       
  109       private static final class XMLStreamReaderForNode extends XMLStreamReaderBase
  110       {
  111           public XMLStreamReaderForNode ( Cur c, boolean inner )
  112           {
  113               super( c );
  114               
  115               assert c.isContainer() || c.isComment() || c.isProcinst() || c.isAttr();
  116   
  117               // Iterate over everything *between* _cur and _end.  Do
  118               // not iterate the thing to the right of _end
  119   
  120               if (inner)
  121               {
  122                   assert c.isContainer();
  123                   
  124                   _cur = c.weakCur( this );
  125   
  126                   if (!_cur.toFirstAttr())
  127                       _cur.next();
  128               
  129                   _end = c.weakCur( this );
  130                   _end.toEnd();
  131               }
  132               else
  133               {
  134                   _cur = c.weakCur( this );
  135   
  136                   if (c.isRoot())
  137                       _wholeDoc = true;
  138                   else
  139                   {
  140                       _end = c.weakCur( this );
  141   
  142                       if (c.isAttr())
  143                       {
  144                           if (!_end.toNextAttr())
  145                           {
  146                               _end.toParent();
  147                               _end.next();
  148                           }
  149                       }
  150                       else
  151                           _end.skip();
  152                   }
  153               }
  154   
  155               if (!_wholeDoc)
  156               {
  157                   // Set the _done bit properly
  158                   
  159                   _cur.push();
  160                   
  161                   try
  162                   {
  163                       next();
  164                   }
  165                   catch ( XMLStreamException e )
  166                   {
  167                       throw new RuntimeException( e.getMessage(), e );
  168                   }
  169                       
  170                   _cur.pop();
  171               }
  172   
  173               assert _wholeDoc || !_cur.isSamePos( _end );
  174           }
  175   
  176           protected Cur getStreamCur ( )
  177           {
  178               return _cur;
  179           }
  180   
  181           //
  182           //
  183           //
  184   
  185           public boolean hasNext ( ) throws XMLStreamException
  186           {
  187               checkChanged();
  188   
  189               return !_done;
  190           }
  191   
  192           public int getEventType ( )
  193           {
  194               switch ( _cur.kind() )
  195               {
  196                   case  Cur.ROOT     : return START_DOCUMENT;
  197                   case -Cur.ROOT     : return END_DOCUMENT;
  198                   case  Cur.ELEM     : return START_ELEMENT;
  199                   case -Cur.ELEM     : return END_ELEMENT;
  200                   case  Cur.ATTR     : return _cur.isXmlns() ? NAMESPACE : ATTRIBUTE;
  201                   case  Cur.TEXT     : return CHARACTERS;
  202                   case  Cur.COMMENT  : return COMMENT;
  203                   case  Cur.PROCINST : return PROCESSING_INSTRUCTION;
  204                   default            : throw new IllegalStateException();
  205               }
  206           }
  207   
  208           public int next ( ) throws XMLStreamException
  209           {
  210               checkChanged();
  211   
  212               if (!hasNext())
  213                   throw new IllegalStateException( "No next event in stream" );
  214   
  215               int kind = _cur.kind();
  216   
  217               if (kind == -Cur.ROOT)
  218               {
  219                   assert _wholeDoc;
  220                   _done = true;
  221               }
  222               else
  223               {
  224                   if (kind == Cur.ATTR)
  225                   {
  226                       if (!_cur.toNextAttr())
  227                       {
  228                           _cur.toParent();
  229                           _cur.next();
  230                       }
  231                   }
  232                   else if (kind == Cur.COMMENT || kind == Cur.PROCINST)
  233                       _cur.skip();
  234                   else if (kind == Cur.ROOT)
  235                   {
  236                       if (!_cur.toFirstAttr())
  237                           _cur.next();
  238                   }
  239                   else
  240                       _cur.next();
  241   
  242                   assert _wholeDoc || _end != null;
  243                   
  244                   _done = _wholeDoc ? _cur.kind() == -Cur.ROOT : _cur.isSamePos( _end );
  245               }
  246   
  247               _textFetched = false;
  248               _srcFetched = false;
  249   
  250               return getEventType();
  251           }
  252   
  253           public String getText ( )
  254           {
  255               checkChanged();
  256   
  257               int k = _cur.kind();
  258   
  259               if (k == Cur.COMMENT)
  260                   return _cur.getValueAsString();
  261   
  262               if (k == Cur.TEXT)
  263                   return _cur.getCharsAsString( -1 );
  264   
  265               throw new IllegalStateException();
  266           }
  267   
  268           public boolean isStartElement ( )
  269           {
  270               return getEventType() == START_ELEMENT;
  271           }
  272   
  273           public boolean isEndElement ( )
  274           {
  275               return getEventType() == END_ELEMENT;
  276           }
  277   
  278           public boolean isCharacters ( )
  279           {
  280               return getEventType() == CHARACTERS;
  281           }
  282   
  283           public String getElementText ( ) throws XMLStreamException
  284           {
  285               checkChanged();
  286   
  287               if (!isStartElement())
  288                   throw new IllegalStateException();
  289   
  290               StringBuffer sb = new StringBuffer();
  291   
  292               for ( ; ; )
  293               {
  294                   if (!hasNext())
  295                       throw new XMLStreamException();
  296   
  297                   int e = next();
  298   
  299                   if (e == END_ELEMENT)
  300                       break;
  301                   else if (e == START_ELEMENT)
  302                       throw new XMLStreamException();
  303                   else if (e != COMMENT && e != PROCESSING_INSTRUCTION)
  304                       sb.append( getText() );
  305               }
  306   
  307               return sb.toString();
  308           }
  309   
  310           public int nextTag ( ) throws XMLStreamException
  311           {
  312               checkChanged();
  313   
  314               for ( ; ; )
  315               {
  316                   if (isStartElement() || isEndElement())
  317                       return getEventType();
  318   
  319                   if (!isWhiteSpace())
  320                       throw new XMLStreamException();
  321   
  322                   if (!hasNext())
  323                       throw new XMLStreamException();
  324   
  325                   next();
  326               }
  327           }
  328   
  329           private static boolean matchAttr ( Cur c, String uri, String local )
  330           {
  331               assert c.isNormalAttr();
  332   
  333               QName name = c.getName();
  334   
  335               return
  336                   name.getLocalPart().equals( local ) &&
  337                       (uri == null || name.getNamespaceURI().equals( uri ));
  338           }
  339   
  340           private static Cur toAttr ( Cur c, String uri, String local )
  341           {
  342               if (uri == null || local == null || local.length() == 0)
  343                   throw new IllegalArgumentException();
  344   
  345               Cur ca = c.tempCur();
  346               boolean match = false;
  347   
  348               if (c.isElem())
  349               {
  350                   if (ca.toFirstAttr())
  351                   {
  352                       do
  353                       {
  354                           if (ca.isNormalAttr() && matchAttr( ca, uri, local ))
  355                           {
  356                               match = true;
  357                               break;
  358                           }
  359                       }
  360                       while ( ca.toNextSibling() );
  361                   }
  362               }
  363               else if (c.isNormalAttr())
  364                   match = matchAttr( c, uri, local );
  365               else
  366                   throw new IllegalStateException();
  367   
  368               if (!match)
  369               {
  370                   ca.release();
  371                   ca = null;
  372               }
  373   
  374               return ca;
  375           }
  376           
  377           public String getAttributeValue ( String uri, String local )
  378           {
  379               Cur ca = toAttr( _cur, uri, local );
  380   
  381               String value = null;
  382   
  383               if (ca != null)
  384               {
  385                   value = ca.getValueAsString();
  386                   ca.release();
  387               }
  388   
  389               return value;
  390           }
  391   
  392           private static Cur toAttr ( Cur c, int i )
  393           {
  394               if (i < 0)
  395                   throw new IndexOutOfBoundsException( "Attribute index is negative" );
  396   
  397               Cur ca = c.tempCur();
  398               boolean match = false;
  399   
  400               if (c.isElem())
  401               {
  402                   if (ca.toFirstAttr())
  403                   {
  404                       do
  405                       {
  406                           if (ca.isNormalAttr() && i-- == 0)
  407                           {
  408                               match = true;
  409                               break;
  410                           }
  411                       }
  412                       while ( ca.toNextSibling() );
  413                   }
  414               }
  415               else if (c.isNormalAttr())
  416                   match = i == 0;
  417               else
  418                   throw new IllegalStateException();
  419   
  420               if (!match)
  421               {
  422                   ca.release();
  423                   throw new IndexOutOfBoundsException( "Attribute index is too large" );
  424               }
  425   
  426               return ca;
  427           }
  428   
  429           public int getAttributeCount ( )
  430           {
  431               int n = 0;
  432               
  433               if (_cur.isElem())
  434               {
  435                   Cur ca = _cur.tempCur();
  436                   
  437                   if (ca.toFirstAttr())
  438                   {
  439                       do
  440                       {
  441                           if (ca.isNormalAttr())
  442                               n++;
  443                       }
  444                       while ( ca.toNextSibling() );
  445                   }
  446   
  447                   ca.release();
  448               }
  449               else if (_cur.isNormalAttr())
  450                   n++;
  451               else
  452                   throw new IllegalStateException();
  453   
  454               return n;
  455           }
  456   
  457           public QName getAttributeName ( int index )
  458           {
  459               Cur ca = toAttr( _cur, index );
  460               QName name = ca.getName();
  461               ca.release();
  462               return name;
  463           }
  464   
  465           public String getAttributeNamespace ( int index )
  466           {
  467               return getAttributeName( index ).getNamespaceURI();
  468           }
  469   
  470           public String getAttributeLocalName ( int index )
  471           {
  472               return getAttributeName( index ).getLocalPart();
  473           }
  474   
  475           public String getAttributePrefix ( int index )
  476           {
  477               return getAttributeName( index ).getPrefix();
  478           }
  479   
  480           public String getAttributeType ( int index )
  481           {
  482               toAttr( _cur, index ).release();
  483               return "CDATA";
  484           }
  485   
  486           public String getAttributeValue ( int index )
  487           {
  488               Cur ca = toAttr( _cur, index );
  489   
  490               String value = null;
  491   
  492               if (ca != null)
  493               {
  494                   value = ca.getValueAsString();
  495                   ca.release();
  496               }
  497   
  498               return value;
  499           }
  500   
  501           public boolean isAttributeSpecified ( int index )
  502           {
  503               // Go to attr to force index check
  504               Cur ca = toAttr( _cur, index );
  505               ca.release();
  506               
  507               return false;
  508           }
  509   
  510           public int getNamespaceCount ( )
  511           {
  512               int n = 0;
  513   
  514               if (_cur.isElem() || _cur.kind() == -Cur.ELEM)
  515               {
  516                   Cur ca = _cur.tempCur();
  517   
  518                   if (_cur.kind() == -Cur.ELEM)
  519                       ca.toParent();
  520   
  521                   if (ca.toFirstAttr())
  522                   {
  523                       do
  524                       {
  525                           if (ca.isXmlns())
  526                               n++;
  527                       }
  528                       while ( ca.toNextSibling() );
  529                   }
  530   
  531                   ca.release();
  532               }
  533               else if (_cur.isXmlns())
  534                   n++;
  535               else
  536                   throw new IllegalStateException();
  537   
  538               return n;
  539           }
  540   
  541           private static Cur toXmlns ( Cur c, int i )
  542           {
  543               if (i < 0)
  544                   throw new IndexOutOfBoundsException( "Namespace index is negative" );
  545   
  546               Cur ca = c.tempCur();
  547               boolean match = false;
  548   
  549               if (c.isElem() || c.kind() == -Cur.ELEM)
  550               {
  551                   if (c.kind() == -Cur.ELEM)
  552                       ca.toParent();
  553                   
  554                   if (ca.toFirstAttr())
  555                   {
  556                       do
  557                       {
  558                           if (ca.isXmlns() && i-- == 0)
  559                           {
  560                               match = true;
  561                               break;
  562                           }
  563                       }
  564                       while ( ca.toNextSibling() );
  565                   }
  566               }
  567               else if (c.isXmlns())
  568                   match = i == 0;
  569               else
  570                   throw new IllegalStateException();
  571   
  572               if (!match)
  573               {
  574                   ca.release();
  575                   throw new IndexOutOfBoundsException( "Namespace index is too large" );
  576               }
  577   
  578               return ca;
  579           }
  580   
  581           public String getNamespacePrefix ( int index )
  582           {
  583               Cur ca = toXmlns( _cur, index );
  584               String prefix = ca.getXmlnsPrefix();
  585               ca.release();
  586               return prefix;
  587           }
  588   
  589           public String getNamespaceURI ( int index )
  590           {
  591               Cur ca = toXmlns( _cur, index );
  592               String uri = ca.getXmlnsUri();
  593               ca.release();
  594               return uri;
  595           }
  596   
  597           private void fetchChars ( )
  598           {
  599               if (!_textFetched)
  600               {
  601                   int k = _cur.kind();
  602   
  603                   Cur cText = null;
  604   
  605                   if (k == Cur.COMMENT)
  606                   {
  607                       cText = _cur.tempCur();
  608                       cText.next();
  609                   }
  610                   else if (k == Cur.TEXT)
  611                       cText = _cur;
  612                   else
  613                       throw new IllegalStateException();
  614   
  615                   Object src = cText.getChars( -1 );
  616                   
  617                   ensureCharBufLen( cText._cchSrc );
  618   
  619                   CharUtil.getChars(
  620                       _chars, _offChars = 0, src, cText._offSrc, _cchChars = cText._cchSrc );
  621   
  622                   if (cText != _cur)
  623                       cText.release();
  624   
  625                   _textFetched = true;
  626               }
  627           }
  628           
  629           private void ensureCharBufLen ( int cch )
  630           {
  631               if (_chars == null || _chars.length < cch)
  632               {
  633                   int l = 256;
  634   
  635                   while ( l < cch )
  636                       l *= 2;
  637                   
  638                   _chars = new char [ l ];
  639               }
  640           }
  641   
  642           public char[] getTextCharacters ( )
  643           {
  644               checkChanged();
  645   
  646               fetchChars();
  647   
  648               return _chars;
  649           }
  650   
  651           public int getTextStart ( )
  652           {
  653               checkChanged();
  654   
  655               fetchChars();
  656   
  657               return _offChars;
  658           }
  659   
  660           public int getTextLength ( )
  661           {
  662               checkChanged();
  663   
  664               fetchChars();
  665   
  666               return _cchChars;
  667           }
  668   
  669           public int getTextCharacters (
  670               int sourceStart, char[] target, int targetStart, int length )
  671                   throws XMLStreamException
  672           {
  673               if (length < 0)
  674                   throw new IndexOutOfBoundsException();
  675   
  676               if (targetStart < 0 || targetStart >= target.length)
  677                   throw new IndexOutOfBoundsException();
  678   
  679               if (targetStart + length > target.length)
  680                   throw new IndexOutOfBoundsException();
  681   
  682               if (!_srcFetched)
  683               {
  684                   int k = _cur.kind();
  685   
  686                   Cur cText = null;
  687   
  688                   if (k == Cur.COMMENT)
  689                   {
  690                       cText = _cur.tempCur();
  691                       cText.next();
  692                   }
  693                   else if (k == Cur.TEXT)
  694                       cText = _cur;
  695                   else
  696                       throw new IllegalStateException();
  697               
  698                   _src = cText.getChars( -1 );
  699                   _offSrc = cText._offSrc;
  700                   _cchSrc = cText._cchSrc;
  701                            
  702                   if (cText != _cur)
  703                       cText.release();
  704                   
  705                   _srcFetched = true;
  706               }
  707   
  708               if (sourceStart > _cchSrc)
  709                   throw new IndexOutOfBoundsException();
  710   
  711               if (sourceStart + length > _cchSrc)
  712                   length = _cchSrc - sourceStart;
  713   
  714               CharUtil.getChars( target, targetStart, _src, _offSrc, length );
  715               
  716               return length;
  717           }
  718   
  719           public boolean hasText ( )
  720           {
  721               int k = _cur.kind();
  722               
  723               return k == Cur.COMMENT || k == Cur.TEXT;
  724           }
  725   
  726           public boolean hasName ( )
  727           {
  728               int k = _cur.kind();
  729               return k == Cur.ELEM || k == -Cur.ELEM;
  730           }
  731   
  732           public QName getName ( )
  733           {
  734               if (!hasName())
  735                   throw new IllegalStateException();
  736   
  737               return _cur.getName();
  738           }
  739   
  740           public String getNamespaceURI ( )
  741           {
  742               return getName().getNamespaceURI();
  743           }
  744   
  745           public String getLocalName ( )
  746           {
  747               return getName().getLocalPart();
  748           }
  749   
  750           public String getPrefix ( )
  751           {
  752               return getName().getPrefix();
  753           }
  754   
  755           public String getPITarget ( )
  756           {
  757               return _cur.kind() == Cur.PROCINST ? _cur.getName().getLocalPart() : null;
  758           }
  759   
  760           public String getPIData ( )
  761           {
  762               return _cur.kind() == Cur.PROCINST ? _cur.getValueAsString() : null;
  763           }
  764   
  765           //
  766           //
  767           //
  768   
  769           private boolean _wholeDoc;
  770           private boolean _done;
  771                   
  772           private Cur _cur;
  773           private Cur _end;
  774   
  775           private boolean _srcFetched;
  776           private Object  _src;
  777           private int     _offSrc;
  778           private int     _cchSrc;
  779           
  780           private boolean _textFetched;
  781           private char[]  _chars;
  782           private int     _offChars;
  783           private int     _cchChars;
  784       }
  785       
  786       //
  787       //
  788       //
  789   
  790       private static abstract class XMLStreamReaderBase
  791           implements XMLStreamReader, NamespaceContext, Location
  792       {
  793           XMLStreamReaderBase ( Cur c )
  794           {
  795               _locale = c._locale;
  796               _version = _locale.version();
  797           }
  798   
  799           protected final void checkChanged ( )
  800           {
  801               if (_version != _locale.version())
  802                   throw new ConcurrentModificationException( "Document changed while streaming" );
  803           }
  804   
  805           //
  806           // XMLStreamReader methods
  807           //
  808   
  809           public void close ( ) throws XMLStreamException
  810           {
  811               checkChanged();
  812           }
  813   
  814           public boolean isWhiteSpace ( )
  815           {
  816               checkChanged();
  817   
  818               // TODO - avoid creating a string here
  819               String s = getText();
  820   
  821               return _locale.getCharUtil().isWhiteSpace( s, 0, s.length() );
  822           }
  823   
  824           public Location getLocation ( )
  825           {
  826               checkChanged();
  827   
  828               Cur c = getStreamCur();
  829   
  830               XmlLineNumber ln = (XmlLineNumber) c.getBookmark( XmlLineNumber.class );
  831   
  832               // BUGBUG - put source name here
  833               _uri = null;
  834   
  835               if (ln != null)
  836               {
  837                   _line = ln.getLine();
  838                   _column = ln.getColumn();
  839                   _offset = ln.getOffset();
  840               }
  841               else
  842               {
  843                   _line = -1;
  844                   _column = -1;
  845                   _offset = -1;
  846               }
  847   
  848               return this;
  849           }
  850   
  851   
  852           public Object getProperty ( String name )
  853           {
  854               checkChanged();
  855   
  856               if (name == null)
  857                   throw new IllegalArgumentException( "Property name is null" );
  858   
  859               // BUGBUG - I should implement some perperties here
  860               
  861               return null;
  862           }
  863   
  864           public String getCharacterEncodingScheme ( )
  865           {
  866               checkChanged();
  867   
  868               XmlDocumentProperties props = _locale.getDocProps( getStreamCur(), false );
  869   
  870               return props == null ? null : props.getEncoding();
  871           }
  872   
  873           public String getEncoding ( )
  874           {
  875               // BUGBUG - this should probably return the actual decoding used on the document
  876               return null;
  877           }
  878   
  879           public String getVersion ( )
  880           {
  881               checkChanged();
  882   
  883               XmlDocumentProperties props = _locale.getDocProps( getStreamCur(), false );
  884   
  885               return props == null ? null : props.getVersion();
  886           }
  887   
  888           public boolean isStandalone ( )
  889           {
  890               checkChanged();
  891   
  892               XmlDocumentProperties props = _locale.getDocProps( getStreamCur(), false );
  893   
  894               return props == null ? false : props.getStandalone();
  895           }
  896   
  897           public boolean standaloneSet ( )
  898           {
  899               checkChanged();
  900   
  901               return false;
  902           }
  903   
  904           public void require ( int type, String namespaceURI, String localName )
  905               throws XMLStreamException
  906           {
  907               checkChanged();
  908   
  909               if (type != getEventType())
  910                   throw new XMLStreamException();
  911   
  912               if (namespaceURI != null && !getNamespaceURI().equals( namespaceURI ))
  913                   throw new XMLStreamException();
  914   
  915               if (localName != null && !getLocalName().equals( localName ))
  916                   throw new XMLStreamException();
  917           }
  918   
  919           //
  920           // Location methods do not need a gatway
  921           //
  922   
  923           public int    getCharacterOffset ( ) { return _offset; }
  924           public int    getColumnNumber    ( ) { return _column; }
  925           public int    getLineNumber      ( ) { return _line;   }
  926           public String getLocationURI     ( ) { return _uri;    }
  927           
  928           public String getPublicId ( ) { return null; }
  929           public String getSystemId ( ) { return null; }
  930   
  931           public NamespaceContext getNamespaceContext ( )
  932           {
  933               throw new RuntimeException( "This version of getNamespaceContext should not be called");
  934           }
  935   
  936           public String getNamespaceURI ( String prefix )
  937           {
  938               checkChanged();
  939   
  940               Cur c = getStreamCur();
  941   
  942               c.push();
  943   
  944               if (!c.isContainer())
  945                   c.toParent();
  946               
  947               String ns = c.namespaceForPrefix( prefix, true );
  948   
  949               c.pop();
  950   
  951               return ns;
  952           }
  953   
  954           public String getPrefix ( String namespaceURI )
  955           {
  956               checkChanged();
  957   
  958               Cur c = getStreamCur();
  959   
  960               c.push();
  961   
  962               if (!c.isContainer())
  963                   c.toParent();
  964               
  965               String prefix = c.prefixForNamespace( namespaceURI, null, false );
  966               
  967               c.pop();
  968   
  969               return prefix;
  970           }
  971   
  972           public Iterator getPrefixes ( String namespaceURI )
  973           {
  974               checkChanged();
  975   
  976               // BUGBUG - get only one for now ...
  977   
  978               HashMap map = new HashMap();
  979   
  980               map.put( namespaceURI, getPrefix( namespaceURI ) );
  981   
  982               return map.values().iterator();
  983           }
  984   
  985           //
  986           //
  987           //
  988   
  989           protected abstract Cur getStreamCur ( );
  990   
  991           //
  992           //
  993           //
  994   
  995           private Locale _locale;
  996           private long   _version;
  997           
  998           String _uri;
  999           
 1000           int _line   = -1;
 1001           int _column = -1;
 1002           int _offset = -1;
 1003       }
 1004       
 1005       //
 1006       //
 1007       //
 1008   
 1009       private static final class XMLStreamReaderForString extends XMLStreamReaderBase
 1010       {
 1011           XMLStreamReaderForString ( Cur c, Object src, int off, int cch )
 1012           {
 1013               super( c );
 1014   
 1015               _src = src;
 1016               _off = off;
 1017               _cch = cch;
 1018   
 1019               _cur = c;
 1020           }
 1021   
 1022           protected Cur getStreamCur ( )
 1023           {
 1024               return _cur;
 1025           }
 1026   
 1027           //
 1028           // Legal stream methods
 1029           //
 1030   
 1031           public String getText ( )
 1032           {
 1033               checkChanged();
 1034   
 1035               return CharUtil.getString( _src, _off, _cch );
 1036           }
 1037           
 1038           public char[] getTextCharacters ( )
 1039           {
 1040               checkChanged();
 1041   
 1042               char[] chars = new char [ _cch ];
 1043   
 1044               CharUtil.getChars( chars, 0, _src, _off, _cch );
 1045   
 1046               return chars;
 1047           }
 1048           public int getTextStart ( )
 1049           {
 1050               checkChanged();
 1051   
 1052               return _off;
 1053           }
 1054           public int getTextLength ( )
 1055           {
 1056               checkChanged();
 1057   
 1058               return _cch;
 1059           }
 1060           
 1061           public int getTextCharacters ( int sourceStart, char[] target, int targetStart, int length )
 1062           {
 1063               checkChanged();
 1064   
 1065               if (length < 0)
 1066                   throw new IndexOutOfBoundsException();
 1067               
 1068               if (sourceStart > _cch)
 1069                   throw new IndexOutOfBoundsException();
 1070   
 1071               if (sourceStart + length > _cch)
 1072                   length = _cch - sourceStart;
 1073               
 1074               CharUtil.getChars( target, targetStart, _src, _off + sourceStart, length );
 1075   
 1076               return length;
 1077           }
 1078   
 1079           public int     getEventType      ( ) { checkChanged(); return CHARACTERS; }
 1080           public boolean hasName           ( ) { checkChanged(); return false;      }
 1081           public boolean hasNext           ( ) { checkChanged(); return false;      }
 1082           public boolean hasText           ( ) { checkChanged(); return true;       }
 1083           public boolean isCharacters      ( ) { checkChanged(); return true;       }
 1084           public boolean isEndElement      ( ) { checkChanged(); return false;      }
 1085           public boolean isStartElement    ( ) { checkChanged(); return false;      }
 1086   
 1087           //
 1088           // Illegal stream methods
 1089           //
 1090   
 1091           public int     getAttributeCount ( ) { throw new IllegalStateException(); }
 1092           public String  getAttributeLocalName ( int index ) { throw new IllegalStateException(); }
 1093           public QName   getAttributeName ( int index ) { throw new IllegalStateException(); }
 1094           public String  getAttributeNamespace ( int index ) { throw new IllegalStateException(); }
 1095           public String  getAttributePrefix ( int index ) { throw new IllegalStateException(); }
 1096           public String  getAttributeType ( int index ) { throw new IllegalStateException(); }
 1097           public String  getAttributeValue ( int index ) { throw new IllegalStateException(); }
 1098           public String  getAttributeValue ( String namespaceURI, String localName ) { throw new IllegalStateException(); }
 1099           public String  getElementText ( ) { throw new IllegalStateException(); }
 1100           public String  getLocalName ( ) { throw new IllegalStateException(); }
 1101           public QName   getName ( ) { throw new IllegalStateException(); }
 1102           public int     getNamespaceCount ( ) { throw new IllegalStateException(); }
 1103           public String  getNamespacePrefix ( int index ) { throw new IllegalStateException(); }
 1104           public String  getNamespaceURI ( int index ) { throw new IllegalStateException(); }
 1105           public String  getNamespaceURI ( ) { throw new IllegalStateException(); }
 1106           public String  getPIData ( ) { throw new IllegalStateException(); }
 1107           public String  getPITarget ( ) { throw new IllegalStateException(); }
 1108           public String  getPrefix ( ) { throw new IllegalStateException(); }
 1109           public boolean isAttributeSpecified ( int index ) { throw new IllegalStateException(); }
 1110           public int     next ( ) { throw new IllegalStateException(); }
 1111           public int     nextTag ( ) { throw new IllegalStateException(); }
 1112           public String  getPublicId() { throw new IllegalStateException();  }
 1113           public String  getSystemId() { throw new IllegalStateException();  }
 1114   
 1115           private Cur    _cur;
 1116           private Object _src;
 1117           private int    _off;
 1118           private int    _cch;
 1119       }
 1120   
 1121       //
 1122       //
 1123       //
 1124   
 1125       private static abstract class Jsr173GateWay
 1126       {
 1127           public Jsr173GateWay ( Locale l, XMLStreamReaderBase xs ) { _l = l; _xs = xs; }
 1128           
 1129           Locale              _l;
 1130           XMLStreamReaderBase _xs;
 1131       }
 1132   
 1133       private static final class SyncedJsr173 extends Jsr173GateWay implements XMLStreamReader, Location, NamespaceContext
 1134       {
 1135           public SyncedJsr173 ( Locale l, XMLStreamReaderBase xs ) { super( l, xs ); }
 1136           
 1137           public Object getProperty ( java.lang.String name ) { synchronized ( _l ) { _l.enter(); try { return _xs.getProperty( name ); } finally { _l.exit(); } } }
 1138           public int next ( ) throws XMLStreamException { synchronized ( _l ) { _l.enter(); try { return _xs.next(); } finally { _l.exit(); } } }
 1139           public void require ( int type, String namespaceURI, String localName ) throws XMLStreamException { synchronized ( _l ) { _l.enter(); try { _xs.require( type, namespaceURI, localName ); } finally { _l.exit(); } } }
 1140           public String getElementText ( ) throws XMLStreamException { synchronized ( _l ) { _l.enter(); try { return _xs.getElementText(); } finally { _l.exit(); } } }
 1141           public int nextTag ( ) throws XMLStreamException { synchronized ( _l ) { _l.enter(); try { return _xs.nextTag(); } finally { _l.exit(); } } }
 1142           public boolean hasNext ( ) throws XMLStreamException { synchronized ( _l ) { _l.enter(); try { return _xs.hasNext(); } finally { _l.exit(); } } }
 1143           public void close ( ) throws XMLStreamException { synchronized ( _l ) { _l.enter(); try { _xs.close(); } finally { _l.exit(); } } }
 1144           public String getNamespaceURI ( String prefix ) { synchronized ( _l ) { _l.enter(); try { return _xs.getNamespaceURI ( prefix ); } finally { _l.exit(); } } }
 1145           public boolean isStartElement ( ) { synchronized ( _l ) { _l.enter(); try { return _xs.isStartElement(); } finally { _l.exit(); } } }
 1146           public boolean isEndElement ( ) { synchronized ( _l ) { _l.enter(); try { return _xs.isEndElement(); } finally { _l.exit(); } } }
 1147           public boolean isCharacters ( ) { synchronized ( _l ) { _l.enter(); try { return _xs.isCharacters(); } finally { _l.exit(); } } }
 1148           public boolean isWhiteSpace ( ) { synchronized ( _l ) { _l.enter(); try { return _xs.isWhiteSpace(); } finally { _l.exit(); } } }
 1149           public String getAttributeValue ( String namespaceURI, String localName ) { synchronized ( _l ) { _l.enter(); try { return _xs.getAttributeValue ( namespaceURI, localName ); } finally { _l.exit(); } } }
 1150           public int getAttributeCount ( ) { synchronized ( _l ) { _l.enter(); try { return _xs.getAttributeCount(); } finally { _l.exit(); } } }
 1151           public QName getAttributeName ( int index ) { synchronized ( _l ) { _l.enter(); try { return _xs.getAttributeName ( index ); } finally { _l.exit(); } } }
 1152           public String getAttributeNamespace ( int index ) { synchronized ( _l ) { _l.enter(); try { return _xs.getAttributeNamespace ( index ); } finally { _l.exit(); } } }
 1153           public String getAttributeLocalName ( int index ) { synchronized ( _l ) { _l.enter(); try { return _xs.getAttributeLocalName ( index ); } finally { _l.exit(); } } }
 1154           public String getAttributePrefix ( int index ) { synchronized ( _l ) { _l.enter(); try { return _xs.getAttributePrefix ( index ); } finally { _l.exit(); } } }
 1155           public String getAttributeType ( int index ) { synchronized ( _l ) { _l.enter(); try { return _xs.getAttributeType ( index ); } finally { _l.exit(); } } }
 1156           public String getAttributeValue ( int index ) { synchronized ( _l ) { _l.enter(); try { return _xs.getAttributeValue ( index ); } finally { _l.exit(); } } }
 1157           public boolean isAttributeSpecified ( int index ) { synchronized ( _l ) { _l.enter(); try { return _xs.isAttributeSpecified ( index ); } finally { _l.exit(); } } }
 1158           public int getNamespaceCount ( ) { synchronized ( _l ) { _l.enter(); try { return _xs.getNamespaceCount(); } finally { _l.exit(); } } }
 1159           public String getNamespacePrefix ( int index ) { synchronized ( _l ) { _l.enter(); try { return _xs.getNamespacePrefix ( index ); } finally { _l.exit(); } } }
 1160           public String getNamespaceURI ( int index ) { synchronized ( _l ) { _l.enter(); try { return _xs.getNamespaceURI ( index ); } finally { _l.exit(); } } }
 1161           public NamespaceContext getNamespaceContext ( ) { return this; }
 1162           public int getEventType ( ) { synchronized ( _l ) { _l.enter(); try { return _xs.getEventType(); } finally { _l.exit(); } } }
 1163           public String getText ( ) { synchronized ( _l ) { _l.enter(); try { return _xs.getText(); } finally { _l.exit(); } } }
 1164           public char[] getTextCharacters ( ) { synchronized ( _l ) { _l.enter(); try { return _xs.getTextCharacters(); } finally { _l.exit(); } } }
 1165           public int getTextCharacters ( int sourceStart, char[] target, int targetStart, int length ) throws XMLStreamException { synchronized ( _l ) { _l.enter(); try { return _xs.getTextCharacters ( sourceStart, target, targetStart, length ); } finally { _l.exit(); } } }
 1166           public int getTextStart ( ) { synchronized ( _l ) { _l.enter(); try { return _xs.getTextStart(); } finally { _l.exit(); } } }
 1167           public int getTextLength ( ) { synchronized ( _l ) { _l.enter(); try { return _xs.getTextLength(); } finally { _l.exit(); } } }
 1168           public String getEncoding ( ) { synchronized ( _l ) { _l.enter(); try { return _xs.getEncoding(); } finally { _l.exit(); } } }
 1169           public boolean hasText ( ) { synchronized ( _l ) { _l.enter(); try { return _xs.hasText(); } finally { _l.exit(); } } }
 1170           public Location getLocation ( ) { synchronized ( _l ) { _l.enter(); try { return _xs.getLocation(); } finally { _l.exit(); } } }
 1171           public QName getName ( ) { synchronized ( _l ) { _l.enter(); try { return _xs.getName(); } finally { _l.exit(); } } }
 1172           public String getLocalName ( ) { synchronized ( _l ) { _l.enter(); try { return _xs.getLocalName(); } finally { _l.exit(); } } }
 1173           public boolean hasName ( ) { synchronized ( _l ) { _l.enter(); try { return _xs.hasName(); } finally { _l.exit(); } } }
 1174           public String getNamespaceURI ( ) { synchronized ( _l ) { _l.enter(); try { return _xs.getNamespaceURI(); } finally { _l.exit(); } } }
 1175           public String getPrefix ( ) { synchronized ( _l ) { _l.enter(); try { return _xs.getPrefix(); } finally { _l.exit(); } } }
 1176           public String getVersion ( ) { synchronized ( _l ) { _l.enter(); try { return _xs.getVersion(); } finally { _l.exit(); } } }
 1177           public boolean isStandalone ( ) { synchronized ( _l ) { _l.enter(); try { return _xs.isStandalone(); } finally { _l.exit(); } } }
 1178           public boolean standaloneSet ( ) { synchronized ( _l ) { _l.enter(); try { return _xs.standaloneSet(); } finally { _l.exit(); } } }
 1179           public String getCharacterEncodingScheme ( ) { synchronized ( _l ) { _l.enter(); try { return _xs.getCharacterEncodingScheme(); } finally { _l.exit(); } } }
 1180           public String getPITarget ( ) { synchronized ( _l ) { _l.enter(); try { return _xs.getPITarget(); } finally { _l.exit(); } } }
 1181           public String getPIData ( ) { synchronized ( _l ) { _l.enter(); try { return _xs.getPIData(); } finally { _l.exit(); } } }
 1182           public String getPrefix ( String namespaceURI ) { synchronized ( _l ) { _l.enter(); try { return _xs.getPrefix( namespaceURI ); } finally { _l.exit(); } } }
 1183           public Iterator getPrefixes ( String namespaceURI ) { synchronized ( _l ) { _l.enter(); try { return _xs.getPrefixes( namespaceURI ); } finally { _l.exit(); } } }
 1184           public int getCharacterOffset ( ) { synchronized ( _l ) { _l.enter(); try { return _xs.getCharacterOffset(); } finally { _l.exit(); } } }
 1185           public int getColumnNumber ( ) { synchronized ( _l ) { _l.enter(); try { return _xs.getColumnNumber(); } finally { _l.exit(); } } }
 1186           public int getLineNumber ( ) { synchronized ( _l ) { _l.enter(); try { return _xs.getLineNumber(); } finally { _l.exit(); } } }
 1187           public String getLocationURI ( ) { synchronized ( _l ) { _l.enter(); try { return _xs.getLocationURI(); } finally { _l.exit(); } } }
 1188           public String getPublicId() { synchronized ( _l ) { _l.enter(); try { return _xs.getPublicId(); } finally { _l.exit(); } } }
 1189           public String getSystemId() { synchronized ( _l ) { _l.enter(); try { return _xs.getSystemId(); } finally { _l.exit(); } } }
 1190       }
 1191   
 1192       private static final class UnsyncedJsr173 extends Jsr173GateWay implements XMLStreamReader, Location, NamespaceContext
 1193       {
 1194           public UnsyncedJsr173 ( Locale l, XMLStreamReaderBase xs ) { super( l, xs ); }
 1195           
 1196           public Object getProperty ( java.lang.String name ) { try { _l.enter(); return _xs.getProperty( name ); } finally { _l.exit(); } }
 1197           public int next ( ) throws XMLStreamException { try { _l.enter(); return _xs.next(); } finally { _l.exit(); } }
 1198           public void require ( int type, String namespaceURI, String localName ) throws XMLStreamException { try { _l.enter(); _xs.require( type, namespaceURI, localName ); } finally { _l.exit(); } }
 1199           public String getElementText ( ) throws XMLStreamException { try { _l.enter(); return _xs.getElementText(); } finally { _l.exit(); } }
 1200           public int nextTag ( ) throws XMLStreamException { try { _l.enter(); return _xs.nextTag(); } finally { _l.exit(); } }
 1201           public boolean hasNext ( ) throws XMLStreamException { try { _l.enter(); return _xs.hasNext(); } finally { _l.exit(); } }
 1202           public void close ( ) throws XMLStreamException { try { _l.enter(); _xs.close(); } finally { _l.exit(); } }
 1203           public String getNamespaceURI ( String prefix ) { try { _l.enter(); return _xs.getNamespaceURI ( prefix ); } finally { _l.exit(); } }
 1204           public boolean isStartElement ( ) { try { _l.enter(); return _xs.isStartElement(); } finally { _l.exit(); } }
 1205           public boolean isEndElement ( ) { try { _l.enter(); return _xs.isEndElement(); } finally { _l.exit(); } }
 1206           public boolean isCharacters ( ) { try { _l.enter(); return _xs.isCharacters(); } finally { _l.exit(); } }
 1207           public boolean isWhiteSpace ( ) { try { _l.enter(); return _xs.isWhiteSpace(); } finally { _l.exit(); } }
 1208           public String getAttributeValue ( String namespaceURI, String localName ) { try { _l.enter(); return _xs.getAttributeValue ( namespaceURI, localName ); } finally { _l.exit(); } }
 1209           public int getAttributeCount ( ) { try { _l.enter(); return _xs.getAttributeCount(); } finally { _l.exit(); } }
 1210           public QName getAttributeName ( int index ) { try { _l.enter(); return _xs.getAttributeName ( index ); } finally { _l.exit(); } }
 1211           public String getAttributeNamespace ( int index ) { try { _l.enter(); return _xs.getAttributeNamespace ( index ); } finally { _l.exit(); } }
 1212           public String getAttributeLocalName ( int index ) { try { _l.enter(); return _xs.getAttributeLocalName ( index ); } finally { _l.exit(); } }
 1213           public String getAttributePrefix ( int index ) { try { _l.enter(); return _xs.getAttributePrefix ( index ); } finally { _l.exit(); } }
 1214           public String getAttributeType ( int index ) { try { _l.enter(); return _xs.getAttributeType ( index ); } finally { _l.exit(); } }
 1215           public String getAttributeValue ( int index ) { try { _l.enter(); return _xs.getAttributeValue ( index ); } finally { _l.exit(); } }
 1216           public boolean isAttributeSpecified ( int index ) { try { _l.enter(); return _xs.isAttributeSpecified ( index ); } finally { _l.exit(); } }
 1217           public int getNamespaceCount ( ) { try { _l.enter(); return _xs.getNamespaceCount(); } finally { _l.exit(); } }
 1218           public String getNamespacePrefix ( int index ) { try { _l.enter(); return _xs.getNamespacePrefix ( index ); } finally { _l.exit(); } }
 1219           public String getNamespaceURI ( int index ) { try { _l.enter(); return _xs.getNamespaceURI ( index ); } finally { _l.exit(); } }
 1220           public NamespaceContext getNamespaceContext ( ) { return this; }
 1221           public int getEventType ( ) { try { _l.enter(); return _xs.getEventType(); } finally { _l.exit(); } }
 1222           public String getText ( ) { try { _l.enter(); return _xs.getText(); } finally { _l.exit(); } }
 1223           public char[] getTextCharacters ( ) { try { _l.enter(); return _xs.getTextCharacters(); } finally { _l.exit(); } }
 1224           public int getTextCharacters ( int sourceStart, char[] target, int targetStart, int length ) throws XMLStreamException { try { _l.enter(); return _xs.getTextCharacters ( sourceStart, target, targetStart, length ); } finally { _l.exit(); } }
 1225           public int getTextStart ( ) { try { _l.enter(); return _xs.getTextStart(); } finally { _l.exit(); } }
 1226           public int getTextLength ( ) { try { _l.enter(); return _xs.getTextLength(); } finally { _l.exit(); } }
 1227           public String getEncoding ( ) { try { _l.enter(); return _xs.getEncoding(); } finally { _l.exit(); } }
 1228           public boolean hasText ( ) { try { _l.enter(); return _xs.hasText(); } finally { _l.exit(); } }
 1229           public Location getLocation ( ) { try { _l.enter(); return _xs.getLocation(); } finally { _l.exit(); } }
 1230           public QName getName ( ) { try { _l.enter(); return _xs.getName(); } finally { _l.exit(); } }
 1231           public String getLocalName ( ) { try { _l.enter(); return _xs.getLocalName(); } finally { _l.exit(); } }
 1232           public boolean hasName ( ) { try { _l.enter(); return _xs.hasName(); } finally { _l.exit(); } }
 1233           public String getNamespaceURI ( ) { try { _l.enter(); return _xs.getNamespaceURI(); } finally { _l.exit(); } }
 1234           public String getPrefix ( ) { try { _l.enter(); return _xs.getPrefix(); } finally { _l.exit(); } }
 1235           public String getVersion ( ) { try { _l.enter(); return _xs.getVersion(); } finally { _l.exit(); } }
 1236           public boolean isStandalone ( ) { try { _l.enter(); return _xs.isStandalone(); } finally { _l.exit(); } }
 1237           public boolean standaloneSet ( ) { try { _l.enter(); return _xs.standaloneSet(); } finally { _l.exit(); } }
 1238           public String getCharacterEncodingScheme ( ) { try { _l.enter(); return _xs.getCharacterEncodingScheme(); } finally { _l.exit(); } }
 1239           public String getPITarget ( ) { try { _l.enter(); return _xs.getPITarget(); } finally { _l.exit(); } }
 1240           public String getPIData ( ) { try { _l.enter(); return _xs.getPIData(); } finally { _l.exit(); } }
 1241           public String getPrefix ( String namespaceURI ) { try { _l.enter(); return _xs.getPrefix( namespaceURI ); } finally { _l.exit(); } }
 1242           public Iterator getPrefixes ( String namespaceURI ) { try { _l.enter(); return _xs.getPrefixes( namespaceURI ); } finally { _l.exit(); } }
 1243           public int getCharacterOffset ( ) { try { _l.enter(); return _xs.getCharacterOffset(); } finally { _l.exit(); } }
 1244           public int getColumnNumber ( ) { try { _l.enter(); return _xs.getColumnNumber(); } finally { _l.exit(); } }
 1245           public int getLineNumber ( ) { synchronized ( _l ) { _l.enter(); try { return _xs.getLineNumber(); } finally { _l.exit(); } } }
 1246           public String getLocationURI ( ) { synchronized ( _l ) { _l.enter(); try { return _xs.getLocationURI(); } finally { _l.exit(); } } }
 1247           public String getPublicId() { synchronized ( _l ) { _l.enter(); try { return _xs.getPublicId(); } finally { _l.exit(); } } }
 1248           public String getSystemId() { synchronized ( _l ) { _l.enter(); try { return _xs.getSystemId(); } finally { _l.exit(); } } }
 1249       }
 1250   }
 1251   
 1252    

Save This Page
Home » xmlbeans-2.5.0-src » org.apache.xmlbeans.impl » store » [javadoc | source]