Clover Coverage Report - XWiki Rendering - Parent POM 4.0-SNAPSHOT (Aggregated)
Coverage timestamp: Mon Mar 12 2012 18:03:13 CET
../../../../../../img/srcFileCovDistChart6.png 81% of files have more coverage
16   152   6   16
6   66   0.38   1
1     6  
1    
 
  LocalEntityResolver       Line # 44 16 0% 6 11 52.2% 0.5217391
 
  (219)
 
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.rendering.wikimodel.xhtml.impl;
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    import java.util.logging.Level;
28    import java.util.logging.Logger;
29   
30    import org.xml.sax.EntityResolver;
31    import org.xml.sax.InputSource;
32    import org.xml.sax.SAXException;
33   
34    /**
35    * Entity resolver that resolves entities using entity files (like
36    * xhtml-symbol.ent, xhtml-special.ent, xhtml-lat1.ent) located on the file
37    * system (in the classpath). This allows an XML parser that uses this entity
38    * resolver to work even when there's no internet connection. It also speeds up
39    * the entity resolution.
40    *
41    * @version $Id: 118444a29b3ccf148370427db2521dad5425b014 $
42    * @since 4.0M1
43    */
 
44    public class LocalEntityResolver implements EntityResolver
45    {
46    private final static Logger log = Logger
47    .getLogger(LocalEntityResolver.class.getName());
48   
49    /**
50    * Allow the application to resolve external entities.
51    * <p/>
52    * <p>
53    * The Parser will call this method before opening any external entity
54    * except the top-level document entity including the external DTD subset,
55    * external entities referenced within the DTD, and external entities
56    * referenced within the document element): the application may request that
57    * the parser resolve the entity itself, that it use an alternative URI, or
58    * that it use an entirely different input source.
59    * </p>
60    * <p/>
61    * <p>
62    * Application writers can use this method to redirect external system
63    * identifiers to secure and/or local URIs, to look up public identifiers in
64    * a catalogue, or to read an entity from a database or other input source
65    * (including, for example, a dialog box).
66    * </p>
67    * <p/>
68    * <p>
69    * If the system identifier is a URL, the SAX parser must resolve it fully
70    * before reporting it to the application.
71    * </p>
72    *
73    * @param publicId The public identifier of the external entity being
74    * referenced, or null if none was supplied.
75    * @param systemId The system identifier of the external entity being
76    * referenced.
77    * @return An InputSource object describing the new input source, or null to
78    * request that the parser open a regular URI connection to the
79    * system identifier.
80    * @throws org.xml.sax.SAXException Any SAX exception, possibly wrapping
81    * another exception.
82    * @throws java.io.IOException A Java-specific IO exception, possibly the
83    * result of creating a new InputStream or Reader for the
84    * InputSource.
85    * @see org.xml.sax.InputSource
86    */
 
87  876 toggle public InputSource resolveEntity(String publicId, String systemId)
88    throws SAXException,
89    IOException
90    {
91  876 InputSource source = null;
92   
93  876 try {
94  876 URI uri = new URI(systemId);
95   
96  876 if ("http".equals(uri.getScheme())
97    || "file".equals(uri.getScheme()))
98    {
99  876 String filename = (new File(uri.getPath())).getName();
100  876 InputStream istream = getClass()
101    .getClassLoader()
102    .getResourceAsStream(filename);
103  876 if (istream != null) {
104  876 source = new InputSource(istream);
105    } else {
106  0 log.warning(String.format(
107    "Failed to load resource [%s] locally. "
108    + "Will try to get it online at [%s]",
109    filename,
110    systemId));
111    }
112    } else {
113    // As there's no scheme we'll assume that it's an already
114    // resolved systemId that is
115    // passed. This happens when a DTD file uses a relative systemId
116    // for dependent
117    // entity files. For example the default xhtml1-strict.dtd and
118    // xhtml1-transitional.dtd files reference xhtml-lat1.ent,
119    // xhtml-special.ent and
120    // xhtml1-symbol.ent relatively. Normally these relative
121    // declarations generate a
122    // URL with a "file" scheme but apparently there are some cases
123    // when the raw
124    // entity file names is passed to this resolveEntity method...
125  0 log
126    .fine(String
127    .format(
128    "Unknown URI scheme [%s] for entity [%s]. "
129    + "Assuming the entity is already resolved and looking for it in the file system.",
130    uri.getScheme(),
131    systemId));
132  0 InputStream istream = getClass()
133    .getClassLoader()
134    .getResourceAsStream(systemId);
135  0 if (istream != null) {
136  0 source = new InputSource(istream);
137    } else {
138  0 log.fine("Failed to load resource ["
139    + systemId
140    + "] locally.");
141    }
142    }
143    } catch (URISyntaxException e) {
144  0 log.log(
145    Level.WARNING,
146    String.format("Invalid URI [%s]", systemId),
147    e);
148    }
149    // Returning null causes the caller to try accessing the entity online
150  876 return source;
151    }
152    }