/*
 *  eXist Open Source Native XML Database
 *  Copyright (C) 2001-07 The eXist Project
 *  http://exist-db.org
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * \$Id\$
 */

package org.exist.stax;

import javax.xml.stream.XMLStreamConstants;

import org.exist.xquery.value.Type;
import org.w3c.dom.Node;

public class StaXUtil {

    public static int streamType2Type(int type) {
        int xpathType;
        switch (type) {
            case XMLStreamConstants.START_ELEMENT :
                xpathType = Type.ELEMENT;
                break;
            case XMLStreamConstants.ATTRIBUTE :
                xpathType = Type.ATTRIBUTE;
                break;
            case XMLStreamConstants.CHARACTERS :
            case XMLStreamConstants.CDATA :
                xpathType = Type.TEXT;
                break;
            case XMLStreamConstants.COMMENT :
                xpathType = Type.COMMENT;
                break;
            case XMLStreamConstants.PROCESSING_INSTRUCTION :
                xpathType = Type.PROCESSING_INSTRUCTION;
                break;
            default:
                xpathType = Type.UNTYPED;
        }
        return xpathType;
    }

    public static short streamType2DOM(int type) {
        short domType;
        switch (type) {
            case XMLStreamConstants.START_ELEMENT :
                domType = Node.ELEMENT_NODE;
                break;
            case XMLStreamConstants.ATTRIBUTE :
                domType = Node.ATTRIBUTE_NODE;
                break;
            case XMLStreamConstants.CHARACTERS :
                domType = Node.TEXT_NODE;
                break;
            case XMLStreamConstants.CDATA :
                domType = Node.CDATA_SECTION_NODE;
                break;
            case XMLStreamConstants.COMMENT :
                domType = Node.COMMENT_NODE;
                break;
            case XMLStreamConstants.PROCESSING_INSTRUCTION :
                domType = Node.PROCESSING_INSTRUCTION_NODE;
                break;
            default:
                domType = -1;
        }
        return domType;
    }
}

@@ -1068,16 +1068,22 @@
 pe2Type(
+final 
 int type
@@ -1242,32 +1242,33 @@
          break;%0A
+%0A
             case
@@ -1283,33 +1283,46 @@
 amConstants.
-ATTRIBUTE
+PROCESSING_INSTRUCTION
  :%0A         
@@ -1345,25 +1345,38 @@
  = Type.
-ATTRIBUTE
+PROCESSING_INSTRUCTION
 ;%0A      
@@ -1384,32 +1384,33 @@
          break;%0A
+%0A
             case
@@ -1446,52 +1446,8 @@
 S :%0A
-            case XMLStreamConstants.CDATA :%0A
     
@@ -1496,32 +1496,33 @@
          break;%0A
+%0A
             case
@@ -1608,32 +1608,33 @@
          break;%0A
+%0A
             case
@@ -1657,88 +1657,286 @@
 nts.
-PROCESSING_INSTRUCTION :%0A                xpathType = Type.PROCESSING_INSTRUCTION
+START_DOCUMENT:%0A                xpathType = Type.DOCUMENT;%0A                break;%0A%0A            case XMLStreamConstants.ATTRIBUTE:%0A                xpathType = Type.ATTRIBUTE;%0A                break;%0A%0A            case XMLStreamConstants.CDATA:%0A                xpathType = Type.TEXT
 ;%0A  
@@ -1948,32 +1948,33 @@
          break;%0A
+%0A
             defa
@@ -2102,16 +2102,22 @@
 ype2DOM(
+final 
 int type
@@ -2208,33 +2208,32 @@
 ts.START_ELEMENT
- 
 :%0A              
@@ -2278,32 +2278,33 @@
          break;%0A
+%0A
             case
@@ -2327,18 +2327,30 @@
 nts.
-ATTRIBUTE 
+PROCESSING_INSTRUCTION
 :%0A  
@@ -2378,25 +2378,38 @@
  = Node.
-ATTRIBUTE
+PROCESSING_INSTRUCTION
 _NODE;%0A 
@@ -2422,32 +2422,33 @@
          break;%0A
+%0A
             case
@@ -2469,33 +2469,32 @@
 tants.CHARACTERS
- 
 :%0A              
@@ -2536,32 +2536,33 @@
          break;%0A
+%0A
             case
@@ -2586,13 +2586,14 @@
 ts.C
-DATA 
+OMMENT
 :%0A  
@@ -2626,20 +2626,14 @@
 de.C
-DATA_SECTION
+OMMENT
 _NOD
@@ -2650,32 +2650,33 @@
          break;%0A
+%0A
             case
@@ -2691,32 +2691,38 @@
 amConstants.
-COM
+START_DOCU
 MENT
- 
 :%0A          
@@ -2734,35 +2734,36 @@
  domType = Node.
-COM
+DOCU
 MENT_NODE;%0A     
@@ -2772,32 +2772,33 @@
          break;%0A
+%0A
             case
@@ -2821,86 +2821,549 @@
 nts.
-PROCESSING_INSTRUCTION :%0A                domType = Node.PROCESSING_INSTRUCTION
+ENTITY_REFERENCE:%0A                domType = Node.ENTITY_REFERENCE_NODE;%0A                break;%0A%0A            case XMLStreamConstants.ATTRIBUTE:%0A                domType = Node.ATTRIBUTE_NODE;%0A                break;%0A%0A            case XMLStreamConstants.DTD:%0A                domType = Node.DOCUMENT_TYPE_NODE;%0A                break;%0A%0A            case XMLStreamConstants.CDATA:%0A                domType = Node.CDATA_SECTION_NODE;%0A                break;%0A%0A            case XMLStreamConstants.ENTITY_DECLARATION:%0A                domType = Node.ENTITY
 _NOD
@@ -3388,16 +3388,17 @@
  break;%0A
+%0A
         

Add missing Stax type conversions