1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.xml.internal

File LocalEntityResolver.java

 

Coverage histogram

../../../../img/srcFileCovDistChart6.png
72% of files have more coverage

Code metrics

6
16
1
1
119
51
6
0.38
16
1
6

Classes

Class Line # Actions
LocalEntityResolver 46 16 0% 6 11
0.521739152.2%
 

Contributing tests

This file is covered by 298 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.xml.internal;
21   
22    import java.io.File;
23    import java.io.IOException;
24    import java.io.InputStream;
25    import java.net.URI;
26    import java.net.URISyntaxException;
27   
28    import javax.inject.Inject;
29    import javax.inject.Singleton;
30   
31    import org.slf4j.Logger;
32    import org.xml.sax.InputSource;
33    import org.xml.sax.SAXException;
34    import org.xwiki.component.annotation.Component;
35    import org.xwiki.xml.EntityResolver;
36   
37    /**
38    * Entity resolver that resolves entities using entity files (like xhtml-symbol.ent, xhtml-special.ent, xhtml-lat1.ent)
39    * located on the file system (in the classpath). This allows an XML parser that uses this entity resolver to work even
40    * when there's no internet connection. It also speeds up the entity resolution.
41    *
42    * @version $Id: 2c91f8dbdef9aac67ff9cec8f1319da6a62fb829 $
43    */
44    @Component
45    @Singleton
 
46    public class LocalEntityResolver implements EntityResolver
47    {
48    /**
49    * The logger to use for logging.
50    */
51    @Inject
52    private Logger logger;
53   
54    /**
55    * Allow the application to resolve external entities.
56    * <p/>
57    * <p>The Parser will call this method before opening any external entity except the top-level document entity
58    * including the external DTD subset, external entities referenced within the DTD, and external entities referenced
59    * within the document element): the application may request that the parser resolve the entity itself, that it use
60    * an alternative URI, or that it use an entirely different input source.</p>
61    * <p/>
62    * <p>Application writers can use this method to redirect external system identifiers to secure and/or local URIs,
63    * to look up public identifiers in a catalogue, or to read an entity from a database or other input source
64    * (including, for example, a dialog box).</p>
65    * <p/>
66    * <p>If the system identifier is a URL, the SAX parser must resolve it fully before reporting it to the
67    * application.</p>
68    *
69    * @param publicId The public identifier of the external entity being referenced, or null if none was supplied.
70    * @param systemId The system identifier of the external entity being referenced.
71    * @return An InputSource object describing the new input source, or null to request that the parser open a regular
72    * URI connection to the system identifier.
73    * @throws org.xml.sax.SAXException Any SAX exception, possibly wrapping another exception.
74    * @throws java.io.IOException A Java-specific IO exception, possibly the result of creating a new InputStream or
75    * Reader for the InputSource.
76    * @see org.xml.sax.InputSource
77    */
 
78  1216 toggle @Override
79    public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException
80    {
81  1216 InputSource source = null;
82   
83  1216 try {
84  1216 URI uri = new URI(systemId);
85   
86  1216 if ("http".equals(uri.getScheme()) || "file".equals(uri.getScheme())) {
87  1216 String filename = new File(uri.getPath()).getName();
88  1216 InputStream istream = getClass().getClassLoader().getResourceAsStream(filename);
89  1216 if (istream != null) {
90  1216 source = new InputSource(istream);
91    } else {
92  0 this.logger.warn("Failed to load resource [{}] locally. "
93    + "Will try to get it online at [{}]", filename, systemId);
94    }
95    } else {
96    // As there's no scheme we'll assume that it's an already resolved systemId that is
97    // passed. This happens when a DTD file uses a relative systemId for dependent
98    // entity files. For example the default xhtml1-strict.dtd and
99    // xhtml1-transitional.dtd files reference xhtml-lat1.ent, xhtml-special.ent and
100    // xhtml1-symbol.ent relatively. Normally these relative declarations generate a
101    // URL with a "file" scheme but apparently there are some cases when the raw
102    // entity file names is passed to this resolveEntity method...
103  0 this.logger.debug("Unknown URI scheme [{}] for entity [{}]. "
104    + "Assuming the entity is already resolved and looking for it in the file system.",
105    uri.getScheme(), systemId);
106  0 InputStream istream = getClass().getClassLoader().getResourceAsStream(systemId);
107  0 if (istream != null) {
108  0 source = new InputSource(istream);
109    } else {
110  0 this.logger.warn("Failed to load resource [{}]", systemId);
111    }
112    }
113    } catch (URISyntaxException e) {
114  0 this.logger.warn("Invalid URI [{}]. Reason [{}]", systemId, e.getMessage());
115    }
116    // Returning null causes the caller to try accessing the entity online
117  1216 return source;
118    }
119    }