1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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 /***
32 * TransformDTDEntityResolver
33 *
34 * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
35 * @version $Id: TransformDTDEntityResolver.java 516448 2007-03-09 16:25:47Z ate $
36 */
37 public class TransformDTDEntityResolver implements EntityResolver
38 {
39 private Map dtds;
40
41 public TransformDTDEntityResolver(Map dtds)
42 {
43 this.dtds = dtds;
44 }
45
46
47
48
49 public InputSource resolveEntity(String publicId, String systemId)
50 throws SAXException, IOException
51 {
52 try
53 {
54
55
56 byte[] dtd = (byte[])dtds.get(systemId);
57 if (dtd == null)
58 {
59 ByteArrayOutputStream baos = new ByteArrayOutputStream();
60 URL url = new URL(systemId);
61 Streams.drain(url.openStream(), baos);
62 dtd = baos.toByteArray();
63 synchronized(dtds)
64 {
65 dtds.put(systemId, dtd);
66 }
67 }
68
69 if (dtd != null)
70 {
71 ByteArrayInputStream bais = new ByteArrayInputStream(dtd);
72 InputSource is = new InputSource(bais);
73 is.setPublicId( publicId );
74 is.setSystemId( systemId );
75
76 return is;
77 }
78
79 }
80 catch(Throwable t )
81 {
82 t.printStackTrace();
83 }
84
85 return null;
86
87 }
88 }