KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright (c) 1998-2006 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 Scott Ferguson
28  */

29
30 package com.caucho.quercus.lib.xml;
31
32 import com.caucho.quercus.QuercusModuleException;
33 import com.caucho.quercus.annotation.NotNull;
34 import com.caucho.quercus.annotation.Optional;
35 import com.caucho.quercus.annotation.Reference;
36 import com.caucho.quercus.env.BooleanValue;
37 import com.caucho.quercus.env.Env;
38 import com.caucho.quercus.env.LongValue;
39 import com.caucho.quercus.env.StringValue;
40 import com.caucho.quercus.env.StringValueImpl;
41 import com.caucho.quercus.env.Value;
42 import com.caucho.quercus.module.AbstractQuercusModule;
43 import com.caucho.util.L10N;
44
45 /**
46  * PHP XML
47  */

48 public class XmlModule extends AbstractQuercusModule {
49   private static final L10N L = new L10N(XmlModule.class);
50
51
52   public static final int XML_OPTION_CASE_FOLDING = 0x0;
53   public static final int XML_OPTION_SKIP_TAGSTART = 0x1;
54   public static final int XML_OPTION_SKIP_WHITE = 0x2;
55   public static final int XML_OPTION_TARGET_ENCODING = 0x3;
56
57   public static final int XML_ERROR_NONE = 0;
58   public static final int XML_ERROR_NO_MEMORY = 1;
59   public static final int XML_ERROR_SYNTAX = 2;
60   public static final int XML_ERROR_NO_ELEMENTS = 3;
61   public static final int XML_ERROR_INVALID_TOKEN = 4;
62   public static final int XML_ERROR_UNCLOSED_TOKEN = 5;
63   public static final int XML_ERROR_PARTIAL_CHAR = 6;
64   public static final int XML_ERROR_TAG_MISMATCH = 7;
65   public static final int XML_ERROR_DUPLICATE_ATTRIBUTE = 8;
66   public static final int XML_ERROR_JUNK_AFTER_DOC_ELEMENT = 9;
67   public static final int XML_ERROR_PARAM_ENTITY_REF = 10;
68   public static final int XML_ERROR_UNDEFINED_ENTITY = 11;
69   public static final int XML_ERROR_RECURSIVE_ENTITY_REF = 12;
70   public static final int XML_ERROR_ASYNC_ENTITY = 13;
71   public static final int XML_ERROR_BAD_CHAR_REF = 14;
72   public static final int XML_ERROR_BINARY_ENTITY_REF = 15;
73   public static final int XML_ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF = 16;
74   public static final int XML_ERROR_MISPLACED_XML_PI = 17;
75   public static final int XML_ERROR_UNKNOWN_ENCODING = 18;
76   public static final int XML_ERROR_INCORRECT_ENCODING = 19;
77   public static final int XML_ERROR_UNCLOSED_CDATA_SECTION = 20;
78   public static final int XML_ERROR_EXTERNAL_ENTITY_HANDLING = 21;
79   public static final int XML_ERROR_NOT_STANDALONE = 22;
80   public static final int XML_ERROR_UNEXPECTED_STATE = 23;
81   public static final int XML_ERROR_ENTITY_DECLARED_IN_PE = 24;
82   public static final int XML_ERROR_FEATURE_REQUIRES_XML_DTD = 25;
83   public static final int XML_ERROR_CANT_CHANGE_FEATURE_ONCE_PARSING = 26;
84   public static final int XML_ERROR_UNBOUND_PREFIX = 27;
85   public static final int XML_ERROR_UNDECLARING_PREFIX = 28;
86   public static final int XML_ERROR_INCOMPLETE_PE = 29;
87   public static final int XML_ERROR_XML_DECL = 30;
88   public static final int XML_ERROR_TEXT_DECL = 31;
89   public static final int XML_ERROR_PUBLICID = 32;
90   public static final int XML_ERROR_SUSPENDED = 33;
91   public static final int XML_ERROR_NOT_SUSPENDED = 34;
92   public static final int XML_ERROR_ABORTED = 35;
93   public static final int XML_ERROR_FINISHED = 36;
94   public static final int XML_ERROR_SUSPEND_PE = 37;
95
96   public String JavaDoc []getLoadedExtensions()
97   {
98     return new String JavaDoc[] { "xml" };
99   }
100   
101   /**
102    * Converts from iso-8859-1 to utf8
103    */

104   public static Value utf8_encode(String JavaDoc str)
105   {
106     // XXX: need to make a marker for the string
107

108     return new StringValueImpl(str);
109   }
110
111   /**
112    * Converts from utf8 to iso-8859-1
113    */

114   public static Value utf8_decode(String JavaDoc str)
115   {
116     // XXX: need to make a marker for the string
117

118     return new StringValueImpl(str);
119   }
120
121   /**
122    * Returns the error code for xml parser
123    */

124   public Value xml_get_error_code(Xml parser)
125   {
126     if (parser == null)
127       return BooleanValue.FALSE;
128
129     return LongValue.create(parser.getErrorCode());
130   }
131
132   /**
133    * Returns the error string for xml parser
134    */

135   public Value xml_error_string(int code)
136   {
137     switch (code) {
138     case XML_ERROR_NONE:
139       return StringValue.create("");
140       
141     case XML_ERROR_SYNTAX:
142       return StringValue.create("syntax error");
143
144     default:
145       return BooleanValue.FALSE;
146     }
147   }
148
149   /**
150    * @see boolean Xml.xml_parse
151    *
152    * @param parser
153    * @param data
154    * @param isFinal
155    * @return false if parser == null
156    * @throws Exception
157    */

158   public boolean xml_parse(Env env,
159                @NotNull Xml parser,
160                            String JavaDoc data,
161                            @Optional("true") boolean isFinal)
162   {
163     if (parser == null)
164       return false;
165
166     try {
167       return parser.xml_parse(env, data, isFinal);
168     } catch (Exception JavaDoc e) {
169       throw QuercusModuleException.create(e);
170     }
171   }
172
173   /**
174    * returns a new Xml Parser
175    */

176   public Xml xml_parser_create(Env env,
177                                     @Optional String JavaDoc outputEncoding)
178   {
179     return new Xml(env,outputEncoding,null);
180   }
181
182   /**
183    * XXX: Should we return warning if separator is
184    * anything but ":"???
185    *
186    * @param env
187    * @param outputEncoding
188    * @param separator
189    * @return namespace aware Xml Parser
190    */

191   public Xml xml_parser_create_ns(Env env,
192                                        @Optional String JavaDoc outputEncoding,
193                                        @Optional("':'") String JavaDoc separator)
194   {
195     return new Xml(env,outputEncoding,separator);
196   }
197
198   /**
199    *
200    * @see boolean Xml.xml_parser_set_option
201    *
202    * @param parser
203    * @param option
204    * @param value
205    * @return false if parser == null
206    */

207   public boolean xml_parser_set_option(@NotNull Xml parser,
208                                        @NotNull int option,
209                                        @NotNull Value value)
210   {
211     if (parser == null)
212       return false;
213
214     return parser.xml_parser_set_option(option, value);
215   }
216
217   /**
218    * @see boolean Xml.xml_parser_get_option
219    *
220    * @param parser
221    * @param option
222    * @return false if parser == null
223    */

224   public Value xml_parser_get_option(@NotNull Xml parser,
225                                        @NotNull int option)
226   {
227     if (parser == null)
228       return BooleanValue.FALSE;
229
230     return parser.xml_parser_get_option(option);
231   }
232
233   /**
234    * @see boolean Xml.xml_set_element_handler
235    *
236    * @param parser
237    * @param startElementHandler
238    * @param endElementHandler
239    * @return false if parser == null
240    */

241   public boolean xml_set_element_handler(@NotNull Xml parser,
242                                          @NotNull Value startElementHandler,
243                                          @NotNull Value endElementHandler)
244   {
245     if (parser == null)
246       return false;
247
248     return parser.xml_set_element_handler(startElementHandler, endElementHandler);
249   }
250
251   /**
252    * @see boolean Xml.xml_set_character_data_handler
253    *
254    * @param parser
255    * @param handler
256    * @return false if parser == null
257    */

258   public boolean xml_set_character_data_handler(@NotNull Xml parser,
259                                                 @NotNull Value handler)
260   {
261     if (parser == null)
262       return false;
263
264     return parser.xml_set_character_data_handler(handler);
265   }
266
267
268   /**
269    * @see boolean Xml.xml_set_start_namespace_decl_handler
270    *
271    * @param parser
272    * @param startNamespaceDeclHandler
273    * @return false if parser == null
274    */

275   public boolean xml_set_start_namespace_decl_handler(@NotNull Xml parser,
276                                                       @NotNull Value startNamespaceDeclHandler)
277   {
278     if (parser == null)
279       return false;
280
281     return parser.xml_set_start_namespace_decl_handler(startNamespaceDeclHandler);
282   }
283
284   /**
285    *
286    * @param parser
287    * @param obj
288    * @return false if parser == null
289    */

290   public boolean xml_set_object(@NotNull Xml parser,
291                                 @NotNull Value obj)
292   {
293     if (parser == null)
294       return false;
295
296     return parser.xml_set_object(obj);
297   }
298
299   /**
300    *
301    * @param parser
302    * @param handler
303    * @return false if parser == null
304    */

305   public boolean xml_set_processing_instruction_handler(@NotNull Xml parser,
306                                                         @NotNull Value handler)
307   {
308     if (parser == null)
309       return false;
310
311     return parser.xml_set_processing_instruction_handler(handler);
312   }
313
314   /**
315    *
316    * @param parser
317    * @param handler
318    * @return false if parser == null
319    */

320   public boolean xml_set_default_handler(@NotNull Xml parser,
321                                          @NotNull Value handler)
322   {
323     if (parser == null)
324       return false;
325
326     return parser.xml_set_default_handler(handler);
327   }
328
329   /**
330    * @see boolean Xml.xml_set_notation_decl_handler
331    *
332    * @param parser
333    * @param handler
334    * @return false is parser == null
335    */

336   public boolean xml_set_notation_decl_handler(@NotNull Xml parser,
337                                                @NotNull Value handler)
338   {
339     if (parser == null)
340       return false;
341
342     return parser.xml_set_notation_decl_handler(handler);
343   }
344
345   /**
346    *
347    * @param parser
348    * @param handler
349    * @return false if parser == null
350    */

351   public boolean xml_set_end_namespace_decl_handler(@NotNull Xml parser,
352                                                     @NotNull Value handler)
353   {
354     if (parser == null)
355       return false;
356
357     return parser.xml_set_end_namespace_decl_handler(handler);
358   }
359
360   /**
361    *
362    * @param parser
363    * @param data
364    * @param valueArray
365    * @param indexArray
366    * @return false if parser == null
367    * @throws Exception
368    */

369   public int xml_parse_into_struct(@NotNull Xml parser,
370                                    @NotNull String JavaDoc data,
371                                    @Reference Value valueArray,
372                                    @Optional @Reference Value indexArray)
373   {
374     try {
375       if (parser == null)
376     return 0;
377
378       return parser.xml_parse_into_struct(data, valueArray, indexArray);
379     } catch (Exception JavaDoc e) {
380       throw QuercusModuleException.create(e);
381     }
382   }
383
384   /**
385    * stub function. parser_free taken care of by garbage collection
386    *
387    * @param parser
388    * @return false if parser == null, otherwise true
389    */

390   public boolean xml_parser_free(@NotNull Xml parser)
391   {
392     if (parser == null)
393       return false;
394     else
395       return true;
396   }
397
398   /**
399    * @see boolean Xml.xml_set_unparsed_entity_decl_handler
400    *
401    * @param parser
402    * @param handler
403    * @return false if parser == null, otherwise true
404    */

405   public boolean xml_set_unparsed_entity_decl_handler(@NotNull Xml parser,
406                                                       @NotNull Value handler)
407   {
408     if (parser == null)
409       return false;
410
411     return parser.xml_set_unparsed_entity_decl_handler(handler);
412   }
413
414   // @todo xml_error_string
415
// @todo xml_get_current_byte_index
416
// @todo xml_get_current_column_number
417
// @todo xml_get_current_line_number
418
// @todo xml_get_error_code
419
// @todo xml_set_external_entity_ref_handler
420
}
421
422
Popular Tags