1 16 package org.apache.cocoon.util.location; 17 18 import java.lang.ref.WeakReference ; 19 import java.util.ArrayList ; 20 import java.util.List ; 21 22 import javax.xml.transform.SourceLocator ; 23 import javax.xml.transform.TransformerException ; 24 25 import org.xml.sax.Locator ; 26 import org.xml.sax.SAXParseException ; 27 28 34 public class LocationUtils { 35 36 39 public static final String UNKNOWN_STRING = "[unknown location]"; 40 41 private static List finders = new ArrayList (); 42 43 48 public interface LocationFinder { 49 56 Location getLocation(Object obj, String description); 57 } 58 59 private LocationUtils() { 60 } 62 63 64 72 public static String toString(Location location) { 73 StringBuffer result = new StringBuffer (); 74 75 String description = location.getDescription(); 76 if (description != null) { 77 result.append(description).append(" - "); 78 } 79 80 String uri = location.getURI(); 81 if (uri != null) { 82 result.append(uri).append(':').append(location.getLineNumber()).append(':').append(location.getColumnNumber()); 83 } else { 84 result.append(UNKNOWN_STRING); 85 } 86 87 return result.toString(); 88 } 89 90 98 public static LocationImpl parse(String text) throws IllegalArgumentException { 99 if (text == null || text.length() == 0) { 100 return null; 101 } 102 103 String description; 105 int uriStart = text.lastIndexOf(" - "); if (uriStart > -1) { 107 description = text.substring(0, uriStart); 108 uriStart += 3; } else { 110 description = null; 111 uriStart = 0; 112 } 113 114 try { 115 int colSep = text.lastIndexOf(':'); 116 if (colSep > -1) { 117 int column = Integer.parseInt(text.substring(colSep + 1)); 118 119 int lineSep = text.lastIndexOf(':', colSep - 1); 120 if (lineSep > -1) { 121 int line = Integer.parseInt(text.substring(lineSep + 1, colSep)); 122 return new LocationImpl(description, text.substring(uriStart, lineSep), line, column); 123 } 124 } else { 125 if (text.endsWith(UNKNOWN_STRING)) { 127 return LocationImpl.UNKNOWN; 128 } 129 } 130 } catch(Exception e) { 131 } 133 134 return LocationImpl.UNKNOWN; 135 } 136 137 143 public static boolean isKnown(Location location) { 144 return location != null && !Location.UNKNOWN.equals(location); 145 } 146 147 153 public static boolean isUnknown(Location location) { 154 return location == null || Location.UNKNOWN.equals(location); 155 } 156 157 181 public static void addFinder(LocationFinder finder) { 182 if (finder == null) { 183 return; 184 } 185 186 synchronized(LocationFinder.class) { 187 List newFinders = new ArrayList (finders); 190 newFinders.add(new WeakReference (finder)); 191 finders = newFinders; 192 } 193 } 194 195 202 public static Location getLocation(Object obj) { 203 return getLocation(obj, null); 204 } 205 206 215 public static Location getLocation(Object obj, String description) { 216 if (obj instanceof Locatable) { 217 return ((Locatable)obj).getLocation(); 218 } 219 220 if (obj instanceof SAXParseException ) { 222 SAXParseException spe = (SAXParseException )obj; 223 if (spe.getSystemId() != null) { 224 return new LocationImpl(description, spe.getSystemId(), spe.getLineNumber(), spe.getColumnNumber()); 225 } else { 226 return Location.UNKNOWN; 227 } 228 } 229 230 if (obj instanceof TransformerException ) { 231 TransformerException ex = (TransformerException )obj; 232 SourceLocator locator = ex.getLocator(); 233 if (locator != null && locator.getSystemId() != null) { 234 return new LocationImpl(description, locator.getSystemId(), locator.getLineNumber(), locator.getColumnNumber()); 235 } else { 236 return Location.UNKNOWN; 237 } 238 } 239 240 if (obj instanceof Locator ) { 241 Locator locator = (Locator )obj; 242 if (locator.getSystemId() != null) { 243 return new LocationImpl(description, locator.getSystemId(), locator.getLineNumber(), locator.getColumnNumber()); 244 } else { 245 return Location.UNKNOWN; 246 } 247 } 248 249 List currentFinders = finders; int size = currentFinders.size(); 251 for (int i = 0; i < size; i++) { 252 WeakReference ref = (WeakReference )currentFinders.get(i); 253 LocationFinder finder = (LocationFinder)ref.get(); 254 if (finder == null) { 255 synchronized(LocationFinder.class) { 257 List newFinders = new ArrayList (finders); 259 newFinders.remove(ref); 260 finders = newFinders; 261 } 262 } 263 264 Location result = finder.getLocation(obj, description); 265 if (result != null) { 266 return result; 267 } 268 } 269 270 return Location.UNKNOWN; 271 } 272 } 273 | Popular Tags |