View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      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  /***
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      /* (non-Javadoc)
47       * @see org.xml.sax.EntityResolver#resolveEntity(java.lang.String, java.lang.String)
48       */
49      public InputSource resolveEntity(String publicId, String systemId)
50          throws SAXException, IOException
51      {        
52          try 
53          {
54              // System.out.println("TSER: ( " + publicId  + " Taking " + systemId + " from cache");
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 ) // java.io.IOException x  
81          {
82              t.printStackTrace();
83          }
84              
85          return null;
86          
87      }
88  }