summaryrefslogtreecommitdiff
path: root/src/library/scala/xml/parsing/FactoryAdapter.scala
blob: e23032ca7040f4fa2c05a312c60c7084f440bd4c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/*                     __                                               *\
**     ________ ___   / /  ___     Scala API                            **
**    / __/ __// _ | / /  / _ |    (c) 2003-2004, LAMP/EPFL             **
**  __\ \/ /__/ __ |/ /__/ __ |                                         **
** /____/\___/_/ |_/____/_/ | |                                         **
**                          |/                                          **
** $Id$
\*                                                                      */
package scala.xml.parsing ;

import java.io._ ;
import scala.collection.mutable.{HashMap,Stack};

import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;

import org.xml.sax.ErrorHandler;
import org.xml.sax.Locator;
import org.xml.sax.InputSource;

import org.xml.sax.SAXException;
import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXNotSupportedException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;

import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;


/** SAX adapter class, for use with Java SAX parser. Keeps track of
 *  namespace bindings, without relying on namespace handling of the
 *  underlying SAX parser.
 */
abstract class FactoryAdapter extends DefaultHandler() {

  val buffer      = new StringBuffer();
  val attribStack = new Stack[MetaData];
  val hStack      = new Stack[Node];   // [ element ] contains siblings
  val tagStack    = new Stack[String];
  var scopeStack  = new Stack[NamespaceBinding];

  var curTag : String = null ;
  var capture:boolean = false;

  // abstract methods

  /** Tests if an XML element contains text.
   * @return true if element named <code>localName</code> contains text.
   */
  def nodeContainsText( localName:String ):boolean ; // abstract

  /** creates an new non-text(tree) node.
   * @param elemName
   * @param attribs
   * @param chIter
   * @return a new XML element.
   */
  def createNode(pre: String, elemName: String, attribs: MetaData, scope: NamespaceBinding, chIter: List[Node] ):Node; //abstract

  /** creates a Text node.
   * @param text
   * @return a new Text node.
   */
  def createText( text:String ):Text; // abstract

  //
  // ContentHandler methods
  //

  val normalizeWhitespace = false;

  /** Characters.
  * @param ch
  * @param offset
  * @param length
  */
   override def characters(ch: Array[Char], offset: Int, length: Int): Unit = {

        if (capture) {
          if( normalizeWhitespace ) { // normalizing whitespace is not compliant, but useful */
	    var i:int = offset;
            var ws:boolean = false;
	    while (i < offset + length) {
              if ( Character.isWhitespace( ch(i) ) ) {
                if (!ws) {
                  buffer.append(' ');
                  ws = true;
                }
              } else {
                buffer.append(ch(i));
                ws = false;
              }
	      i = i+1;
            }
          } else { // compliant:report every character

              buffer.append( ch, offset, length );

          }
	}
   }

    //var elemCount = 0; //STATISTICS

    /* ContentHandler methods */

    /* Start prefix mapping - use default impl.
     def startPrefixMapping( prefix:String , uri:String ):Unit = {}
     */



    /* Start element. */
    override def startElement(uri:String, _localName:String, qname:String, attributes:Attributes ):Unit = {
      /*elemCount = elemCount + 1; STATISTICS */
      captureText();
      //Console.println("FactoryAdapter::startElement("+uri+","+_localName+","+qname+","+attributes+")");
      tagStack.push(curTag);
      curTag = qname; //localName ;

      val colon = qname.indexOf(':');
      val localName = if(-1 == colon) qname else qname.substring(colon+1,qname.length());

      //Console.println("FactoryAdapter::startElement - localName ="+localName);

      capture = nodeContainsText(localName) ;

      hStack.push( null );
      var m: MetaData = Null;

      var scpe = scopeStack.top;
      for( val i <- List.range( 0, attributes.getLength() )) {
        //val attrType = attributes.getType(i); // unused for now
        val qname = attributes.getQName(i);
        val value = attributes.getValue(i);
        val colon = qname.indexOf(':');
        if(-1 != colon) {                     // prefixed attribute
          val pre = qname.substring(0, colon);
          val key = qname.substring(colon+1, qname.length());
          if("xmlns" == pre)
            scpe = value.length() match {
              case 0 => new NamespaceBinding(key, null,  scpe);
              case _ => new NamespaceBinding(key, value, scpe);
            }
          else
              m = new PrefixedAttribute(pre, key, value, m)
        } else if("xmlns" == qname)
          scpe = value.length() match {
            case 0 => new NamespaceBinding(null, null,  scpe);
            case _ => new NamespaceBinding(null, value, scpe);
          }
          else
            m = new UnprefixedAttribute(qname, value, m)
      }
      scopeStack.push(scpe);
      attribStack.push( m );
      {}
    } // startElement(String,String,String,Attributes)


