KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > quercus > lib > simplexml > SimpleXMLModule


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  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Charles Reich
27  */

28
29 package com.caucho.quercus.lib.simplexml;
30
31 import com.caucho.quercus.annotation.NotNull;
32 import com.caucho.quercus.annotation.Optional;
33 import com.caucho.quercus.env.Env;
34 import com.caucho.quercus.module.AbstractQuercusModule;
35 import com.caucho.util.L10N;
36 import com.caucho.vfs.Path;
37
38 import org.w3c.dom.Document JavaDoc;
39
40 import javax.xml.parsers.DocumentBuilder JavaDoc;
41 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
42 import java.io.InputStream JavaDoc;
43 import java.util.logging.Level JavaDoc;
44 import java.util.logging.Logger JavaDoc;
45
46 /**
47  * PHP SimpleXML
48  */

49 public class SimpleXMLModule
50   extends AbstractQuercusModule
51 {
52   private static final Logger JavaDoc log
53     = Logger.getLogger(SimpleXMLModule.class.getName());
54   private static final L10N L = new L10N(SimpleXMLModule.class);
55
56   public SimpleXMLElement simplexml_load_string(Env env,
57                                                 InputStream JavaDoc data,
58                                                 @Optional String JavaDoc className,
59                                                 @Optional int options)
60   {
61     DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
62     
63     try {
64       DocumentBuilder JavaDoc builder = factory.newDocumentBuilder();
65
66       Document JavaDoc document = builder.parse(data);
67       
68       return new SimpleXMLElement(env, document, document.getDocumentElement());
69     } catch (Exception JavaDoc e) {
70       log.log(Level.FINE, L.l(e.toString()), e);
71       return null;
72     }
73   }
74
75   public SimpleXMLElement simplexml_load_file(Env env,
76                                               @NotNull Path file,
77                                               @Optional String JavaDoc className,
78                                               @Optional int options)
79   {
80     DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
81     
82     try {
83       DocumentBuilder JavaDoc builder = factory.newDocumentBuilder();
84       Document JavaDoc document = builder.parse(file.openRead());
85       
86       return new SimpleXMLElement(env, document, document.getDocumentElement());
87     } catch (Exception JavaDoc e) {
88       log.log(Level.FINE, L.l(e.toString()), e);
89       return null;
90     }
91   }
92
93   //@todo simplexml_import_dom -- Skip until (XXX. DOM Functions implemented)
94
}
95
Popular Tags