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: */package org.apache.openejb.config;
17:
18: import java.io.BufferedInputStream;
19: import java.io.ByteArrayInputStream;
20: import java.io.ByteArrayOutputStream;
21: import java.io.IOException;
22: import java.io.InputStream;
23: import java.net.URL;
24: import java.util.HashMap;
25:
26: import org.xml.sax.EntityResolver;
27: import org.xml.sax.InputSource;
28: import org.xml.sax.SAXException;
29: import org.apache.xbean.finder.ResourceFinder;
30:
31: public class DTDResolver implements EntityResolver {
32: public static HashMap dtds = new HashMap();
33:
34: static {
35: byte[] bytes = getDtd("ejb-jar_1_1.dtd");
36: if (bytes != null) {
37: dtds.put("ejb-jar.dtd", bytes);
38: dtds.put("ejb-jar_1_1.dtd", bytes);
39: }
40: bytes = getDtd("ejb-jar_2_0.dtd");
41: if (bytes != null) {
42: dtds.put("ejb-jar_2_0.dtd", bytes);
43: }
44: }
45:
46: public static byte[] getDtd(String dtdName) {
47: try {
48:
49: ResourceFinder finder = new ResourceFinder("schema/");
50: URL dtd = finder.find(dtdName);
51: InputStream in = dtd.openStream();
52: if (in == null)
53: return null;
54:
55: byte[] buf = new byte[512];
56:
57: in = new BufferedInputStream(in);
58: ByteArrayOutputStream out = new ByteArrayOutputStream();
59:
60: int count;
61: while ((count = in.read(buf)) > -1)
62: out.write(buf, 0, count);
63:
64: in.close();
65: out.close();
66:
67: return out.toByteArray();
68: } catch (Throwable e) {
69: return null;
70: }
71: }
72:
73: public InputSource resolveEntity(String publicId, String systemId)
74: throws SAXException, IOException {
75:
76: int pos = systemId.lastIndexOf('/');
77: if (pos != -1) {
78: systemId = systemId.substring(pos + 1);
79: }
80:
81: byte[] data = (byte[]) dtds.get(systemId);
82:
83: if (data != null) {
84: return new InputSource(new ByteArrayInputStream(data));
85: } else {
86: return null;
87: }
88: }
89: }
|