01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: *
17: * $Header:$
18: */
19: package org.apache.beehive.netui.compiler;
20:
21: import org.xml.sax.EntityResolver;
22: import org.xml.sax.InputSource;
23: import org.xml.sax.SAXException;
24:
25: import java.io.IOException;
26: import java.io.InputStream;
27:
28: /**
29: * Entity resolver that tries to find the resource locally (from classloader, under
30: * org/apache/beehive/netui/compiler/resources) before trying to resolve via the network.
31: */
32: public class LocalFileEntityResolver implements EntityResolver {
33: private static final String RESORUCE_PATH_PREFIX = "org/apache/beehive/netui/compiler/resources/";
34: private static final LocalFileEntityResolver INSTANCE = new LocalFileEntityResolver();
35:
36: protected LocalFileEntityResolver() {
37: }
38:
39: public static LocalFileEntityResolver getInstance() {
40: return INSTANCE;
41: }
42:
43: /**
44: * Resolve the entity. First try to find it locally, then fallback to the network.
45: */
46: public InputSource resolveEntity(String publicID, String systemID)
47: throws SAXException, IOException {
48: InputSource localFileInput = resolveLocalEntity(systemID);
49: return localFileInput != null ? localFileInput
50: : new InputSource(systemID);
51: }
52:
53: /**
54: * Resolve the given entity locally.
55: */
56: public InputSource resolveLocalEntity(String systemID)
57: throws SAXException, IOException {
58: String localFileName = systemID;
59: int fileNameStart = localFileName.lastIndexOf('/') + 1;
60: if (fileNameStart < localFileName.length()) {
61: localFileName = systemID.substring(fileNameStart);
62: }
63: ClassLoader cl = LocalFileEntityResolver.class.getClassLoader();
64: InputStream stream = cl
65: .getResourceAsStream(RESORUCE_PATH_PREFIX
66: + localFileName);
67: if (stream != null) {
68: return new InputSource(stream);
69: }
70: return null;
71: }
72: }
|