src/share/jaxws_classes/com/sun/xml/internal/stream/buffer/sax/SAXBufferProcessor.java

Print this page


   1 /*
   2  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


  71     /**
  72      * Reference to error handler.
  73      */
  74     protected ErrorHandler _errorHandler = DEFAULT_LEXICAL_HANDLER;
  75 
  76     /**
  77      * Reference to lexical handler.
  78      */
  79     protected LexicalHandler _lexicalHandler = DEFAULT_LEXICAL_HANDLER;
  80 
  81     /**
  82      * SAX Namespace attributes features
  83      */
  84     protected boolean _namespacePrefixesFeature = false;
  85 
  86     protected AttributesHolder _attributes = new AttributesHolder();
  87 
  88     protected String[] _namespacePrefixes = new String[16];
  89     protected int _namespacePrefixesIndex;
  90 

  91     protected int[] _namespaceAttributesStack = new int[16];
  92     protected int _namespaceAttributesStackIndex;
  93 
  94     public SAXBufferProcessor() {
  95     }
  96 
  97     /**
  98      * @deprecated
  99      *      Use {@link #SAXBufferProcessor(XMLStreamBuffer, boolean)}
 100      */
 101     public SAXBufferProcessor(XMLStreamBuffer buffer) {
 102         setXMLStreamBuffer(buffer);
 103     }
 104 
 105     /**
 106      * @param produceFragmentEvent
 107      *      True to generate fragment SAX events without start/endDocument.
 108      *      False to generate a full document SAX events.
 109      */
 110     public SAXBufferProcessor(XMLStreamBuffer buffer, boolean produceFragmentEvent) {


 419                 case STATE_COMMENT_AS_STRING:
 420                     processComment(readContentString());
 421                     break;
 422                 case STATE_PROCESSING_INSTRUCTION:
 423                     processProcessingInstruction(readStructureString(), readStructureString());
 424                     break;
 425                 case STATE_END:
 426                     return;
 427                 default:
 428                     throw reportFatalError("Illegal state for child of DII: "+item);
 429             }
 430         }
 431     }
 432 
 433     protected void processElement(String uri, String localName, String qName, boolean inscope) throws SAXException {
 434         boolean hasAttributes = false;
 435         boolean hasNamespaceAttributes = false;
 436         int item = peekStructure();
 437         Set<String> prefixSet = inscope ? new HashSet<String>() : Collections.<String>emptySet();
 438         if ((item & TYPE_MASK) == T_NAMESPACE_ATTRIBUTE) {

 439             hasNamespaceAttributes = true;
 440             item = processNamespaceAttributes(item, inscope, prefixSet);
 441         }
 442         if (inscope) {
 443             readInscopeNamespaces(prefixSet);
 444         }
 445 
 446         if ((item & TYPE_MASK) == T_ATTRIBUTE) {
 447             hasAttributes = true;
 448             processAttributes(item);
 449         }
 450 
 451         _contentHandler.startElement(uri, localName, qName, _attributes);
 452 
 453         if (hasAttributes) {
 454             _attributes.clear();
 455         }
 456 
 457         do {
 458             item = readEiiState();


 553 
 554     }
 555 
 556      private static String fixNull(String s) {
 557         if (s == null) return "";
 558         else return s;
 559     }
 560     private void processCommentAsCharArrayCopy() throws SAXException {
 561         final char[] ch = readContentCharactersCopy();
 562         processComment(ch, 0, ch.length);
 563     }
 564 
 565     private void processCommentAsCharArrayMedium() throws SAXException {
 566         final int length = readStructure16();
 567         final int start = readContentCharactersBuffer(length);
 568         processComment(_contentCharactersBuffer, start, length);
 569     }
 570 
 571     private void processEndPrefixMapping() throws SAXException {
 572         final int end = _namespaceAttributesStack[--_namespaceAttributesStackIndex];
 573         final int start = (_namespaceAttributesStackIndex > 0) ? _namespaceAttributesStack[_namespaceAttributesStackIndex] : 0;

 574 
 575         for (int i = end - 1; i >= start; i--) {
 576             _contentHandler.endPrefixMapping(_namespacePrefixes[i]);
 577         }
 578         _namespacePrefixesIndex = start;
 579     }
 580 
 581     private int processNamespaceAttributes(int item,boolean collectPrefixes, Set<String> prefixSet) throws SAXException {
 582         do {
 583             String prefix;
 584             switch(getNIIState(item)) {
 585                 case STATE_NAMESPACE_ATTRIBUTE:
 586                     // Undeclaration of default namespace
 587                     processNamespaceAttribute("", "");
 588                     if(collectPrefixes) {
 589                         prefixSet.add("");
 590                     }
 591                     break;
 592                 case STATE_NAMESPACE_ATTRIBUTE_P:
 593                     // Undeclaration of namespace


 680     }
 681 
 682     private void cacheNamespacePrefix(String prefix) {
 683         if (_namespacePrefixesIndex == _namespacePrefixes.length) {
 684             final String[] namespaceAttributes = new String[_namespacePrefixesIndex * 3 / 2 + 1];
 685             System.arraycopy(_namespacePrefixes, 0, namespaceAttributes, 0, _namespacePrefixesIndex);
 686             _namespacePrefixes = namespaceAttributes;
 687         }
 688 
 689         _namespacePrefixes[_namespacePrefixesIndex++] = prefix;
 690     }
 691 
 692     private void cacheNamespacePrefixIndex() {
 693         if (_namespaceAttributesStackIndex == _namespaceAttributesStack.length) {
 694             final int[] namespaceAttributesStack = new int[_namespaceAttributesStackIndex * 3 /2 + 1];
 695             System.arraycopy(_namespaceAttributesStack, 0, namespaceAttributesStack, 0, _namespaceAttributesStackIndex);
 696             _namespaceAttributesStack = namespaceAttributesStack;
 697         }
 698 
 699         _namespaceAttributesStack[_namespaceAttributesStackIndex++] = _namespacePrefixesIndex;









 700     }
 701 
 702     private void processComment(String s)  throws SAXException {
 703         processComment(s.toCharArray(), 0, s.length());
 704     }
 705 
 706     private void processComment(char[] ch, int start, int length) throws SAXException {
 707         _lexicalHandler.comment(ch, start, length);
 708     }
 709 
 710     private void processProcessingInstruction(String target, String data) throws SAXException {
 711         _contentHandler.processingInstruction(target, data);
 712     }
 713 
 714     private static final DefaultWithLexicalHandler DEFAULT_LEXICAL_HANDLER = new DefaultWithLexicalHandler();
 715 }
   1 /*
   2  * Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


  71     /**
  72      * Reference to error handler.
  73      */
  74     protected ErrorHandler _errorHandler = DEFAULT_LEXICAL_HANDLER;
  75 
  76     /**
  77      * Reference to lexical handler.
  78      */
  79     protected LexicalHandler _lexicalHandler = DEFAULT_LEXICAL_HANDLER;
  80 
  81     /**
  82      * SAX Namespace attributes features
  83      */
  84     protected boolean _namespacePrefixesFeature = false;
  85 
  86     protected AttributesHolder _attributes = new AttributesHolder();
  87 
  88     protected String[] _namespacePrefixes = new String[16];
  89     protected int _namespacePrefixesIndex;
  90 
  91     protected int[] _namespaceAttributesStartingStack = new int[16];
  92     protected int[] _namespaceAttributesStack = new int[16];
  93     protected int _namespaceAttributesStackIndex;
  94 
  95     public SAXBufferProcessor() {
  96     }
  97 
  98     /**
  99      * @deprecated
 100      *      Use {@link #SAXBufferProcessor(XMLStreamBuffer, boolean)}
 101      */
 102     public SAXBufferProcessor(XMLStreamBuffer buffer) {
 103         setXMLStreamBuffer(buffer);
 104     }
 105 
 106     /**
 107      * @param produceFragmentEvent
 108      *      True to generate fragment SAX events without start/endDocument.
 109      *      False to generate a full document SAX events.
 110      */
 111     public SAXBufferProcessor(XMLStreamBuffer buffer, boolean produceFragmentEvent) {


 420                 case STATE_COMMENT_AS_STRING:
 421                     processComment(readContentString());
 422                     break;
 423                 case STATE_PROCESSING_INSTRUCTION:
 424                     processProcessingInstruction(readStructureString(), readStructureString());
 425                     break;
 426                 case STATE_END:
 427                     return;
 428                 default:
 429                     throw reportFatalError("Illegal state for child of DII: "+item);
 430             }
 431         }
 432     }
 433 
 434     protected void processElement(String uri, String localName, String qName, boolean inscope) throws SAXException {
 435         boolean hasAttributes = false;
 436         boolean hasNamespaceAttributes = false;
 437         int item = peekStructure();
 438         Set<String> prefixSet = inscope ? new HashSet<String>() : Collections.<String>emptySet();
 439         if ((item & TYPE_MASK) == T_NAMESPACE_ATTRIBUTE) {
 440             cacheNamespacePrefixStartingIndex();
 441             hasNamespaceAttributes = true;
 442             item = processNamespaceAttributes(item, inscope, prefixSet);
 443         }
 444         if (inscope) {
 445             readInscopeNamespaces(prefixSet);
 446         }
 447 
 448         if ((item & TYPE_MASK) == T_ATTRIBUTE) {
 449             hasAttributes = true;
 450             processAttributes(item);
 451         }
 452 
 453         _contentHandler.startElement(uri, localName, qName, _attributes);
 454 
 455         if (hasAttributes) {
 456             _attributes.clear();
 457         }
 458 
 459         do {
 460             item = readEiiState();


 555 
 556     }
 557 
 558      private static String fixNull(String s) {
 559         if (s == null) return "";
 560         else return s;
 561     }
 562     private void processCommentAsCharArrayCopy() throws SAXException {
 563         final char[] ch = readContentCharactersCopy();
 564         processComment(ch, 0, ch.length);
 565     }
 566 
 567     private void processCommentAsCharArrayMedium() throws SAXException {
 568         final int length = readStructure16();
 569         final int start = readContentCharactersBuffer(length);
 570         processComment(_contentCharactersBuffer, start, length);
 571     }
 572 
 573     private void processEndPrefixMapping() throws SAXException {
 574         final int end = _namespaceAttributesStack[--_namespaceAttributesStackIndex];
 575 //      final int start = (_namespaceAttributesStackIndex > 0) ? _namespaceAttributesStack[_namespaceAttributesStackIndex] : 0;
 576         final int start = (_namespaceAttributesStackIndex >= 0) ? _namespaceAttributesStartingStack[_namespaceAttributesStackIndex] : 0;
 577 
 578         for (int i = end - 1; i >= start; i--) {
 579             _contentHandler.endPrefixMapping(_namespacePrefixes[i]);
 580         }
 581         _namespacePrefixesIndex = start;
 582     }
 583 
 584     private int processNamespaceAttributes(int item,boolean collectPrefixes, Set<String> prefixSet) throws SAXException {
 585         do {
 586             String prefix;
 587             switch(getNIIState(item)) {
 588                 case STATE_NAMESPACE_ATTRIBUTE:
 589                     // Undeclaration of default namespace
 590                     processNamespaceAttribute("", "");
 591                     if(collectPrefixes) {
 592                         prefixSet.add("");
 593                     }
 594                     break;
 595                 case STATE_NAMESPACE_ATTRIBUTE_P:
 596                     // Undeclaration of namespace


 683     }
 684 
 685     private void cacheNamespacePrefix(String prefix) {
 686         if (_namespacePrefixesIndex == _namespacePrefixes.length) {
 687             final String[] namespaceAttributes = new String[_namespacePrefixesIndex * 3 / 2 + 1];
 688             System.arraycopy(_namespacePrefixes, 0, namespaceAttributes, 0, _namespacePrefixesIndex);
 689             _namespacePrefixes = namespaceAttributes;
 690         }
 691 
 692         _namespacePrefixes[_namespacePrefixesIndex++] = prefix;
 693     }
 694 
 695     private void cacheNamespacePrefixIndex() {
 696         if (_namespaceAttributesStackIndex == _namespaceAttributesStack.length) {
 697             final int[] namespaceAttributesStack = new int[_namespaceAttributesStackIndex * 3 /2 + 1];
 698             System.arraycopy(_namespaceAttributesStack, 0, namespaceAttributesStack, 0, _namespaceAttributesStackIndex);
 699             _namespaceAttributesStack = namespaceAttributesStack;
 700         }
 701 
 702         _namespaceAttributesStack[_namespaceAttributesStackIndex++] = _namespacePrefixesIndex;
 703     }
 704 
 705     private void cacheNamespacePrefixStartingIndex() {
 706         if (_namespaceAttributesStackIndex == _namespaceAttributesStartingStack.length) {
 707             final int[] namespaceAttributesStart = new int[_namespaceAttributesStackIndex * 3 /2 + 1];
 708             System.arraycopy(_namespaceAttributesStartingStack, 0, namespaceAttributesStart, 0, _namespaceAttributesStackIndex);
 709             _namespaceAttributesStartingStack = namespaceAttributesStart;
 710         }
 711         _namespaceAttributesStartingStack[_namespaceAttributesStackIndex] = _namespacePrefixesIndex;
 712     }
 713 
 714     private void processComment(String s)  throws SAXException {
 715         processComment(s.toCharArray(), 0, s.length());
 716     }
 717 
 718     private void processComment(char[] ch, int start, int length) throws SAXException {
 719         _lexicalHandler.comment(ch, start, length);
 720     }
 721 
 722     private void processProcessingInstruction(String target, String data) throws SAXException {
 723         _contentHandler.processingInstruction(target, data);
 724     }
 725 
 726     private static final DefaultWithLexicalHandler DEFAULT_LEXICAL_HANDLER = new DefaultWithLexicalHandler();
 727 }