Home » xmlbeans-2.5.0-src » org.apache.xmlbeans.impl » richParser » [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.richParser;
   17   
   18   import org.apache.xmlbeans.GDate;
   19   import org.apache.xmlbeans.GDateBuilder;
   20   import org.apache.xmlbeans.GDuration;
   21   import org.apache.xmlbeans.XmlCalendar;
   22   import org.apache.xmlbeans.impl.common.XMLChar;
   23   import org.apache.xmlbeans.impl.util.XsTypeConverter;
   24   import org.apache.xmlbeans.impl.util.Base64;
   25   import org.apache.xmlbeans.impl.util.HexBin;
   26   import org.apache.xmlbeans.impl.common.InvalidLexicalValueException;
   27   import org.apache.xmlbeans.impl.common.XmlWhitespace;
   28   
   29   import javax.xml.namespace.NamespaceContext;
   30   import javax.xml.namespace.QName;
   31   import javax.xml.stream.Location;
   32   import javax.xml.stream.XMLStreamException;
   33   import javax.xml.stream.XMLStreamReader;
   34   import java.io.ByteArrayInputStream;
   35   import java.io.InputStream;
   36   import java.math.BigDecimal;
   37   import java.math.BigInteger;
   38   import java.util.Date;
   39   
   40   /**
   41    * Author: Cezar Andrei (cezar.andrei at bea.com)
   42    * Date: Nov 17, 2003
   43    */
   44   public class XMLStreamReaderExtImpl
   45       implements XMLStreamReaderExt
   46   {
   47       private final XMLStreamReader _xmlStream;
   48       private final CharSeqTrimWS _charSeq;
   49       private String _defaultValue;
   50   
   51       public XMLStreamReaderExtImpl(XMLStreamReader xmlStream)
   52       {
   53           if (xmlStream==null)
   54               throw new IllegalArgumentException();
   55   
   56           _xmlStream = xmlStream;
   57           _charSeq = new CharSeqTrimWS(this);
   58       }
   59   
   60       public XMLStreamReader getUnderlyingXmlStream()
   61       {
   62           return _xmlStream;
   63       }
   64   
   65       // XMLStreamReaderExt methods
   66       public String getStringValue()
   67           throws XMLStreamException
   68       {
   69           _charSeq.reload(CharSeqTrimWS.XMLWHITESPACE_PRESERVE);
   70           return _charSeq.toString();
   71       }
   72   
   73       public String getStringValue(int wsStyle)
   74           throws XMLStreamException
   75       {
   76           _charSeq.reload(CharSeqTrimWS.XMLWHITESPACE_PRESERVE);
   77           //REVIEW zieg 2004-01-11 - we should write a collapse method
   78           //that takes a CharSequence to void creating this extra String object
   79           return XmlWhitespace.collapse(_charSeq.toString(), wsStyle);
   80       }
   81   
   82       public boolean getBooleanValue()
   83           throws XMLStreamException, InvalidLexicalValueException
   84       {
   85           _charSeq.reload(CharSeqTrimWS.XMLWHITESPACE_TRIM);
   86           try
   87           {
   88               return XsTypeConverter.lexBoolean(_charSeq);
   89           }
   90           catch(InvalidLexicalValueException e)
   91           {
   92               throw new InvalidLexicalValueException(e, _charSeq.getLocation());
   93           }
   94       }
   95   
   96       public byte getByteValue()
   97           throws XMLStreamException, InvalidLexicalValueException
   98       {
   99           _charSeq.reload(CharSeqTrimWS.XMLWHITESPACE_TRIM);
  100           try
  101           {
  102               return XsTypeConverter.lexByte(_charSeq);
  103           }
  104           catch(NumberFormatException e)
  105           {
  106               throw new InvalidLexicalValueException(e, _charSeq.getLocation());
  107           }
  108       }
  109   
  110       public short getShortValue()
  111           throws XMLStreamException, InvalidLexicalValueException
  112       {
  113           _charSeq.reload(CharSeqTrimWS.XMLWHITESPACE_TRIM);
  114           try
  115           {
  116               return XsTypeConverter.lexShort(_charSeq);
  117           }
  118           catch(NumberFormatException e)
  119           {
  120               throw new InvalidLexicalValueException(e, _charSeq.getLocation());
  121           }
  122       }
  123   
  124       public int getIntValue()
  125           throws XMLStreamException, InvalidLexicalValueException
  126       {
  127           _charSeq.reload(CharSeqTrimWS.XMLWHITESPACE_TRIM);
  128           try
  129           {
  130               return XsTypeConverter.lexInt(_charSeq);
  131           }
  132           catch(NumberFormatException e)
  133           {
  134               throw new InvalidLexicalValueException(e, _charSeq.getLocation());
  135           }
  136       }
  137   
  138       public long getLongValue()
  139           throws XMLStreamException, InvalidLexicalValueException
  140       {
  141           _charSeq.reload(CharSeqTrimWS.XMLWHITESPACE_TRIM);
  142           try
  143           {
  144               return XsTypeConverter.lexLong(_charSeq);
  145           }
  146           catch(NumberFormatException e)
  147           {
  148               throw new InvalidLexicalValueException(e, _charSeq.getLocation());
  149           }
  150       }
  151   
  152       public BigInteger getBigIntegerValue()
  153           throws XMLStreamException, InvalidLexicalValueException
  154       {
  155           _charSeq.reload(CharSeqTrimWS.XMLWHITESPACE_TRIM);
  156           try
  157           {
  158               return XsTypeConverter.lexInteger(_charSeq);
  159           }
  160           catch(NumberFormatException e)
  161           {
  162               throw new InvalidLexicalValueException(e, _charSeq.getLocation());
  163           }
  164       }
  165   
  166       public BigDecimal getBigDecimalValue()
  167           throws XMLStreamException, InvalidLexicalValueException
  168       {
  169           _charSeq.reload(CharSeqTrimWS.XMLWHITESPACE_TRIM);
  170           try
  171           {
  172               return XsTypeConverter.lexDecimal(_charSeq);
  173           }
  174           catch(NumberFormatException e)
  175           {
  176               throw new InvalidLexicalValueException(e, _charSeq.getLocation());
  177           }
  178       }
  179   
  180       public float getFloatValue()
  181           throws XMLStreamException, InvalidLexicalValueException
  182       {
  183           _charSeq.reload(CharSeqTrimWS.XMLWHITESPACE_TRIM);
  184           try
  185           {
  186               return XsTypeConverter.lexFloat(_charSeq);
  187           }
  188           catch(NumberFormatException e)
  189           {
  190               throw new InvalidLexicalValueException(e, _charSeq.getLocation());
  191           }
  192       }
  193   
  194       public double getDoubleValue()
  195           throws XMLStreamException, InvalidLexicalValueException
  196       {
  197           _charSeq.reload(CharSeqTrimWS.XMLWHITESPACE_TRIM);
  198           try
  199           {
  200               return XsTypeConverter.lexDouble(_charSeq);
  201           }
  202           catch(NumberFormatException e)
  203           {
  204               throw new InvalidLexicalValueException(e, _charSeq.getLocation());
  205           }
  206       }
  207   
  208       public InputStream getHexBinaryValue()
  209           throws XMLStreamException, InvalidLexicalValueException
  210       {
  211           _charSeq.reload(CharSeqTrimWS.XMLWHITESPACE_TRIM);
  212           String text = _charSeq.toString();
  213           byte[] buf = HexBin.decode(text.getBytes());
  214           if (buf!=null)
  215               return new ByteArrayInputStream(buf);
  216           else
  217               throw new InvalidLexicalValueException("invalid hexBinary value", _charSeq.getLocation());
  218       }
  219   
  220       public InputStream getBase64Value()
  221           throws XMLStreamException, InvalidLexicalValueException
  222       {
  223           _charSeq.reload(CharSeqTrimWS.XMLWHITESPACE_TRIM);
  224           String text = _charSeq.toString();
  225           byte[] buf = Base64.decode(text.getBytes());
  226           if (buf!=null)
  227               return new ByteArrayInputStream(buf);
  228           else
  229               throw new InvalidLexicalValueException("invalid base64Binary value", _charSeq.getLocation());
  230       }
  231   
  232       public XmlCalendar getCalendarValue()
  233           throws XMLStreamException, InvalidLexicalValueException
  234       {
  235           _charSeq.reload(CharSeqTrimWS.XMLWHITESPACE_TRIM);
  236           try
  237           {
  238               return new GDateBuilder(_charSeq).getCalendar();
  239           }
  240           catch( IllegalArgumentException e)
  241           {
  242               throw new InvalidLexicalValueException(e, _charSeq.getLocation());
  243           }
  244       }
  245   
  246       public Date getDateValue()
  247           throws XMLStreamException, InvalidLexicalValueException
  248       {
  249           _charSeq.reload(CharSeqTrimWS.XMLWHITESPACE_TRIM);
  250           try
  251           {
  252               return new GDateBuilder(_charSeq).getDate();
  253           }
  254           catch(IllegalArgumentException e)
  255           {
  256               throw new InvalidLexicalValueException(e, _charSeq.getLocation());
  257           }
  258       }
  259   
  260       public GDate getGDateValue()
  261           throws XMLStreamException, InvalidLexicalValueException
  262       {
  263           _charSeq.reload(CharSeqTrimWS.XMLWHITESPACE_TRIM);
  264           try
  265           {
  266               return XsTypeConverter.lexGDate(_charSeq);
  267           }
  268           catch(IllegalArgumentException e)
  269           {
  270               throw new InvalidLexicalValueException(e, _charSeq.getLocation());
  271           }
  272       }
  273   
  274       public GDuration getGDurationValue()
  275           throws XMLStreamException, InvalidLexicalValueException
  276       {
  277           _charSeq.reload(CharSeqTrimWS.XMLWHITESPACE_TRIM);
  278           try
  279           {
  280               return new GDuration(_charSeq);
  281           }
  282           catch(IllegalArgumentException e)
  283           {
  284               throw new InvalidLexicalValueException(e, _charSeq.getLocation());
  285           }
  286       }
  287   
  288       public QName getQNameValue()
  289           throws XMLStreamException, InvalidLexicalValueException
  290       {
  291           _charSeq.reload(CharSeqTrimWS.XMLWHITESPACE_TRIM);
  292           try
  293           {
  294               return XsTypeConverter.lexQName(_charSeq, _xmlStream.getNamespaceContext());
  295           }
  296           catch(InvalidLexicalValueException e)
  297           {
  298               throw new InvalidLexicalValueException(e.getMessage(), _charSeq.getLocation());
  299           }
  300       }
  301   
  302       public String getAttributeStringValue(int index) throws XMLStreamException
  303       {
  304           return _xmlStream.getAttributeValue(index);
  305       }
  306   
  307       public String getAttributeStringValue(int index, int wsStyle) throws XMLStreamException
  308       {
  309           return XmlWhitespace.collapse(_xmlStream.getAttributeValue(index), wsStyle);
  310       }
  311   
  312       public boolean getAttributeBooleanValue(int index) throws XMLStreamException
  313       {
  314           try
  315           {
  316               return XsTypeConverter.lexBoolean(_charSeq.reloadAtt(index, CharSeqTrimWS.XMLWHITESPACE_TRIM));
  317           }
  318           catch(InvalidLexicalValueException e)
  319           {
  320               throw new InvalidLexicalValueException(e, _charSeq.getLocation());
  321           }
  322       }
  323   
  324       public byte getAttributeByteValue(int index) throws XMLStreamException
  325       {
  326           try
  327           {
  328               return XsTypeConverter.lexByte(_charSeq.reloadAtt(index, CharSeqTrimWS.XMLWHITESPACE_TRIM));
  329           }
  330           catch(NumberFormatException e)
  331           {
  332               throw new InvalidLexicalValueException(e, _charSeq.getLocation());
  333           }
  334       }
  335   
  336       public short getAttributeShortValue(int index) throws XMLStreamException
  337       {
  338           try
  339           {
  340               return XsTypeConverter.lexShort(_charSeq.reloadAtt(index, CharSeqTrimWS.XMLWHITESPACE_TRIM));
  341           }
  342           catch(NumberFormatException e)
  343           {
  344               throw new InvalidLexicalValueException(e, _charSeq.getLocation());
  345           }
  346       }
  347   
  348       public int getAttributeIntValue(int index) throws XMLStreamException
  349       {
  350           try
  351           {
  352               return XsTypeConverter.lexInt(_charSeq.reloadAtt(index, CharSeqTrimWS.XMLWHITESPACE_TRIM));
  353           }
  354           catch(NumberFormatException e)
  355           {
  356               throw new InvalidLexicalValueException(e, _charSeq.getLocation());
  357           }
  358       }
  359   
  360       public long getAttributeLongValue(int index) throws XMLStreamException
  361       {
  362           try
  363           {
  364               return XsTypeConverter.lexLong(_charSeq.reloadAtt(index, CharSeqTrimWS.XMLWHITESPACE_TRIM));
  365           }
  366           catch(NumberFormatException e)
  367           {
  368               throw new InvalidLexicalValueException(e, _charSeq.getLocation());
  369           }
  370       }
  371   
  372       public BigInteger getAttributeBigIntegerValue(int index) throws XMLStreamException
  373       {
  374           try
  375           {
  376               return XsTypeConverter.lexInteger(_charSeq.reloadAtt(index, CharSeqTrimWS.XMLWHITESPACE_TRIM));
  377           }
  378           catch(NumberFormatException e)
  379           {
  380               throw new InvalidLexicalValueException(e, _charSeq.getLocation());
  381           }
  382       }
  383   
  384       public BigDecimal getAttributeBigDecimalValue(int index) throws XMLStreamException
  385       {
  386           try
  387           {
  388               return XsTypeConverter.lexDecimal(_charSeq.reloadAtt(index, CharSeqTrimWS.XMLWHITESPACE_TRIM));
  389           }
  390           catch(NumberFormatException e)
  391           {
  392               throw new InvalidLexicalValueException(e, _charSeq.getLocation());
  393           }
  394       }
  395   
  396       public float getAttributeFloatValue(int index) throws XMLStreamException
  397       {
  398           try
  399           {
  400               return XsTypeConverter.lexFloat(_charSeq.reloadAtt(index, CharSeqTrimWS.XMLWHITESPACE_TRIM));
  401           }
  402           catch(NumberFormatException e)
  403           {
  404               throw new InvalidLexicalValueException(e, _charSeq.getLocation());
  405           }
  406       }
  407   
  408       public double getAttributeDoubleValue(int index) throws XMLStreamException
  409       {
  410           try
  411           {
  412               return XsTypeConverter.lexDouble(_charSeq.reloadAtt(index, CharSeqTrimWS.XMLWHITESPACE_TRIM));
  413           }
  414           catch(NumberFormatException e)
  415           {
  416               throw new InvalidLexicalValueException(e, _charSeq.getLocation());
  417           }
  418       }
  419   
  420       public InputStream getAttributeHexBinaryValue(int index) throws XMLStreamException
  421       {
  422           String text = _charSeq.reloadAtt(index, CharSeqTrimWS.XMLWHITESPACE_TRIM).toString();
  423           byte[] buf = HexBin.decode(text.getBytes());
  424           if (buf!=null)
  425               return new ByteArrayInputStream(buf);
  426           else
  427               throw new InvalidLexicalValueException("invalid hexBinary value", _charSeq.getLocation());
  428       }
  429   
  430       public InputStream getAttributeBase64Value(int index) throws XMLStreamException
  431       {
  432           String text = _charSeq.reloadAtt(index, CharSeqTrimWS.XMLWHITESPACE_TRIM).toString();
  433           byte[] buf = Base64.decode(text.getBytes());
  434           if (buf!=null)
  435               return new ByteArrayInputStream(buf);
  436           else
  437               throw new InvalidLexicalValueException("invalid base64Binary value", _charSeq.getLocation());
  438       }
  439   
  440       public XmlCalendar getAttributeCalendarValue(int index) throws XMLStreamException
  441       {
  442           try
  443           {
  444               return new GDateBuilder(_charSeq.reloadAtt(index, CharSeqTrimWS.XMLWHITESPACE_TRIM)).
  445                   getCalendar();
  446           }
  447           catch(IllegalArgumentException e)
  448           {
  449               throw new InvalidLexicalValueException(e, _charSeq.getLocation());
  450           }
  451       }
  452   
  453       public Date getAttributeDateValue(int index) throws XMLStreamException
  454       {
  455           try
  456           {
  457               return new GDateBuilder(_charSeq.reloadAtt(index, CharSeqTrimWS.XMLWHITESPACE_TRIM))
  458                   .getDate();
  459           }
  460           catch(IllegalArgumentException e)
  461           {
  462               throw new InvalidLexicalValueException(e, _charSeq.getLocation());
  463           }
  464       }
  465   
  466       public GDate getAttributeGDateValue(int index) throws XMLStreamException
  467       {
  468           try
  469           {
  470               return new GDate(_charSeq.reloadAtt(index, CharSeqTrimWS.XMLWHITESPACE_TRIM));
  471           }
  472           catch(IllegalArgumentException e)
  473           {
  474               throw new InvalidLexicalValueException(e, _charSeq.getLocation());
  475           }
  476       }
  477   
  478       public GDuration getAttributeGDurationValue(int index) throws XMLStreamException
  479       {
  480           try
  481           {
  482               return new GDuration(_charSeq.reloadAtt(index, CharSeqTrimWS.XMLWHITESPACE_TRIM));
  483           }
  484           catch(IllegalArgumentException e)
  485           {
  486               throw new InvalidLexicalValueException(e, _charSeq.getLocation());
  487           }
  488       }
  489   
  490       public QName getAttributeQNameValue(int index) throws XMLStreamException
  491       {
  492           try
  493           {
  494               return XsTypeConverter.lexQName(_charSeq.reloadAtt(index, CharSeqTrimWS.XMLWHITESPACE_TRIM),
  495                   _xmlStream.getNamespaceContext());
  496           }
  497           catch(InvalidLexicalValueException e)
  498           {
  499               throw new InvalidLexicalValueException(e.getMessage(), _charSeq.getLocation());
  500           }
  501       }
  502   
  503       public String getAttributeStringValue(String uri, String local) throws XMLStreamException
  504       {
  505           return _charSeq.reloadAtt(uri, local, CharSeqTrimWS.XMLWHITESPACE_PRESERVE).toString();
  506       }
  507   
  508       public String getAttributeStringValue(String uri, String local, int wsStyle) throws XMLStreamException
  509       {
  510           return XmlWhitespace.collapse(_xmlStream.getAttributeValue(uri, local), wsStyle);
  511       }
  512   
  513       public boolean getAttributeBooleanValue(String uri, String local) throws XMLStreamException
  514       {
  515           CharSequence cs = _charSeq.reloadAtt(uri, local, CharSeqTrimWS.XMLWHITESPACE_TRIM);
  516           try
  517           {
  518               return XsTypeConverter.lexBoolean(cs);
  519           }
  520           catch(InvalidLexicalValueException e)
  521           {
  522               throw new InvalidLexicalValueException(e, _charSeq.getLocation());
  523           }
  524       }
  525   
  526       public byte getAttributeByteValue(String uri, String local) throws XMLStreamException
  527       {
  528           CharSequence cs = _charSeq.reloadAtt(uri, local, CharSeqTrimWS.XMLWHITESPACE_TRIM);
  529           try
  530           {
  531               return XsTypeConverter.lexByte(cs);
  532           }
  533           catch(NumberFormatException e)
  534           {
  535               throw new InvalidLexicalValueException(e, _charSeq.getLocation());
  536           }
  537       }
  538   
  539       public short getAttributeShortValue(String uri, String local) throws XMLStreamException
  540       {
  541           CharSequence cs = _charSeq.reloadAtt(uri, local, CharSeqTrimWS.XMLWHITESPACE_TRIM);
  542           try
  543           {
  544               return XsTypeConverter.lexShort(cs);
  545           }
  546           catch(NumberFormatException e)
  547           {
  548               throw new InvalidLexicalValueException(e, _charSeq.getLocation());
  549           }
  550       }
  551   
  552       public int getAttributeIntValue(String uri, String local) throws XMLStreamException
  553       {
  554           CharSequence cs = _charSeq.reloadAtt(uri, local, CharSeqTrimWS.XMLWHITESPACE_TRIM);
  555           try
  556           {
  557               return XsTypeConverter.lexInt(cs);
  558           }
  559           catch(NumberFormatException e)
  560           {
  561               throw new InvalidLexicalValueException(e, _charSeq.getLocation());
  562           }
  563       }
  564   
  565       public long getAttributeLongValue(String uri, String local) throws XMLStreamException
  566       {
  567           CharSequence cs = _charSeq.reloadAtt(uri, local, CharSeqTrimWS.XMLWHITESPACE_TRIM);
  568           try
  569           {
  570               return XsTypeConverter.lexLong(cs);
  571           }
  572           catch(NumberFormatException e)
  573           {
  574               throw new InvalidLexicalValueException(e, _charSeq.getLocation());
  575           }
  576       }
  577   
  578       public BigInteger getAttributeBigIntegerValue(String uri, String local) throws XMLStreamException
  579       {
  580           CharSequence cs = _charSeq.reloadAtt(uri, local, CharSeqTrimWS.XMLWHITESPACE_TRIM);
  581           try
  582           {
  583               return XsTypeConverter.lexInteger(cs);
  584           }
  585           catch(NumberFormatException e)
  586           {
  587               throw new InvalidLexicalValueException(e, _charSeq.getLocation());
  588           }
  589       }
  590   
  591       public BigDecimal getAttributeBigDecimalValue(String uri, String local) throws XMLStreamException
  592       {
  593           CharSequence cs = _charSeq.reloadAtt(uri, local, CharSeqTrimWS.XMLWHITESPACE_TRIM);
  594           try
  595           {
  596               return XsTypeConverter.lexDecimal(cs);
  597           }
  598           catch(NumberFormatException e)
  599           {
  600               throw new InvalidLexicalValueException(e, _charSeq.getLocation());
  601           }
  602       }
  603   
  604       public float getAttributeFloatValue(String uri, String local) throws XMLStreamException
  605       {
  606           CharSequence cs = _charSeq.reloadAtt(uri, local, CharSeqTrimWS.XMLWHITESPACE_TRIM);
  607           try
  608           {
  609               return XsTypeConverter.lexFloat(cs);
  610           }
  611           catch(NumberFormatException e)
  612           {
  613               throw new InvalidLexicalValueException(e, _charSeq.getLocation());
  614           }
  615       }
  616   
  617       public double getAttributeDoubleValue(String uri, String local) throws XMLStreamException
  618       {
  619           CharSequence cs = _charSeq.reloadAtt(uri, local, CharSeqTrimWS.XMLWHITESPACE_TRIM);
  620           try
  621           {
  622               return XsTypeConverter.lexDouble(cs);
  623           }
  624           catch(NumberFormatException e)
  625           {
  626               throw new InvalidLexicalValueException(e, _charSeq.getLocation());
  627           }
  628       }
  629   
  630       public InputStream getAttributeHexBinaryValue(String uri, String local) throws XMLStreamException
  631       {
  632           CharSequence cs = _charSeq.reloadAtt(uri, local, CharSeqTrimWS.XMLWHITESPACE_TRIM);
  633           String text = cs.toString();
  634           byte[] buf = HexBin.decode(text.getBytes());
  635           if (buf!=null)
  636               return new ByteArrayInputStream(buf);
  637           else
  638               throw new InvalidLexicalValueException("invalid hexBinary value", _charSeq.getLocation());
  639       }
  640   
  641       public InputStream getAttributeBase64Value(String uri, String local) throws XMLStreamException
  642       {
  643           CharSequence cs = _charSeq.reloadAtt(uri, local, CharSeqTrimWS.XMLWHITESPACE_TRIM);
  644           String text = cs.toString();
  645           byte[] buf = Base64.decode(text.getBytes());
  646           if (buf!=null)
  647               return new ByteArrayInputStream(buf);
  648           else
  649               throw new InvalidLexicalValueException("invalid base64Binary value", _charSeq.getLocation());
  650       }
  651   
  652       public XmlCalendar getAttributeCalendarValue(String uri, String local) throws XMLStreamException
  653       {
  654           CharSequence cs = _charSeq.reloadAtt(uri, local, CharSeqTrimWS.XMLWHITESPACE_TRIM);
  655           try
  656           {
  657               return new GDateBuilder(cs).getCalendar();
  658           }
  659           catch(IllegalArgumentException e)
  660           {
  661               throw new InvalidLexicalValueException(e, _charSeq.getLocation());
  662           }
  663       }
  664   
  665       public Date getAttributeDateValue(String uri, String local) throws XMLStreamException
  666       {
  667           try
  668           {
  669               CharSequence cs = _charSeq.reloadAtt(uri, local, CharSeqTrimWS.XMLWHITESPACE_TRIM);
  670               return new GDateBuilder(cs).getDate();
  671           }
  672           catch(IllegalArgumentException e)
  673           {
  674               throw new InvalidLexicalValueException(e, _charSeq.getLocation());
  675           }
  676       }
  677   
  678       public GDate getAttributeGDateValue(String uri, String local) throws XMLStreamException
  679       {
  680           try
  681           {
  682               CharSequence cs = _charSeq.reloadAtt(uri, local, CharSeqTrimWS.XMLWHITESPACE_TRIM);
  683               return new GDate(cs);
  684           }
  685           catch(IllegalArgumentException e)
  686           {
  687               throw new InvalidLexicalValueException(e, _charSeq.getLocation());
  688           }
  689       }
  690   
  691       public GDuration getAttributeGDurationValue(String uri, String local) throws XMLStreamException
  692       {
  693           try
  694           {
  695               return new GDuration(_charSeq.reloadAtt(uri, local, CharSeqTrimWS.XMLWHITESPACE_TRIM));
  696           }
  697           catch(IllegalArgumentException e)
  698           {
  699               throw new InvalidLexicalValueException(e, _charSeq.getLocation());
  700           }
  701       }
  702   
  703       public QName getAttributeQNameValue(String uri, String local) throws XMLStreamException
  704       {
  705           CharSequence cs = _charSeq.reloadAtt(uri, local, CharSeqTrimWS.XMLWHITESPACE_TRIM);
  706           try
  707           {
  708               return XsTypeConverter.lexQName(cs, _xmlStream.getNamespaceContext());
  709           }
  710           catch (InvalidLexicalValueException e)
  711           {
  712               throw new InvalidLexicalValueException(e.getMessage(), _charSeq.getLocation());
  713           }
  714       }
  715   
  716       public void setDefaultValue(String defaultValue) throws XMLStreamException
  717       {
  718           _defaultValue = defaultValue;
  719       }
  720   
  721       /**
  722        * Only trims the XML whaitspace at edges, it should not be used for WS collapse
  723        * Used for int, short, byte
  724        */
  725       private static class CharSeqTrimWS
  726           implements CharSequence
  727       {
  728           final static int XMLWHITESPACE_PRESERVE = 1;
  729           final static int XMLWHITESPACE_TRIM = 2;
  730   
  731           private static int INITIAL_SIZE = 100;
  732           private char[] _buf = new char[INITIAL_SIZE];
  733           private int _start, _length = 0;
  734           private int _nonWSStart = 0;
  735           private int _nonWSEnd = 0;
  736           private String _toStringValue;
  737           private XMLStreamReaderExtImpl _xmlSteam;
  738           //private boolean _supportForGetTextCharacters = true;
  739           private final ExtLocation _location;
  740           private boolean _hasText;
  741   
  742           CharSeqTrimWS(XMLStreamReaderExtImpl xmlSteam)
  743           {
  744               _xmlSteam = xmlSteam;
  745               _location = new ExtLocation();
  746           }
  747   
  748           void reload(int style)
  749               throws XMLStreamException
  750           {
  751               _toStringValue = null;
  752               _location.reset();
  753               _hasText = false;
  754   
  755               fillBuffer();
  756   
  757               if (style==XMLWHITESPACE_PRESERVE)
  758               {
  759                   _nonWSStart = 0;
  760                   _nonWSEnd = _length;
  761   
  762                   // takes defaultValue only if there wasn't any text
  763                   if (!_hasText && _xmlSteam._defaultValue!=null)
  764                   {
  765                       _length = 0;
  766                       fillBufferFromString(_xmlSteam._defaultValue);
  767                   }
  768               }
  769               else if (style==XMLWHITESPACE_TRIM)
  770               {
  771                   for (_nonWSStart=0; _nonWSStart<_length; _nonWSStart++)
  772                       if (!XMLChar.isSpace(_buf[_nonWSStart]))
  773                           break;
  774                   for (_nonWSEnd=_length; _nonWSEnd>_nonWSStart; _nonWSEnd--)
  775                       if (!XMLChar.isSpace(_buf[_nonWSEnd-1]))
  776                           break;
  777   
  778                   // takes defaultValue if length after triming is 0
  779                   if (length()==0 && _xmlSteam._defaultValue!=null)
  780                   {
  781                       _length = 0;
  782                       fillBufferFromString(_xmlSteam._defaultValue);
  783   
  784                       //apply whispace rule on the default value
  785                       for (_nonWSStart=0; _nonWSStart<_length; _nonWSStart++)
  786                           if (!XMLChar.isSpace(_buf[_nonWSStart]))
  787                               break;
  788                       for (_nonWSEnd=_length; _nonWSEnd>_nonWSStart; _nonWSEnd--)
  789                           if (!XMLChar.isSpace(_buf[_nonWSEnd-1]))
  790                               break;
  791                   }
  792               }
  793               _xmlSteam._defaultValue = null;
  794           }
  795   
  796           private void fillBuffer()
  797               throws XMLStreamException
  798           {
  799               _length = 0;
  800   
  801               if (_xmlSteam.getEventType() == XMLStreamReader.START_DOCUMENT)
  802                   _xmlSteam.next();
  803               if (_xmlSteam.isStartElement())
  804                   _xmlSteam.next();
  805   
  806               int depth = 0;
  807               String error = null;
  808               int eventType = _xmlSteam.getEventType();
  809   
  810               loop:
  811               while(true)
  812               {
  813                   switch(eventType)
  814                   {
  815                   case XMLStreamReader.CDATA:
  816                   case XMLStreamReader.CHARACTERS:
  817                   case XMLStreamReader.SPACE:
  818                       _location.set(_xmlSteam.getLocation());
  819   
  820                       if (depth==0)
  821                           addTextToBuffer();
  822   
  823                       break;
  824   
  825                   case XMLStreamReader.ATTRIBUTE:
  826                   case XMLStreamReader.COMMENT:
  827                   case XMLStreamReader.DTD:
  828                   case XMLStreamReader.ENTITY_DECLARATION:
  829                   case XMLStreamReader.NAMESPACE:
  830                   case XMLStreamReader.NOTATION_DECLARATION:
  831                   case XMLStreamReader.PROCESSING_INSTRUCTION:
  832                   case XMLStreamReader.START_DOCUMENT:
  833                       // ignore
  834                       break;
  835   
  836                   case XMLStreamReader.END_DOCUMENT:
  837                       _location.set(_xmlSteam.getLocation());
  838   
  839                       break loop;
  840   
  841                   case XMLStreamReader.END_ELEMENT:
  842                       _location.set(_xmlSteam.getLocation());
  843                       depth--;
  844                       if (depth<0)
  845                           break loop;
  846                       break;
  847   
  848                   case XMLStreamReader.ENTITY_REFERENCE:
  849                       _location.set(_xmlSteam.getLocation());
  850   
  851                       addEntityToBuffer();
  852                       break;
  853   
  854                   case XMLStreamReader.START_ELEMENT:
  855                       depth++;
  856                       error = "Unexpected element '" + _xmlSteam.getName() + "' in text content.";
  857                       _location.set(_xmlSteam.getLocation());
  858   
  859                       break;
  860                   }
  861                   eventType = _xmlSteam.next();
  862               }
  863               if (error!=null)
  864                   throw new XMLStreamException(error);
  865           }
  866   
  867           private void ensureBufferLength(int lengthToAdd)
  868           {
  869               if (_length + lengthToAdd>_buf.length)
  870               {
  871                   char[] newBuf = new char[_length + lengthToAdd];
  872                   if (_length>0)
  873                       System.arraycopy(_buf, 0, newBuf, 0, _length);
  874                   _buf = newBuf;
  875               }
  876           }
  877   
  878           private void fillBufferFromString(CharSequence value)
  879           {
  880               int textLength = value.length();
  881               ensureBufferLength(textLength);
  882   
  883               for (int i=0; i<textLength; i++)
  884               {
  885                   _buf[i] = value.charAt(i);
  886               }
  887               _length = textLength;
  888           }
  889   
  890           private void addTextToBuffer()
  891           {
  892               _hasText = true;
  893               int textLength = _xmlSteam.getTextLength();
  894               ensureBufferLength(textLength);
  895   
  896               /*
  897               Commented out as part of the receipt of the more up to date
  898               jsr173_1.0_ri.jar. getTextCharacters(int, char[], int, int)
  899               used to throw UnsupportedOperationException always. Now it no longer
  900               does, but getTextCharacters(int, char[], int, int) does not return what
  901               we expect. So reverting to always calling getTextCharacters() until
  902               we can work out whether it's us that's wrong or them.
  903   
  904               if (_supportForGetTextCharacters)
  905                   try
  906                   {
  907                       _length = _xmlSteam.getTextCharacters(0, _buf, _length, textLength);
  908                   }
  909                   catch(Exception e)
  910                   {
  911                       _supportForGetTextCharacters = false;
  912                   }
  913               */
  914   
  915               // if(!_supportForGetTextCharacters)
  916               //{
  917               System.arraycopy(_xmlSteam.getTextCharacters(), _xmlSteam.getTextStart(), _buf, _length, textLength);
  918               _length = _length + textLength;
  919               //}
  920           }
  921   
  922           private void addEntityToBuffer()
  923           {
  924               String text = _xmlSteam.getText();
  925   
  926               int textLength = text.length();
  927               ensureBufferLength(textLength);
  928   
  929               text.getChars(0, text.length(), _buf, _length);
  930               _length = _length + text.length();
  931           }
  932   
  933           CharSequence reloadAtt(int index, int style)
  934               throws XMLStreamException
  935           {
  936               _location.reset();
  937               _location.set(_xmlSteam.getLocation());
  938               String value = _xmlSteam.getAttributeValue(index);
  939   
  940               if (value==null && _xmlSteam._defaultValue!=null)
  941                   value = _xmlSteam._defaultValue;
  942   
  943               _xmlSteam._defaultValue = null;
  944   
  945               int length = value.length();
  946   
  947               if (style==XMLWHITESPACE_PRESERVE)
  948               {
  949                   return value;
  950               }
  951               else if (style==XMLWHITESPACE_TRIM)
  952               {
  953                   int nonWSStart, nonWSEnd;
  954                   for (nonWSStart=0; nonWSStart<length; nonWSStart++)
  955                       if (!XMLChar.isSpace(value.charAt(nonWSStart)))
  956                           break;
  957                   for (nonWSEnd=length; nonWSEnd>nonWSStart; nonWSEnd--)
  958                       if (!XMLChar.isSpace(value.charAt(nonWSEnd-1)))
  959                           break;
  960                   if (nonWSStart==0 && nonWSEnd==length)
  961                       return value;
  962                   else
  963                       return value.subSequence(nonWSStart, nonWSEnd);
  964               }
  965   
  966               throw new IllegalStateException("unknown style");
  967           }
  968   
  969           CharSequence reloadAtt(String uri, String local, int style)
  970               throws XMLStreamException
  971           {
  972               _location.reset();
  973               _location.set(_xmlSteam.getLocation());
  974               String value = _xmlSteam.getAttributeValue(uri, local);
  975   
  976               if (value==null && _xmlSteam._defaultValue!=null)
  977                   value = _xmlSteam._defaultValue;
  978   
  979               _xmlSteam._defaultValue = null;
  980   
  981               int length = value.length();
  982   
  983               if (style==XMLWHITESPACE_PRESERVE)
  984               {
  985                   return value;
  986               }
  987               else if (style==XMLWHITESPACE_TRIM)
  988               {
  989                   for (_nonWSStart=0; _nonWSStart<length; _nonWSStart++)
  990                       if (!XMLChar.isSpace(value.charAt(_nonWSStart)))
  991                           break;
  992                   for (_nonWSEnd=length; _nonWSEnd>_nonWSStart; _nonWSEnd--)
  993                       if (!XMLChar.isSpace(value.charAt(_nonWSEnd-1)))
  994                           break;
  995                   if (_nonWSStart==0 && _nonWSEnd==length)
  996                       return value;
  997                   else
  998                       return value.subSequence(_nonWSStart, _nonWSEnd);
  999               }
 1000               throw new IllegalStateException("unknown style");
 1001           }
 1002   
 1003           Location getLocation()
 1004           {
 1005               ExtLocation loc = new ExtLocation();
 1006               loc.set(_location);
 1007               return loc;
 1008           }
 1009   
 1010           public int length()
 1011           {
 1012               return _nonWSEnd - _nonWSStart;
 1013           }
 1014   
 1015           public char charAt(int index)
 1016           {
 1017               // for each char, this has to be fast, using assert instead of if throw
 1018               assert (index<_nonWSEnd-_nonWSStart && -1<index) :
 1019                   "Index " + index + " must be >-1 and <" + (_nonWSEnd - _nonWSStart);
 1020   
 1021               return _buf[_nonWSStart + index];
 1022           }
 1023   
 1024           public CharSequence subSequence(int start, int end)
 1025           {
 1026               return new String(_buf, _nonWSStart + start, end - start);
 1027           }
 1028   
 1029           public String toString()
 1030           {
 1031               if (_toStringValue!=null)
 1032                   return _toStringValue;
 1033   
 1034               _toStringValue = new String(_buf, _nonWSStart, _nonWSEnd - _nonWSStart);
 1035               return _toStringValue;
 1036           }
 1037   
 1038           private static class ExtLocation implements Location
 1039           {
 1040               private int _line;
 1041               private int _col;
 1042               private int _off;
 1043               private String _pid;
 1044               private String _sid;
 1045               private boolean _isSet;
 1046   
 1047               ExtLocation()
 1048               {
 1049                   _isSet = false;
 1050               }
 1051   
 1052               public int getLineNumber()
 1053               {
 1054                   if (_isSet)
 1055                       return _line;
 1056                   else
 1057                       throw new IllegalStateException();
 1058               }
 1059   
 1060               public int getColumnNumber()
 1061               {
 1062                   if (_isSet)
 1063                       return _col;
 1064                   else
 1065                       throw new IllegalStateException();
 1066               }
 1067   
 1068               public int getCharacterOffset()
 1069               {
 1070                   if (_isSet)
 1071                       return _off;
 1072                   else
 1073                       throw new IllegalStateException();
 1074               }
 1075   
 1076               public String getPublicId()
 1077               {
 1078                   if (_isSet)
 1079                       return _pid;
 1080                   else
 1081                       throw new IllegalStateException();
 1082               }
 1083   
 1084               public String getSystemId()
 1085               {
 1086                   if (_isSet)
 1087                       return _sid;
 1088                   else
 1089                       throw new IllegalStateException();
 1090               }
 1091   
 1092               void set(Location loc)
 1093               {
 1094                   if (_isSet)
 1095                       return;
 1096   
 1097                   _isSet = true;
 1098                   _line = loc.getLineNumber();
 1099                   _col = loc.getColumnNumber();
 1100                   _off = loc.getCharacterOffset();
 1101                   _pid = loc.getPublicId();
 1102                   _sid = loc.getSystemId();
 1103               }
 1104   
 1105               void reset()
 1106               {
 1107                   _isSet = false;
 1108               }
 1109           }
 1110       }
 1111   
 1112       // XMLStreamReader methods
 1113       public Object getProperty(String s)
 1114           throws IllegalArgumentException
 1115       {
 1116           return _xmlStream.getProperty(s);
 1117       }
 1118   
 1119       public int next()
 1120           throws XMLStreamException
 1121       {
 1122           return _xmlStream.next();
 1123       }
 1124   
 1125       public void require(int i, String s, String s1)
 1126           throws XMLStreamException
 1127       {
 1128           _xmlStream.require(i, s, s1);
 1129       }
 1130   
 1131       public String getElementText() throws XMLStreamException
 1132       {
 1133           return _xmlStream.getElementText();
 1134       }
 1135   
 1136       public int nextTag() throws XMLStreamException
 1137       {
 1138           return _xmlStream.nextTag();
 1139       }
 1140   
 1141       public boolean hasNext() throws XMLStreamException
 1142       {
 1143           return _xmlStream.hasNext();
 1144       }
 1145   
 1146       public void close() throws XMLStreamException
 1147       {
 1148           _xmlStream.close();
 1149       }
 1150   
 1151       public String getNamespaceURI(String s)
 1152       {
 1153           return _xmlStream.getNamespaceURI(s);
 1154       }
 1155   
 1156       public boolean isStartElement()
 1157       {
 1158           return _xmlStream.isStartElement();
 1159       }
 1160   
 1161       public boolean isEndElement()
 1162       {
 1163           return _xmlStream.isEndElement();
 1164       }
 1165   
 1166       public boolean isCharacters()
 1167       {
 1168           return _xmlStream.isCharacters();
 1169       }
 1170   
 1171       public boolean isWhiteSpace()
 1172       {
 1173           return _xmlStream.isWhiteSpace();
 1174       }
 1175   
 1176       public String getAttributeValue(String s, String s1)
 1177       {
 1178           return _xmlStream.getAttributeValue(s, s1);
 1179       }
 1180   
 1181       public int getAttributeCount()
 1182       {
 1183           return _xmlStream.getAttributeCount();
 1184       }
 1185   
 1186       public QName getAttributeName(int i)
 1187       {
 1188           return _xmlStream.getAttributeName(i);
 1189       }
 1190   
 1191       public String getAttributeNamespace(int i)
 1192       {
 1193           return _xmlStream.getAttributeNamespace(i);
 1194       }
 1195   
 1196       public String getAttributeLocalName(int i)
 1197       {
 1198           return _xmlStream.getAttributeLocalName(i);
 1199       }
 1200   
 1201       public String getAttributePrefix(int i)
 1202       {
 1203           return _xmlStream.getAttributePrefix(i);
 1204       }
 1205   
 1206       public String getAttributeType(int i)
 1207       {
 1208           return _xmlStream.getAttributeType(i);
 1209       }
 1210   
 1211       public String getAttributeValue(int i)
 1212       {
 1213           return _xmlStream.getAttributeValue(i);
 1214       }
 1215   
 1216       public boolean isAttributeSpecified(int i)
 1217       {
 1218           return _xmlStream.isAttributeSpecified(i);
 1219       }
 1220   
 1221       public int getNamespaceCount()
 1222       {
 1223           return _xmlStream.getNamespaceCount();
 1224       }
 1225   
 1226       public String getNamespacePrefix(int i)
 1227       {
 1228           return _xmlStream.getNamespacePrefix(i);
 1229       }
 1230   
 1231       public String getNamespaceURI(int i)
 1232       {
 1233           return _xmlStream.getNamespaceURI(i);
 1234       }
 1235   
 1236       public NamespaceContext getNamespaceContext()
 1237       {
 1238           return _xmlStream.getNamespaceContext();
 1239       }
 1240   
 1241       public int getEventType()
 1242       {
 1243           return _xmlStream.getEventType();
 1244       }
 1245   
 1246       public String getText()
 1247       {
 1248           return _xmlStream.getText();
 1249       }
 1250   
 1251       public char[] getTextCharacters()
 1252       {
 1253           return _xmlStream.getTextCharacters();
 1254       }
 1255   
 1256       public int getTextCharacters(int i, char[] chars, int i1, int i2)
 1257           throws XMLStreamException
 1258       {
 1259           return _xmlStream.getTextCharacters(i, chars, i1, i2);
 1260       }
 1261   
 1262       public int getTextStart()
 1263       {
 1264           return _xmlStream.getTextStart();
 1265       }
 1266   
 1267       public int getTextLength()
 1268       {
 1269           return _xmlStream.getTextLength();
 1270       }
 1271   
 1272       public String getEncoding()
 1273       {
 1274           return _xmlStream.getEncoding();
 1275       }
 1276   
 1277       public boolean hasText()
 1278       {
 1279           return _xmlStream.hasText();
 1280       }
 1281   
 1282       public Location getLocation()
 1283       {
 1284           return _xmlStream.getLocation();
 1285       }
 1286   
 1287       public QName getName()
 1288       {
 1289           return _xmlStream.getName();
 1290       }
 1291   
 1292       public String getLocalName()
 1293       {
 1294           return _xmlStream.getLocalName();
 1295       }
 1296   
 1297       public boolean hasName()
 1298       {
 1299           return _xmlStream.hasName();
 1300       }
 1301   
 1302       public String getNamespaceURI()
 1303       {
 1304           return _xmlStream.getNamespaceURI();
 1305       }
 1306   
 1307       public String getPrefix()
 1308       {
 1309           return _xmlStream.getPrefix();
 1310       }
 1311   
 1312       public String getVersion()
 1313       {
 1314           return _xmlStream.getVersion();
 1315       }
 1316   
 1317       public boolean isStandalone()
 1318       {
 1319           return _xmlStream.isStandalone();
 1320       }
 1321   
 1322       public boolean standaloneSet()
 1323       {
 1324           return _xmlStream.standaloneSet();
 1325       }
 1326   
 1327       public String getCharacterEncodingScheme()
 1328       {
 1329           return _xmlStream.getCharacterEncodingScheme();
 1330       }
 1331   
 1332       public String getPITarget()
 1333       {
 1334           return _xmlStream.getPITarget();
 1335       }
 1336   
 1337       public String getPIData()
 1338       {
 1339           return _xmlStream.getPIData();
 1340       }
 1341   }

Home » xmlbeans-2.5.0-src » org.apache.xmlbeans.impl » richParser » [javadoc | source]