    /** captures text, possibly normalizing whitespace
     */
    def captureText():Unit = {
        if (capture == true) {
	    val text = buffer.toString();
	    if(( text.length() > 0 )&&( !( text.equals(" ")))) {
		val _ = hStack.push( createText( text ) );
	    }
        }
        buffer.setLength(0);
    }

    /** End element.
     * @param uri
     * @param localName
     * @param qname
     * @throws org.xml.sax.SAXException if ..
     */
    override def endElement(uri:String , _localName:String , qname:String ):Unit = {
      captureText();

      val metaData = attribStack.pop;

      // reverse order to get it right
      var v:List[Node] = Nil;
      var child:Node = hStack.pop;
      while( child != null ) {
        v = child::v;
        child = hStack.pop;
      }

      val colon = qname.indexOf(':');
      val localName = if(-1 == colon) qname else qname.substring(colon+1,qname.length());

      val scp = scopeStack.pop;
      // create element
      rootElem = if(-1 == colon)
          createNode( null, localName, metaData, scp, v );
        else
          createNode( qname.substring(0,colon), localName, metaData, scp, v );

      hStack.push(rootElem);

      // set
      curTag = tagStack.pop;

      if (curTag != null) // root level
        capture = nodeContainsText(curTag);
      else
        capture = false;

    } // endElement(String,String,String)

    //
    // ErrorHandler methods
    //

    /** Warning.*/
    override def warning(ex:SAXParseException ):Unit  = {
	// ignore warning, crimson warns even for entity resolution!
        //printError("Warning", ex);
    }
    /** Error.     */
    override def error(ex:SAXParseException ):Unit = {
        printError("Error", ex);
    }

    /** Fatal error.*/
    override def  fatalError(ex:SAXParseException ):Unit = {
        printError("Fatal Error", ex);
    }

    //
    // Protected methods
    //

    /** Prints the error message */
    protected def printError( errtype:String , ex:SAXParseException ):Unit = {

        System.err.print("[");
        System.err.print(errtype);
        System.err.print("] ");

        var systemId = ex.getSystemId();
        if (systemId != null) {
            val index = systemId.lastIndexOf('/');
            if (index != -1)
                systemId = systemId.substring(index + 1);
            //System.err.print(systemId);
        }

        System.err.print(':');
        System.err.print(ex.getLineNumber());
        System.err.print(':');
        System.err.print(ex.getColumnNumber());
        System.err.print(": ");
        System.err.print(ex.getMessage());
        System.err.println();
        System.err.flush();

    }

    var rootElem : Node = null:Node;

    //FactoryAdapter
    // MAIN
    //

    /** load XML document
     * @param source
     * @return a new XML document object
     */
    def  loadXML( source:InputSource ):Node = {

      // variables
      var parser:SAXParser  = null;

      // create parser
      try {
        val f = SAXParserFactory.newInstance();
        f.setNamespaceAware( false );
        parser = f.newSAXParser();
      } catch {
	case ( e:Exception ) => {
          System.err.println("error: Unable to instantiate parser");
          System.exit(-1);
        }
      }

      // parse file
      try {
        //System.err.println("[parsing \"" + source + "\"]");
        scopeStack.push(TopScope);
        parser.parse( source, this );
        scopeStack.pop;
      } catch {
        case ( e:SAXParseException ) => {
          // ignore
        }
        case ( e:Exception ) => {
          System.err.println("error: Parse error occurred - " + e.getMessage());
          if (e.isInstanceOf[ SAXException ]) {
            (e.asInstanceOf[ SAXException ])
	    .getException()
	    .printStackTrace( System.err );
          } else {
            e.printStackTrace(System.err);
	  }
        }
      } // catch
      //System.err.println("[FactoryAdapter: total #elements = "+elemCount+"]");
      rootElem

    } // loadXML



  /** loads XML from given file */
  def loadFile( file:File ):Node = loadXML( new InputSource(
    new FileInputStream( file )
  ));

  /** loads XML from given file descriptor */
  def loadFile( fileDesc:FileDescriptor ):Node = loadXML( new InputSource(
    new FileInputStream( fileDesc )
  ));

  /** loads XML from given file */
  def loadFile( fileName:String ):Node = loadXML( new InputSource(
    new FileInputStream( fileName )
  ));

  /** loads XML from given InputStream */
  def load( is:InputStream ):Node = loadXML( new InputSource( is ));

  /** loads XML from given Reader */
  def load( reader:Reader ):Node = loadXML( new InputSource( reader ));

  /** loads XML from given sysID */
  def load( sysID:String ):Node = loadXML( new InputSource( sysID ));

}