1 17 18 19 20 package org.apache.lenya.xml; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.net.MalformedURLException ; 25 import java.net.URL ; 26 import java.util.Vector ; 27 28 import org.apache.log4j.Category; 29 30 public class XPSSourceInformation { 31 static Category log = Category.getInstance(XPSSourceInformation.class); 32 public int lineNumber = -1; 33 public URL url = null; 34 public XPSSourceInformation parentInfo = null; 35 public Vector children = null; 36 String offset = null; 37 String cocoon = null; 38 39 45 public XPSSourceInformation(String fileURL, String cocoon) { 46 this.cocoon = cocoon; 47 48 parentInfo = null; 49 offset = "++"; 50 children = new Vector (); 51 52 try { 53 url = new URL (fileURL); 54 } catch (MalformedURLException e) { 55 log.error(e); 56 } 57 } 58 59 66 public XPSSourceInformation(String urlString, XPSSourceInformation parentInfo, String cocoon) { 67 this.cocoon = cocoon; 68 69 this.parentInfo = parentInfo; 70 offset = "++"; 71 children = new Vector (); 72 73 try { 74 if (urlString.indexOf("/") == 0) { 75 url = new URL ("file:" + urlString); 76 } else if (urlString.indexOf("cocoon:") == 0) { 77 if (cocoon != null) { 78 url = new URL (cocoon + "/" + urlString.substring(7)); log.warn("Protocol 7789: COCOON (" + urlString + 80 ") -- will be transformed into http: " + url); 81 } else { 82 log.error("No cocoon base set!"); 83 } 84 } else { 85 url = new URL (urlString); 86 } 87 88 String p = url.getProtocol(); 89 90 if (!(p.equals("http") || p.equals("file") || p.equals("class"))) { 92 log.error("This type of protocol is not supported yet: " + p); 93 } 94 } catch (MalformedURLException e) { 96 log.debug("1079: " + e + " -- Let's hope it's a relative path!"); 97 98 File parent = new File (parentInfo.url.getFile()); 99 100 try { 102 int index = urlString.indexOf("#"); 103 String xpointer = ""; 104 String relativePath = urlString; 105 106 if (index != -1) { 107 relativePath = urlString.substring(0, index); 108 xpointer = urlString.substring(index); 109 } 110 111 relativePath = relativePath.replace('/', File.separatorChar); 112 113 File file = new File (parent.getParentFile(), relativePath); 114 115 url = new URL ("file", null, -1, file.getCanonicalPath() + xpointer); 116 117 log.info("Concatenated URL: " + url); 118 } catch (MalformedURLException exception) { 119 log.error(exception); 120 } catch (IOException exception) { 121 log.error(exception); 122 } 123 } 124 125 if (url.getProtocol().equals("http")) { 126 } else if (url.getProtocol().equals("file")) { 127 if (parentInfo.url.getProtocol().equals("http")) { 128 String protocol = parentInfo.url.getProtocol(); 129 String host = parentInfo.url.getHost(); 130 int port = parentInfo.url.getPort(); 131 132 try { 133 if (url.getRef() != null) { 134 url = new URL (protocol, host, port, url.getFile() + "#" + url.getRef()); 135 } else { 136 url = new URL (protocol, host, port, url.getFile()); 137 } 138 } catch (MalformedURLException e) { 139 log.error(e); 140 } 141 } 142 } else { 143 log.error("EXCEPTION: 0.2.21"); 144 } 145 } 146 147 152 public static void main(String [] args) { 153 String cocoon = null; 154 XPSSourceInformation xpssf = new XPSSourceInformation(args[0], cocoon); 155 System.out.println(xpssf); 156 } 157 158 163 public void addChild(XPSSourceInformation child) { 164 children.addElement(child); 165 } 166 167 172 public String toString() { 173 return toString("", offset); 174 } 175 176 184 public String toString(String index, String offset) { 185 String s = index + url.toString() + "\n"; 186 187 for (int i = 0; i < children.size(); i++) { 188 XPSSourceInformation child = (XPSSourceInformation) children.elementAt(i); 189 s = s + child.toString(index + offset, offset); 190 } 191 192 return s; 193 } 194 195 203 public boolean checkLoop(XPSSourceInformation xpssf, URL url) { 204 if (xpssf.url.getFile().equals(url.getFile())) { 205 if (xpssf.parentInfo != null) { 206 return true; 207 } else { 208 return false; } 210 } 211 212 if (xpssf.parentInfo != null) { 213 return checkLoop(xpssf.parentInfo, url); 214 } 215 216 return false; 217 } 218 } 219 | Popular Tags |