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: package org.apache.portals.applications.transform.impl;
18:
19: import java.io.ByteArrayInputStream;
20: import java.io.ByteArrayOutputStream;
21: import java.io.IOException;
22: import java.net.URL;
23: import java.util.Map;
24:
25: import org.apache.portals.applications.util.Streams;
26: import org.xml.sax.EntityResolver;
27: import org.xml.sax.InputSource;
28: import org.xml.sax.SAXException;
29:
30: /**
31: * TransformDTDEntityResolver
32: *
33: * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
34: * @version $Id: TransformDTDEntityResolver.java 516448 2007-03-09 16:25:47Z ate $
35: */
36: public class TransformDTDEntityResolver implements EntityResolver {
37: private Map dtds;
38:
39: public TransformDTDEntityResolver(Map dtds) {
40: this .dtds = dtds;
41: }
42:
43: /* (non-Javadoc)
44: * @see org.xml.sax.EntityResolver#resolveEntity(java.lang.String, java.lang.String)
45: */
46: public InputSource resolveEntity(String publicId, String systemId)
47: throws SAXException, IOException {
48: try {
49: // System.out.println("TSER: ( " + publicId + " Taking " + systemId + " from cache");
50:
51: byte[] dtd = (byte[]) dtds.get(systemId);
52: if (dtd == null) {
53: ByteArrayOutputStream baos = new ByteArrayOutputStream();
54: URL url = new URL(systemId);
55: Streams.drain(url.openStream(), baos);
56: dtd = baos.toByteArray();
57: synchronized (dtds) {
58: dtds.put(systemId, dtd);
59: }
60: }
61:
62: if (dtd != null) {
63: ByteArrayInputStream bais = new ByteArrayInputStream(
64: dtd);
65: InputSource is = new InputSource(bais);
66: is.setPublicId(publicId);
67: is.setSystemId(systemId);
68:
69: return is;
70: }
71:
72: } catch (Throwable t) // java.io.IOException x
73: {
74: t.printStackTrace();
75: }
76:
77: return null;
78:
79: }
80: }
|