1:
37:
38: package ;
39:
40: import ;
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48: import ;
49: import ;
50:
51: import ;
52: import ;
53: import ;
54: import ;
55: import ;
56:
57: import ;
58:
59: import ;
60:
61: import ;
62: import ;
63:
64:
67: public final class XMLJ
68: {
69:
70: static class XMLJShutdownHook
71: implements Runnable
72: {
73:
74: public void run ()
75: {
76:
77: System.gc ();
78: Runtime.getRuntime ().runFinalization ();
79:
80:
81: GnomeTransformerFactory.freeLibxsltGlobal ();
82: }
83:
84: }
85:
86: private static boolean initialised = false;
87:
88: public static void init ()
89: {
90: if (!initialised)
91: {
92: System.loadLibrary ("xmlj");
93:
94: XMLJShutdownHook hook = new XMLJShutdownHook ();
95: Runtime.getRuntime ().addShutdownHook (new Thread (hook));
96: }
97: initialised = true;
98: }
99:
100: private static final int LOOKAHEAD = 50;
101:
102:
107: public static NamedInputStream getInputStream (InputSource input)
108: throws IOException
109: {
110: InputStream in = input.getByteStream ();
111: String systemId = input.getSystemId ();
112: if (in == null)
113: {
114: Reader r = input.getCharacterStream();
115: if (r != null)
116: in = new ReaderInputStream(r);
117: }
118: if (in == null)
119: {
120: in = getInputStream(systemId);
121: }
122: return new NamedInputStream (systemId, in, LOOKAHEAD);
123: }
124:
125:
130: public static NamedInputStream getInputStream (Source source)
131: throws IOException
132: {
133: if (source instanceof SAXSource)
134: {
135: return getInputStream (((SAXSource) source).getInputSource ());
136: }
137: InputStream in = null;
138: String systemId = source.getSystemId ();
139: if (source instanceof StreamSource)
140: {
141: in = ((StreamSource) source).getInputStream ();
142: }
143: if (in == null)
144: {
145: in = getInputStream(systemId);
146: }
147: return new NamedInputStream (systemId, in, LOOKAHEAD);
148: }
149:
150: private static InputStream getInputStream(String systemId)
151: throws IOException
152: {
153: if (systemId == null)
154: {
155: throw new IOException("no system ID");
156: }
157: try
158: {
159: return new URL(systemId).openStream();
160: }
161: catch (MalformedURLException e)
162: {
163: return new FileInputStream(systemId);
164: }
165: }
166:
167:
172: public static NamedInputStream getInputStream (URL url)
173: throws IOException
174: {
175: return new NamedInputStream (url.toString (), url.openStream(),
176: LOOKAHEAD);
177: }
178:
179:
182: static NamedInputStream xmljGetInputStream(String base, String url)
183: throws IOException
184: {
185: try
186: {
187: if (base != null)
188: {
189: url = new URL(new URL(base), url).toString();
190: }
191: }
192: catch (MalformedURLException e)
193: {
194: }
195: InputStream in = getInputStream(url);
196: return new NamedInputStream(url, in, LOOKAHEAD);
197: }
198:
199:
202: public static OutputStream getOutputStream (Result result)
203: throws IOException
204: {
205: OutputStream out = null;
206: if (result instanceof StreamResult)
207: {
208: out = ((StreamResult) result).getOutputStream ();
209: }
210: if (out == null)
211: {
212: Writer w = ((StreamResult) result).getWriter ();
213: if (w != null)
214: out = new WriterOutputStream (w);
215: }
216: if (out == null)
217: {
218: String systemId = result.getSystemId ();
219: if (systemId == null)
220: {
221: throw new IOException ("no system ID");
222: }
223: try
224: {
225: URL url = new URL (systemId);
226: URLConnection connection = url.openConnection ();
227: connection.setDoOutput (true);
228: out = connection.getOutputStream ();
229: }
230: catch (MalformedURLException e)
231: {
232: out = new FileOutputStream (systemId);
233: }
234: }
235:
236: return out;
237: }
238:
239:
244: public static String getAbsoluteURI (String base, String uri)
245: {
246: if (uri != null &&
247: base != null &&
248: (uri.length() > 0) &&
249: (uri.indexOf(':') == -1) &&
250: (uri.charAt(0) != '/'))
251: {
252:
253: if (base.charAt(base.length() - 1) != '/')
254: {
255: int i = base.lastIndexOf('/');
256: base = base.substring(0, i + 1);
257: }
258: return base + uri;
259: }
260: else
261: {
262:
263: return uri;
264: }
265: }
266:
267: public static String getBaseURI(String uri)
268: {
269: if (uri != null)
270: {
271: int si = uri.lastIndexOf('/');
272: if (si != -1)
273: {
274: uri = uri.substring(0, si + 1);
275: }
276: }
277: return uri;
278: }
279:
280: }