KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > clif > scenario > util > isac > util > xml > IsacEntityResolver


1 /*
2  * CLIF is a Load Injection Framework
3  * Copyright (C) 2004 France Telecom R&D
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * CLIF
20  *
21  * Contact: clif@objectweb.org
22  */

23 package org.objectweb.clif.scenario.util.isac.util.xml;
24
25 import java.io.IOException JavaDoc;
26 import java.io.InputStream JavaDoc;
27
28 import org.xml.sax.EntityResolver JavaDoc;
29 import org.xml.sax.InputSource JavaDoc;
30 import org.xml.sax.SAXException JavaDoc;
31
32 /**
33  * This class is used to parse XML with sax
34  *
35  * @author JC Meillaud
36  * @author A Peyrard
37  */

38 public class IsacEntityResolver implements EntityResolver JavaDoc {
39     // attribute storing the class loader
40
private final ClassLoader JavaDoc loader;
41
42     private static final String JavaDoc CLASSPATH = "classpath://";
43
44     private static final String JavaDoc FILE = "file:///";
45
46     /**
47      * Constructor
48      *
49      * @param loader
50      * The class loader
51      */

52     public IsacEntityResolver(ClassLoader JavaDoc loader) {
53         super();
54         this.loader = loader;
55     }
56
57     /**
58      * @see org.xml.sax.EntityResolver#resolveEntity(java.lang.String,
59      * java.lang.String)
60      */

61     public InputSource JavaDoc resolveEntity(String JavaDoc publicId, String JavaDoc systemId)
62             throws SAXException JavaDoc, IOException JavaDoc {
63         if (systemId != null) {
64             if (systemId.startsWith(CLASSPATH)) {
65                 // Search for DTD
66
ClassLoader JavaDoc classLoader = this.getClass().getClassLoader();
67                 InputStream JavaDoc dtdStream = classLoader
68                         .getResourceAsStream(systemId.substring(CLASSPATH.length()));
69                 if (dtdStream == null) {
70                     System.out.println(systemId + " not found in classpath");
71                     return null;
72                 } else {
73                     InputSource JavaDoc source = new InputSource JavaDoc(dtdStream);
74                     source.setPublicId(publicId);
75                     source.setSystemId(systemId);
76                     return source;
77                 }
78             } else if (systemId.startsWith(FILE)) {
79                 InputStream JavaDoc childStream = loader.getResourceAsStream(systemId
80                         .substring(FILE.length()));
81                 if (childStream == null)
82                     return null;
83                 else {
84                     InputSource JavaDoc source = new InputSource JavaDoc(childStream);
85                     source.setPublicId(publicId);
86                     source.setSystemId(systemId);
87                     return source;
88                 }
89             } else
90                 return null;
91         } else {
92             // use the default behaviour
93
return null;
94         }
95     }
96 }
Popular Tags