1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 /*
24  * Created on 27.01.2005
25  *
26  */
27 package com.sun.star.tooling.converter;
28 
29 import java.io.IOException;
30 import java.io.InputStream;
31 
32 import org.xml.sax.EntityResolver;
33 import org.xml.sax.InputSource;
34 import org.xml.sax.SAXException;
35 
36 /**
37  * Resolve external entities by provide the
38  * XLIFF DTD stored in the jar file
39  */
40 public class Resolver implements EntityResolver {
41 
42     /**
43      * Resolver constructor.
44      */
Resolver()45     public Resolver() {
46     }
47 
48     /**
49      * Allow the application to resolve external entities.
50      *
51      * The Parser will call this method before opening any external entity
52      * except the top-level document entity (including the external DTD subset,
53      * external entities referenced within the DTD, and external entities
54      * referenced within the document element): the application may request that
55      * the parser resolve the entity itself, that it use an alternative URI, or
56      * that it use an entirely different input source.
57      */
58 
59     /* (non-Javadoc)
60      * @see org.xml.sax.EntityResolver#resolveEntity(java.lang.String, java.lang.String)
61      */
62 
resolveEntity(String publicId, String systemId)63     public InputSource resolveEntity(String publicId, String systemId)
64             throws SAXException, IOException {
65 
66         if ((publicId != null) && (publicId.equals("-//XLIFF//DTD XLIFF//EN"))) {
67             systemId = "dtd:///xliff.dtd";
68         }
69         if (systemId != null) {
70             if (systemId.startsWith("dtd://")) {
71                 String dtd = "com/sun/star/tooling/converter/dtd"
72                         + systemId.substring(6);
73                 ClassLoader cl = this.getClass().getClassLoader();
74                 InputStream in = cl.getResourceAsStream(dtd);
75                 InputSource ins = new InputSource(in);
76                 ins.setSystemId(systemId);
77                 return ins;
78             } /*
79                * else if ( systemId.startsWith("jar:") ) { try { URL url=new
80                * URL(systemId); JarURLConnection jarConn =
81                * (JarURLConnection)url.openConnection(); InputSource ins=new
82                * InputSource(jarConn.getInputStream());
83                * ins.setSystemId(systemId); return ins; }
84                * catch(MalformedURLException me){ throw new SAXException(me);
85                * Incorrect URL format used } }
86                */
87         }
88         return null;
89     }
90 
91 }