KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > util > SchemaUtil


1 /*******************************************************************************
2  * Copyright (c) 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.pde.internal.core.util;
13
14 import java.io.BufferedInputStream JavaDoc;
15 import java.io.FileInputStream JavaDoc;
16 import java.io.IOException JavaDoc;
17 import java.io.InputStream JavaDoc;
18 import java.net.MalformedURLException JavaDoc;
19 import java.net.URL JavaDoc;
20
21 import javax.xml.parsers.FactoryConfigurationError JavaDoc;
22 import javax.xml.parsers.ParserConfigurationException JavaDoc;
23
24 import org.eclipse.pde.internal.core.PDECore;
25 import org.xml.sax.SAXException JavaDoc;
26 import org.xml.sax.helpers.DefaultHandler JavaDoc;
27
28 /**
29  * SchemaUtil
30  *
31  */

32 public class SchemaUtil {
33     
34     public static InputStream JavaDoc getInputStream(URL JavaDoc url) throws IOException JavaDoc {
35         if (url == null) {
36             throw new MalformedURLException JavaDoc("URL specified is null"); //$NON-NLS-1$
37
} else if ("file".equals(url.getProtocol())) { //$NON-NLS-1$
38
return new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(url.getFile()));
39         }
40         return new BufferedInputStream JavaDoc(url.openStream());
41     }
42
43     public static void parseURL(URL JavaDoc url, DefaultHandler JavaDoc handler) {
44         InputStream JavaDoc input = null;
45         try {
46             input = getInputStream(url);
47             SAXParserWrapper parser = new SAXParserWrapper();
48             parser.parse(input, handler);
49         } catch (MalformedURLException JavaDoc e) {
50             // Ignore
51
// Caused when URL is null
52
// This occurs when the extension point schema is first
53
// created.
54
} catch (IOException JavaDoc e) {
55             PDECore.logException(e);
56         } catch (SAXException JavaDoc e) {
57             // Ignore parse errors
58
// Handler may send a SAX Exception to prematurely abort parsing
59
// in order to save execution time. This is not an error
60
} catch (ParserConfigurationException JavaDoc e) {
61             PDECore.logException(e);
62         } catch (FactoryConfigurationError JavaDoc e) {
63             PDECore.logException(e);
64         } finally {
65             try {
66                 if (input != null)
67                     input.close();
68             } catch (IOException JavaDoc e1) {
69             }
70         }
71     }
72
73 }
74
Popular Tags