| 1 | /** |
| 2 | * BSD-style license; for more info see http://xradar.sourceforge.net/license.html |
| 3 | */ |
| 4 | package org.sourceforge.xradar.resolver; |
| 5 | |
| 6 | import java.util.regex.Pattern; |
| 7 | |
| 8 | import org.apache.xml.resolver.tools.CatalogResolver; |
| 9 | import org.sourceforge.xradar.util.XRadarUtils; |
| 10 | |
| 11 | import javax.xml.transform.Source; |
| 12 | import javax.xml.transform.TransformerException; |
| 13 | |
| 14 | /** |
| 15 | * @author pfouquet |
| 16 | * @author Romain PELISSE <belaran@gmail.com> |
| 17 | * |
| 18 | * <p>Apache CatalogResolver extension to fix a path-related issue on Windows system.</p> |
| 19 | * |
| 20 | * <p>Such a path D:\temp\test.xml would be converted in file:///D:/temp/test.xml</p> |
| 21 | */ |
| 22 | public class XRadarCatalogResolver extends CatalogResolver { |
| 23 | |
| 24 | private static final String FILE_PROTOCOL = "file:"; |
| 25 | private static final Pattern pattern = Pattern.compile("^[A-Za-z]:.*$"); |
| 26 | |
| 27 | public XRadarCatalogResolver() { |
| 28 | super(false); |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * {@inheritDoc} |
| 33 | * |
| 34 | * <p>Test if the href contains a win32 file path, beginning with a hard drive letter, such as:</p> |
| 35 | * <code>D:\\path\\to\\file</code> |
| 36 | * <p>If so, the method will normalize it using XRadarUtils.normalizeFileName method.</p> |
| 37 | * |
| 38 | * @see org.sourceforge.xradar.utils.XRadarUtils#normalizeFileName(java.lang.String) |
| 39 | * @see org.apache.xml.resolver.tools.CatalogResolver#resolve(java.lang.String,java.lang.String) |
| 40 | */ |
| 41 | @Override |
| 42 | public Source resolve(String href, String base) throws TransformerException { |
| 43 | String patchedHref = href; |
| 44 | if ( pattern.matcher(href).matches() ) { |
| 45 | patchedHref = FILE_PROTOCOL + "//" + XRadarUtils.normalizeFileName(href); |
| 46 | } |
| 47 | return super.resolve(patchedHref, base); |
| 48 | } |
| 49 | } |
| 50 | |