KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > quercus > lib > xml > Xml


1 /*
2  * Copyright (c) 1998-2005 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Charles Reich
28  */

29
30 package com.caucho.quercus.lib.xml;
31
32 import com.caucho.quercus.annotation.Optional;
33 import com.caucho.quercus.annotation.Reference;
34 import com.caucho.quercus.env.*;
35 import com.caucho.util.L10N;
36
37 import org.xml.sax.Attributes JavaDoc;
38 import org.xml.sax.InputSource JavaDoc;
39 import org.xml.sax.SAXException JavaDoc;
40 import org.xml.sax.helpers.DefaultHandler JavaDoc;
41
42 import javax.xml.parsers.ParserConfigurationException JavaDoc;
43 import javax.xml.parsers.SAXParser JavaDoc;
44 import javax.xml.parsers.SAXParserFactory JavaDoc;
45 import java.io.IOException JavaDoc;
46 import java.io.StringReader JavaDoc;
47 import java.util.ArrayList JavaDoc;
48 import java.util.HashMap JavaDoc;
49 import java.util.logging.Level JavaDoc;
50 import java.util.logging.Logger JavaDoc;
51
52 /**
53  * XML object oriented API facade
54  */

55 public class Xml {
56   private static final Logger JavaDoc log = Logger.getLogger(Xml.class.getName());
57   private static final L10N L = new L10N(Xml.class);
58
59   /**
60    * XML_OPTION_CASE_FOLDING is enabled by default
61    *
62    * only affects startElement (including attribute
63    * names) and endElement handlers.
64    */

65   private boolean _xmlOptionCaseFolding = true;
66
67   private String JavaDoc _xmlOptionTargetEncoding;
68
69   /**
70    * XML_OPTION_SKIP_TAGSTART specifies how many chars
71    * should be skipped in the beginning of a tag name (default = 0)
72    *
73    * XXX: Not yet implemented
74    */

75   private long _xmlOptionSkipTagstart = 0;
76
77   /**
78    * XXX: _xmlOptionSkipWhite not yet implemented
79    */

80   private boolean _xmlOptionSkipWhite = false;
81
82   private Env _env;
83
84   /** XXX: _separator is set by xml_parse_create_ns but
85    * not yet used. Default value is ":"
86    * Possibly should report error if user wants to use
87    * anything other than ":"
88    */

89   private String JavaDoc _separator;
90
91   private int _errorCode = XmlModule.XML_ERROR_NONE;
92   private String JavaDoc _errorString;
93
94   private Callback _startElementHandler;
95   private Callback _endElementHandler;
96   private Callback _characterDataHandler;
97   private Callback _processingInstructionHandler;
98   private Callback _defaultHandler;
99   private Callback _startNamespaceDeclHandler;
100   private Callback _endNamespaceDeclHandler;
101   private Callback _notationDeclHandler;
102   private Callback _unparsedEntityDeclHandler;
103
104   private Value _parser;
105   private Value _obj;
106
107   SAXParserFactory JavaDoc _factory = SAXParserFactory.newInstance();
108
109   private StringBuilder JavaDoc _xmlString = new StringBuilder JavaDoc();
110
111   public Xml(Env env,
112              String JavaDoc outputEncoding,
113              String JavaDoc separator)
114   {
115     _env = env;
116     _xmlOptionTargetEncoding = outputEncoding;
117     _parser = _env.wrapJava(this);
118     _separator = separator;
119   }
120
121   public int getErrorCode()
122   {
123     return _errorCode;
124   }
125
126   public String JavaDoc getErrorString()
127   {
128     return _errorString;
129   }
130
131   /**
132    * Sets the element handler functions for the XML parser.
133    *
134    * @param startElementHandler must exist when xml_parse is called
135    * @param endElementHandler must exist when xml_parse is called
136    * @return true always even if handlers are disabled
137    */

138
139   public boolean xml_set_element_handler(Value startElementHandler,
140                                          Value endElementHandler)
141   {
142     if (_obj == null) {
143       _startElementHandler = _env.createCallback(startElementHandler);
144       _endElementHandler = _env.createCallback(endElementHandler);
145     } else {
146       Value value = new ArrayValueImpl();
147       value.put(_obj);
148       value.put(startElementHandler);
149       _startElementHandler = _env.createCallback(value);
150
151       value = new ArrayValueImpl();
152       value.put(_obj);
153       value.put(endElementHandler);
154       _endElementHandler = _env.createCallback(value);
155     }
156     return true;
157   }
158
159   /**
160    * Sets the character data handler function.
161    *
162    * @param handler can be empty string or FALSE
163    * @return true always even if handler is disabled
164    */

165   public boolean xml_set_character_data_handler(Value handler)
166   {
167     if (_obj == null) {
168       _characterDataHandler = _env.createCallback(handler);
169     } else {
170       Value value = new ArrayValueImpl();
171       value.put(_obj);
172       value.put(handler);
173       _characterDataHandler = _env.createCallback(value);
174     }
175
176     return true;
177   }
178
179   /**
180    * The php documentation is very vague as to the purpose
181    * of the default handler.
182    *
183    * We are interpreting it as an alternative to the character
184    * data handler.
185    *
186    * If character handler is defined, then use that. Otherwise,
187    * use default handler, if it is defined.
188    *
189    * XXX: Need to confirm that this is appropriate
190    *
191    * @param handler
192    * @return true always even if handler is disabled
193    */

194   public boolean xml_set_default_handler(Value handler)
195   {
196     if (_obj == null) {
197       _defaultHandler = _env.createCallback(handler);
198     } else {
199       Value value = new ArrayValueImpl();
200       value.put(_obj);
201       value.put(handler);
202       _defaultHandler = _env.createCallback(value);
203     }
204     return true;
205   }
206
207   /**
208    * Sets the processing instruction handler function
209    *
210    * @param processingInstructionHandler
211    * @return true always even if handler is disabled
212    */

213   public boolean xml_set_processing_instruction_handler(Value processingInstructionHandler)
214   {
215     if (_obj == null) {
216       _processingInstructionHandler = _env.createCallback(processingInstructionHandler);
217     } else {
218       Value value = new ArrayValueImpl();
219       value.put(_obj);
220       value.put(processingInstructionHandler);
221       _processingInstructionHandler = _env.createCallback(value);
222     }
223     return true;
224   }
225
226   /**
227    * Sets the startPrefixMapping handler
228    *
229    * @param startNamespaceDeclHandler
230    * @return true always even if handler is disabled
231    */

232   public boolean xml_set_start_namespace_decl_handler(Value startNamespaceDeclHandler)
233   {
234     if (_obj == null) {
235       _startNamespaceDeclHandler = _env.createCallback(startNamespaceDeclHandler);
236     } else {
237       Value value = new ArrayValueImpl();
238       value.put(_obj);
239       value.put(startNamespaceDeclHandler);
240       _startNamespaceDeclHandler = _env.createCallback(value);
241     }
242     return true;
243   }
244
245   /**
246    * Sets the unparsedEntityDecl handler
247    *
248    * @param handler
249    * @return true always even if handler is disabled
250    */

251   public boolean xml_set_unparsed_entity_decl_handler(Value handler)
252   {
253     if (_obj == null) {
254       _unparsedEntityDeclHandler = _env.createCallback(handler);
255     } else {
256       Value value = new ArrayValueImpl();
257       value.put(_obj);
258       value.put(handler);
259       _unparsedEntityDeclHandler = _env.createCallback(value);
260     }
261     return true;
262   }
263
264   /**
265    * Sets the endPrefixMapping handler
266    *
267    * @param endNamespaceDeclHandler
268    * @return true always even if handler is disabled
269    */

270   public boolean xml_set_end_namespace_decl_handler(Value endNamespaceDeclHandler)
271   {
272     if (_obj == null) {
273       _endNamespaceDeclHandler = _env.createCallback(endNamespaceDeclHandler);
274     } else {
275       Value value = new ArrayValueImpl();
276       value.put(_obj);
277       value.put(endNamespaceDeclHandler);
278       _endNamespaceDeclHandler = _env.createCallback(value);
279     }
280     return true;
281   }
282
283   /**
284    * Sets the notationDecl handler
285    *
286    * @param handler
287    * @return true always even if handler is disabled
288    */

289   public boolean xml_set_notation_decl_handler(Value handler)
290   {
291     if (_obj == null) {
292       _notationDeclHandler = _env.createCallback(handler);
293     } else {
294       Value value = new ArrayValueImpl();
295       value.put(_obj);
296       value.put(handler);
297       _notationDeclHandler = _env.createCallback(value);
298     }
299     return true;
300   }
301
302   /**
303    * sets the object which houses all the callback functions
304    *
305    * @param obj
306    * @return returns true unless obj == null
307    */

308   public boolean xml_set_object(Value obj)
309   {
310     if (obj == null)
311       return false;
312
313     _obj = obj;
314
315     return true;
316   }
317
318   /**
319    * xml_parse will keep accumulating "data" until
320    * either is_final is true or omitted
321    *
322    * @param data
323    * @param isFinal
324    * @return
325    * @throws IOException
326    * @throws SAXException
327    * @throws ParserConfigurationException
328    */

329   public boolean xml_parse(Env env, String JavaDoc data,
330                            @Optional("true") boolean isFinal)
331     throws Exception JavaDoc
332   {
333     _xmlString.append(data);
334
335     if (isFinal) {
336       InputSource JavaDoc is = new InputSource JavaDoc(new StringReader JavaDoc(_xmlString.toString()));
337       if (_xmlOptionTargetEncoding == null)
338         _xmlOptionTargetEncoding = is.getEncoding();
339       
340       try {
341     _errorCode = XmlModule.XML_ERROR_NONE;
342     _errorString = null;
343     
344         SAXParser JavaDoc saxParser = _factory.newSAXParser();
345         saxParser.parse(is, new XmlHandler());
346       } catch (Exception JavaDoc ex) {
347     ArrayValue array = new ArrayValueImpl();
348     _errorCode = XmlModule.XML_ERROR_SYNTAX;
349     
350         throw ex;
351       }
352     }
353
354     return true;
355   }
356
357   /**
358    * Parses data into 2 parallel array structures.
359    *
360    * @param data
361    * @param valsV
362    * @param indexV
363    * @return 0 for failure, 1 for success
364    */

365   public int xml_parse_into_struct(String JavaDoc data,
366                                    @Reference Value valsV,
367                                    @Optional @Reference Value indexV)
368     throws Exception JavaDoc
369   {
370     _xmlString.append(data);
371
372     InputSource JavaDoc is = new InputSource JavaDoc(new StringReader JavaDoc(_xmlString.toString()));
373
374     ArrayValueImpl valueArray = new ArrayValueImpl();
375     ArrayValueImpl indexArray = new ArrayValueImpl();
376
377     try {
378       SAXParser JavaDoc saxParser = _factory.newSAXParser();
379       saxParser.parse(is, new StructHandler(valueArray, indexArray));
380     } catch (Exception JavaDoc ex) {
381       log.log(Level.FINE, ex.toString(), ex);
382       throw new Exception JavaDoc(L.l(ex.getMessage()));
383     }
384
385     valsV.set(valueArray);
386     indexV.set(indexArray);
387
388     return 1;
389   }
390
391   /**
392    * sets one of the following:
393    * _xmlOptionCaseFolding (ENABLED / DISABLED)
394    * _xmlOptionTargetEncoding (String)
395    * _xmlOptionSkipTagstart (int)
396    * _xmlOptionSkipWhite (ENABLED / DISABLED)
397    *
398    * XXX: currently only _xmlOptionCaseFolding actually does something
399    *
400    * @param option
401    * @param value
402    * @return true unless value could not be set
403    */

404   public boolean xml_parser_set_option(int option,
405                                        Value value)
406   {
407     switch(option) {
408       case XmlModule.XML_OPTION_CASE_FOLDING:
409         if (value instanceof BooleanValue) {
410           _xmlOptionCaseFolding = value.toBoolean();
411           return true;
412         } else {
413           return false;
414         }
415       case XmlModule.XML_OPTION_SKIP_TAGSTART:
416         if (value instanceof LongValue) {
417           _xmlOptionSkipTagstart = value.toLong();
418           return true;
419         } else {
420           return false;
421         }
422       case XmlModule.XML_OPTION_SKIP_WHITE:
423         if (value instanceof BooleanValue) {
424           _xmlOptionSkipWhite = value.toBoolean();
425           return true;
426         } else {
427           return false;
428         }
429       case XmlModule.XML_OPTION_TARGET_ENCODING:
430         if (value instanceof StringValue) {
431           _xmlOptionTargetEncoding = value.toString();
432           return true;
433         } else {
434           return false;
435         }
436       default:
437         return false;
438     }
439   }
440
441   /**
442    *
443    * @param option
444    * @return relevant value
445    */

446   public Value xml_parser_get_option(int option)
447   {
448     switch (option) {
449       case XmlModule.XML_OPTION_CASE_FOLDING:
450         return (_xmlOptionCaseFolding ? BooleanValue.TRUE : BooleanValue.FALSE);
451       case XmlModule.XML_OPTION_SKIP_TAGSTART:
452         return new LongValue(_xmlOptionSkipTagstart);
453       case XmlModule.XML_OPTION_SKIP_WHITE:
454         return (_xmlOptionSkipWhite ? BooleanValue.TRUE : BooleanValue.FALSE);
455       case XmlModule.XML_OPTION_TARGET_ENCODING:
456         return new StringValueImpl(_xmlOptionTargetEncoding);
457       default:
458         return BooleanValue.FALSE;
459     }
460   }
461
462   public String JavaDoc toString()
463   {
464     return "Xml[]";
465   }
466
467   /**
468    * handler solely for xml_parse_into_struct
469    */

470   class StructHandler extends DefaultHandler JavaDoc {
471     private ArrayValueImpl _valueArray;
472     private ArrayValueImpl _indexArray;
473
474     //Keeps track of depth within tree;
475
//startElement increments, endElement decrements
476
private int _level = 1;
477
478     private HashMap JavaDoc<Integer JavaDoc, String JavaDoc> _paramHashMap = new HashMap JavaDoc<Integer JavaDoc, String JavaDoc> ();
479     private HashMap JavaDoc<StringValue, ArrayValueImpl> _indexArrayHashMap = new HashMap JavaDoc<StringValue, ArrayValueImpl>();
480     private ArrayList JavaDoc<StringValue> _indexArrayKeys = new ArrayList JavaDoc<StringValue>();
481
482     // Used to determine whether a given element has sub elements
483
private boolean _isComplete = true;
484     private boolean _isOutside = true;
485
486     private int _valueArrayIndex = 0;
487
488     public StructHandler(ArrayValueImpl valueArray,
489                          ArrayValueImpl indexArray)
490     {
491       _valueArray = valueArray;
492       _indexArray = indexArray;
493     }
494
495     /**
496      * helper function to create an array of attributes for a tag
497      * @param attrs
498      * @return array of attributes
499      */

500     private ArrayValueImpl createAttributeArray(Attributes JavaDoc attrs)
501     {
502       ArrayValueImpl result = new ArrayValueImpl();
503
504       // turn attrs into an array of name, value pairs
505
for (int i = 0; i < attrs.getLength(); i++) {
506         String JavaDoc aName = attrs.getLocalName(i); // Attr name
507
if ("".equals(aName)) aName = attrs.getQName(i);
508         if (_xmlOptionCaseFolding) aName = aName.toUpperCase();
509         result.put(new StringValueImpl(aName), new StringValueImpl(attrs.getValue(i)));
510       }
511
512       return result;
513     }
514
515     public void endDocument()
516       throws SAXException JavaDoc
517     {
518       for(StringValue sv : _indexArrayKeys) {
519         _indexArray.put(sv, _indexArrayHashMap.get(sv));
520       }
521     }
522
523     public void startElement(String JavaDoc namespaceURI,
524                              String JavaDoc lName,
525                              String JavaDoc qName,
526                              Attributes JavaDoc attrs)
527       throws SAXException JavaDoc
528     {
529       Value elementArray = new ArrayValueImpl();
530
531       String JavaDoc eName = lName; // element name
532
if ("".equals(eName)) eName = qName;
533       if (_xmlOptionCaseFolding) eName = eName.toUpperCase();
534
535       elementArray.put(new StringValueImpl("tag"), new StringValueImpl(eName));
536       elementArray.put(new StringValueImpl("type"), new StringValueImpl("open"));
537       elementArray.put(new StringValueImpl("level"), new DoubleValue((double) _level));
538       _paramHashMap.put(_level, eName);
539
540       if (attrs.getLength() > 0) {
541         elementArray.put(new StringValueImpl("attributes"), createAttributeArray(attrs));
542       }
543
544       _valueArray.put(new DoubleValue((double)_valueArrayIndex), elementArray);
545
546       addToIndexArrayHashMap(eName);
547
548       _valueArrayIndex++;
549       _level++;
550       _isComplete = true;
551       _isOutside = false;
552     }
553
554     public void endElement(String JavaDoc namespaceURI,
555                            String JavaDoc sName,
556                            String JavaDoc qName)
557       throws SAXException JavaDoc
558     {
559       Value elementArray;
560
561       _level--;
562
563       if (_isComplete) {
564         elementArray = _valueArray.get(new DoubleValue((double) _valueArrayIndex - 1));
565         elementArray.put(new StringValueImpl("type"), new StringValueImpl("complete"));
566       } else {
567         elementArray = new ArrayValueImpl();
568         String JavaDoc eName = sName; // element name
569
if ("".equals(sName)) eName = qName;
570         if (_xmlOptionCaseFolding) eName = eName.toUpperCase();
571         elementArray.put(new StringValueImpl("tag"), new StringValueImpl(eName));
572         elementArray.put(new StringValueImpl("type"), new StringValueImpl("close"));
573         elementArray.put(new StringValueImpl("level"), new DoubleValue((double) _level));
574         _valueArray.put(new DoubleValue((double)_valueArrayIndex), elementArray);
575
576         addToIndexArrayHashMap(eName);
577         _valueArrayIndex++;
578       }
579
580       _isComplete = false;
581       _isOutside = true;
582     }
583
584     private void addToIndexArrayHashMap(String JavaDoc eName)
585     {
586       StringValue key = new StringValueImpl(eName);
587       ArrayValueImpl indexArray = _indexArrayHashMap.get(key);
588
589       if (indexArray == null) {
590         indexArray = new ArrayValueImpl();
591         _indexArrayKeys.add(key);
592       }
593
594       indexArray.put(new DoubleValue((double) _valueArrayIndex));
595       _indexArrayHashMap.put(key, indexArray);
596     }
597
598     public void characters(char[] ch,
599                            int start,
600                            int length)
601       throws SAXException JavaDoc
602     {
603       String JavaDoc s = new String JavaDoc(ch, start, length);
604
605       if (_isOutside) {
606         Value elementArray = new ArrayValueImpl();
607         elementArray.put(new StringValueImpl("tag"), new StringValueImpl(_paramHashMap.get(_level - 1)));
608         elementArray.put(new StringValueImpl("value"), new StringValueImpl(s));
609         elementArray.put(new StringValueImpl("type"), new StringValueImpl("cdata"));
610         elementArray.put(new StringValueImpl("level"), new DoubleValue((double) _level - 1));
611         _valueArray.put(new DoubleValue((double)_valueArrayIndex), elementArray);
612
613         Value indexArray = _indexArray.get(new StringValueImpl(_paramHashMap.get(_level - 1)));
614         indexArray.put(new DoubleValue((double) _valueArrayIndex));
615
616         _valueArrayIndex++;
617       } else {
618         Value elementArray = _valueArray.get(new DoubleValue((double) _valueArrayIndex - 1));
619         elementArray.put(new StringValueImpl("value"), new StringValueImpl(s));
620       }
621     }
622   }
623
624   class XmlHandler extends DefaultHandler JavaDoc {
625
626     /**
627      * wrapper for _startElementHandler. creates Value[] args
628      *
629      * @param namespaceURI
630      * @param lName
631      * @param qName
632      * @param attrs
633      * @throws SAXException
634      */

635     public void startElement(String JavaDoc namespaceURI,
636                              String JavaDoc lName,
637                              String JavaDoc qName,
638                              Attributes JavaDoc attrs)
639       throws SAXException JavaDoc
640     {
641       /**
642        * args[0] reference to this parser
643        * args[1] name of element
644        * args[2] array of attributes
645        *
646        * Typical call in PHP looks like:
647        *
648        * function startElement($parser, $name, $attrs) {...}
649        */

650       Value[] args = new Value[3];
651
652       args[0] = _parser;
653
654       String JavaDoc eName = lName; // element name
655
if ("".equals(eName)) eName = qName;
656       if (_xmlOptionCaseFolding) eName = eName.toUpperCase();
657       args[1] = new StringValueImpl(eName);
658
659       // turn attrs into an array of name, value pairs
660
args[2] = new ArrayValueImpl();
661       for (int i = 0; i < attrs.getLength(); i++) {
662         String JavaDoc aName = attrs.getLocalName(i); // Attr name
663
if ("".equals(aName)) aName = attrs.getQName(i);
664         if (_xmlOptionCaseFolding) aName = aName.toUpperCase();
665         args[2].put(new StringValueImpl(aName), new StringValueImpl(attrs.getValue(i)));
666       }
667
668       try {
669         if (_startElementHandler != null)
670           _startElementHandler.call(_env,args);
671         else
672           throw new Throwable JavaDoc("start element handler is not set");
673       } catch (Throwable JavaDoc t) {
674         log.log(Level.FINE, t.toString(), t);
675         throw new SAXException JavaDoc(L.l(t.getMessage()));
676       }
677     }
678
679     /**
680      * wrapper for _endElementHandler
681      *
682      * @param namespaceURI
683      * @param sName
684      * @param qName
685      * @throws SAXException
686      */

687     public void endElement(String JavaDoc namespaceURI,
688                            String JavaDoc sName,
689                            String JavaDoc qName)
690       throws SAXException JavaDoc
691     {
692       try {
693         String JavaDoc eName = sName; // element name
694
if ("".equals(eName)) eName = qName;
695         if (_xmlOptionCaseFolding) eName = eName.toUpperCase();
696
697         if (_endElementHandler != null)
698           _endElementHandler.call(_env, _parser, new StringValueImpl(eName));
699         else
700           throw new Throwable JavaDoc("end element handler is not set");
701       } catch (Throwable JavaDoc t) {
702         log.log(Level.FINE, t.toString(), t);
703         throw new SAXException JavaDoc(L.l(t.getMessage()));
704       }
705     }
706
707     /**
708      * wrapper for _characterDataHandler
709      *
710      * @param ch
711      * @param start
712      * @param length
713      * @throws SAXException
714      */

715     public void characters(char[] ch,
716                            int start,
717                            int length)
718       throws SAXException JavaDoc
719     {
720       String JavaDoc s = new String JavaDoc(ch,start,length);
721
722       try {
723         if (_characterDataHandler != null)
724           _characterDataHandler.call(_env, _parser, new StringValueImpl(s));
725         else if (_defaultHandler != null)
726           _defaultHandler.call(_env, _parser, new StringValueImpl(s));
727         else
728           throw new Throwable JavaDoc("neither character data handler nor default handler is set");
729       } catch (Throwable JavaDoc t) {
730         log.log(Level.FINE, t.toString(), t);
731         throw new SAXException JavaDoc(L.l(t.getMessage()));
732       }
733     }
734
735     /**
736      * wrapper for _processingInstructionHandler
737      * @param target
738      * @param data
739      * @throws SAXException
740      */

741     public void processingInstruction(String JavaDoc target,
742                                       String JavaDoc data)
743       throws SAXException JavaDoc
744     {
745       try {
746         if (_processingInstructionHandler != null)
747           _processingInstructionHandler.call(_env, _parser, new StringValueImpl(target), new StringValueImpl(data));
748         else
749           throw new Throwable JavaDoc("processing instruction handler is not set");
750       } catch (Throwable JavaDoc t) {
751         log.log(Level.FINE, t.toString(), t);
752         throw new SAXException JavaDoc(L.l(t.getMessage()));
753       }
754     }
755
756     /**
757      * wrapper for _startNamespaceDeclHandler
758      * @param prefix
759      * @param uri
760      * @throws SAXException
761      */

762     public void startPrefixMapping (String JavaDoc prefix,
763                                     String JavaDoc uri)
764       throws SAXException JavaDoc
765     {
766       try {
767         if (_startNamespaceDeclHandler != null)
768           _startNamespaceDeclHandler.call(_env, new StringValueImpl(prefix), new StringValueImpl(uri));
769         else
770           throw new Throwable JavaDoc("start namespace decl handler is not set");
771       } catch (Throwable JavaDoc t) {
772         log.log(Level.FINE, t.toString(), t);
773         throw new SAXException JavaDoc(L.l(t.getMessage()));
774       }
775     }
776
777     /**
778      * wrapper for _endNamespaceDeclHandler
779      *
780      * @param prefix
781      * @throws SAXException
782      */

783     public void endPrefixMapping(String JavaDoc prefix)
784       throws SAXException JavaDoc
785     {
786       try {
787         if (_endNamespaceDeclHandler != null)
788           _endNamespaceDeclHandler.call(_env, new StringValueImpl(prefix));
789         else
790           throw new Throwable JavaDoc("end namespace decl handler is not set");
791       } catch (Throwable JavaDoc t) {
792         log.log(Level.FINE, t.toString(), t);
793         throw new SAXException JavaDoc(L.l(t.getMessage()));
794       }
795     }
796
797     public void notationDecl(String JavaDoc name,
798                              String JavaDoc publicId,
799                              String JavaDoc systemId)
800       throws SAXException JavaDoc
801     {
802       try {
803         if (_notationDeclHandler != null)
804           _notationDeclHandler.call(_env,
805                                     _parser,
806                                     new StringValueImpl(name),
807                                     new StringValueImpl(""),
808                                     new StringValueImpl(systemId),
809                                     new StringValueImpl(publicId));
810         else
811           throw new Throwable JavaDoc("notation declaration handler is not set");
812       } catch (Throwable JavaDoc t) {
813         log.log(Level.FINE, t.toString(), t);
814         throw new SAXException JavaDoc(L.l(t.getMessage()));
815       }
816     }
817
818     @Override JavaDoc
819     public void unparsedEntityDecl(String JavaDoc name,
820                                    String JavaDoc publicId,
821                                    String JavaDoc systemId,
822                                    String JavaDoc notationName)
823       throws SAXException JavaDoc
824     {
825       /**
826        * args[0] reference to this parser
827        * args[1] name
828        * args[2] base (always "")
829        * args[3] systemId
830        * args[4] publicId
831        * args[5] notationName
832        */

833       Value[] args = new Value[6];
834
835       args[0] = _parser;
836       args[1] = new StringValueImpl(name);
837       args[2] = new StringValueImpl("");
838       args[3] = new StringValueImpl(systemId);
839       args[4] = new StringValueImpl(publicId);
840       args[5] = new StringValueImpl(notationName);
841
842       try {
843         if (_unparsedEntityDeclHandler != null)
844           _unparsedEntityDeclHandler.call(_env, args);
845         else
846           throw new Exception JavaDoc("unparsed entity declaration handler is not set");
847       } catch (Throwable JavaDoc t) {
848         log.log(Level.FINE, t.toString(), t);
849         throw new SAXException JavaDoc(L.l(t.getMessage()));
850       }
851     }
852   }
853 }
854
Popular Tags