KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > sp > util > XMLUtilities


1 /*
2  * StandardUtilities.java - Miscelaneous XML utility functions.
3  * :tabSize=8:indentSize=8:noTabs=false:
4  * :folding=explicit:collapseFolds=1:
5  *
6  * Copyright (C) 1999, 2006 Marcelo Vanzin, Slava Pestov
7  * Portions copyright (C) 2000 Richard S. Hall
8  * Portions copyright (C) 2001 Dirk Moebius
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23  */

24
25 package org.gjt.sp.util;
26
27 import java.io.BufferedReader JavaDoc;
28 import java.io.InputStream JavaDoc;
29 import java.io.InputStreamReader JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.io.Reader JavaDoc;
32
33 import org.xml.sax.InputSource JavaDoc;
34 import org.xml.sax.SAXException JavaDoc;
35 import org.xml.sax.SAXParseException JavaDoc;
36 import org.xml.sax.XMLReader JavaDoc;
37 import org.xml.sax.helpers.DefaultHandler JavaDoc;
38 import org.xml.sax.helpers.XMLReaderFactory JavaDoc;
39
40 /**
41  * XML utility methods that only depend on the JDK.
42  *
43  * @author Marcelo Vanzin
44  * @version $Id: XMLUtilities.java 5573 2006-07-13 04:37:32Z vanza $
45  * @since 4.3pre6
46  */

47 public class XMLUtilities
48 {
49
50     //{{{ charsToEntities() method
51
/**
52      * Converts <, >, & in the string to their HTML entity
53      * equivalents.
54      *
55      * <p>If <code>xml11</code> is true, then character entities
56      * are used to convert illegal XML characters (mainly ASCII
57      * control characters).</p>
58      *
59      * @param str The string
60      * @param xml11 Whether to allow XML 1.1 constructs.
61      */

62     public static String JavaDoc charsToEntities(String JavaDoc str, boolean xml11)
63     {
64         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(str.length());
65         for(int i = 0; i < str.length(); i++)
66         {
67             char ch = str.charAt(i);
68
69             // control characters, excluding \t, \r and \n
70
if (xml11 && ch < 32 && ch != '\r' && ch != '\n' && ch != '\t')
71             {
72                 buf.append("&#").append((int)ch).append(";");
73                 continue;
74             }
75
76             switch(ch)
77             {
78             case '<':
79                 buf.append("&lt;");
80                 break;
81             case '>':
82                 buf.append("&gt;");
83                 break;
84             case '&':
85                 buf.append("&amp;");
86                 break;
87             default:
88                 buf.append(ch);
89                 break;
90             }
91         }
92         return buf.toString();
93     } //}}}
94

95     //{{{ parseXML() method
96
/**
97      * Convenience method for parsing an XML file. This method will
98      * wrap the resource in an InputSource and set the source's
99      * systemId to "jedit.jar" (so the source should be able to
100      * handle any external entities by itself).
101      *
102      * <p>SAX Errors are caught and are not propagated to the caller;
103      * instead, an error message is printed to jEdit's activity
104      * log. So, if you need custom error handling, <b>do not use
105      * this method</b>.
106      *
107      * <p>The given stream is closed before the method returns,
108      * regardless whether there were errors or not.</p>
109      *
110      * @return Whether any error occured during parsing.
111      */

112     public static boolean parseXML(InputStream JavaDoc in, DefaultHandler JavaDoc handler)
113         throws IOException JavaDoc
114     {
115         Reader JavaDoc r = null;
116         try
117         {
118             XMLReader JavaDoc parser = XMLReaderFactory.createXMLReader();
119             r = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(in));
120             InputSource JavaDoc isrc = new InputSource JavaDoc(r);
121             isrc.setSystemId("jedit.jar");
122             parser.setContentHandler(handler);
123             parser.setDTDHandler(handler);
124             parser.setEntityResolver(handler);
125             parser.setErrorHandler(handler);
126             parser.parse(isrc);
127         }
128         catch(SAXParseException JavaDoc se)
129         {
130             int line = se.getLineNumber();
131             String JavaDoc message = se.getMessage();
132             Log.log(Log.ERROR,XMLUtilities.class,
133                 "while parsing from " + in + ": SAXParseException: line " + line + ": " , se);
134             return true;
135         }
136         catch(SAXException JavaDoc e)
137         {
138             Log.log(Log.ERROR,XMLUtilities.class,e);
139             return true;
140         }
141         finally
142         {
143             try
144             {
145                 if(in != null)
146                     in.close();
147             }
148             catch(IOException JavaDoc io)
149             {
150                 Log.log(Log.ERROR,XMLUtilities.class,io);
151             }
152         }
153         return false;
154     } //}}}
155

156     //{{{ resolveEntity() method
157
/**
158      * Tries to find the given systemId in the context of the given
159      * class. If the given systemId ends with the given test string,
160      * then try to load a resource using the Class's
161      * <code>getResourceAsStream()</code> method using the test string
162      * as the resource.
163      *
164      * <p>This is used a lot internally while parsing XML files used
165      * by jEdit, but anyone is free to use the method if it sounds
166      * usable.</p>
167      */

168     public static InputSource JavaDoc findEntity(String JavaDoc systemId, String JavaDoc test, Class JavaDoc where)
169     {
170         if (systemId != null && systemId.endsWith(test))
171         {
172             try
173             {
174                 return new InputSource JavaDoc(new BufferedReader JavaDoc(
175                     new InputStreamReader JavaDoc(
176                         where.getResourceAsStream(test))));
177             }
178             catch (Exception JavaDoc e)
179             {
180                 Log.log(Log.ERROR,XMLUtilities.class,
181                     "Error while opening " + test + ":");
182                 Log.log(Log.ERROR,XMLUtilities.class,e);
183             }
184         }
185
186         return null;
187     } //}}}
188

189     private XMLUtilities() { }
190 }
191
192
Popular Tags