Source code: com/tripi/asp/msxml2/DOMURIResolver.java
1 /*
2 * Created on Mar 15, 2003
3 *
4 * To change this generated comment go to
5 * Window>Preferences>Java>Code Generation>Code Template
6 */
7 package com.tripi.asp.msxml2;
8
9 import java.io.File;
10
11 import javax.xml.transform.Source;
12 import javax.xml.transform.URIResolver;
13 import javax.xml.transform.stream.StreamSource;
14
15 import org.apache.log4j.Category;
16 /**
17 * @author jhorner
18 */
19 public class DOMURIResolver implements URIResolver {
20
21 /** Debugging category. */
22 Category DBG = Category.getInstance(DOMDocument.class);
23
24 private String pathprefix;
25 private String basefilename;
26
27 public DOMURIResolver() {
28 }
29
30 public Source resolve(String href, String base) {
31
32 StreamSource result = null;
33
34 if (DBG.isDebugEnabled()) {
35 DBG.debug("Base " + base);
36 DBG.debug("HREF " + href);
37 DBG.debug("Prefix " + this.pathprefix);
38 DBG.debug("Basefilename " + this.basefilename);
39 }
40
41 // try just the filename, absolute path
42 try {
43 DBG.debug("Trying [" + href + "]");
44 File file = new File(href);
45 if ( file.exists() ) {
46 result = new StreamSource(file);
47 }
48 }
49 catch (Exception e) {
50 // nothing
51 }
52
53 if ( result == null ) {
54 // use the basefilename and try a relative path
55 try {
56 String pathbase = parseBasepath(this.getBasefilename());
57 if (DBG.isDebugEnabled()) {
58 DBG.debug("Trying [" + pathbase + href + "]");
59 }
60 File file = new File(pathbase + href);
61 if ( file.exists() ) {
62 result = new StreamSource(file);
63 }
64 }
65 catch (Exception e) {
66 // nothing
67 }
68 }
69
70 // last resort try the prefix
71 if ( result == null ) {
72 String path = pathprefix + href;
73 if (DBG.isDebugEnabled()) {
74 DBG.debug("Trying [" + path + "]");
75 }
76 File file = new File(path);
77 if ( file.exists() ) {
78 result = new StreamSource(file);
79 }
80 }
81
82 // last resort try the prefix
83 if ( result == null ) {
84 String path = parseBasepath(pathprefix) + href;
85 if (DBG.isDebugEnabled()) {
86 DBG.debug("Trying [" + path + "]");
87 }
88 File file = new File(path);
89 if ( file.exists() ) {
90 result = new StreamSource(file);
91 }
92 }
93
94 return result;
95 }
96
97 private String parseBasepath(String filename) {
98 String result = "";
99
100 // remove everything after the last path separator
101 int index = filename.lastIndexOf(File.separatorChar);
102 if ( index > -1 ) {
103 result = filename.substring(0, index) + File.separator;
104 }
105
106 return result;
107 }
108
109 /**
110 * @return String
111 */
112 public String getBasefilename() {
113 return basefilename;
114 }
115
116 /**
117 * @return String
118 */
119 public String getPathprefix() {
120 return pathprefix;
121 }
122
123 /**
124 * Sets the basefilename.
125 * @param basefilename The basefilename to set
126 */
127 public void setBasefilename(String basefilename) {
128 this.basefilename = basefilename;
129 }
130
131 /**
132 * Sets the pathprefix.
133 * @param pathprefix The pathprefix to set
134 */
135 public void setPathprefix(String pathprefix) {
136 this.pathprefix = pathprefix;
137 }
138
139 }
140