1 16 19 package org.apache.xalan.templates; 20 21 import java.io.IOException ; 22 import java.io.PrintWriter ; 23 import java.io.StringWriter ; 24 25 import javax.xml.transform.ErrorListener ; 26 import javax.xml.transform.Source ; 27 import javax.xml.transform.SourceLocator ; 28 import javax.xml.transform.TransformerException ; 29 30 import org.apache.xalan.res.XSLMessages; 31 import org.apache.xalan.res.XSLTErrorResources; 32 import org.apache.xml.dtm.DTM; 33 import org.apache.xml.dtm.DTMIterator; 34 import org.apache.xml.utils.XMLString; 35 import org.apache.xpath.Expression; 36 import org.apache.xpath.NodeSetDTM; 37 import org.apache.xpath.SourceTreeManager; 38 import org.apache.xpath.XPathContext; 39 import org.apache.xpath.functions.Function2Args; 40 import org.apache.xpath.functions.WrongNumberArgsException; 41 import org.apache.xpath.objects.XNodeSet; 42 import org.apache.xpath.objects.XObject; 43 44 59 public class FuncDocument extends Function2Args 60 { 61 62 70 public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException 71 { 72 int context = xctxt.getCurrentNode(); 73 DTM dtm = xctxt.getDTM(context); 74 75 int docContext = dtm.getDocumentRoot(context); 76 XObject arg = (XObject) this.getArg0().execute(xctxt); 77 78 String base = ""; 79 Expression arg1Expr = this.getArg1(); 80 81 if (null != arg1Expr) 82 { 83 84 XObject arg2 = arg1Expr.execute(xctxt); 89 90 if (XObject.CLASS_NODESET == arg2.getType()) 91 { 92 int baseNode = arg2.iter().nextNode(); 93 94 if (baseNode == DTM.NULL) 95 { 96 warn(xctxt, XSLTErrorResources.WG_EMPTY_SECOND_ARG, null); 100 XNodeSet nodes = new XNodeSet(xctxt.getDTMManager()); 101 return nodes; 102 } else{ 103 DTM baseDTM = xctxt.getDTM(baseNode); 104 base = baseDTM.getDocumentBaseURI(); 105 } 106 } 119 else 120 { 121 arg2.iter(); 123 } 124 } 125 else 126 { 127 128 assertion(null != xctxt.getNamespaceContext(), "Namespace context can not be null!"); 138 base = xctxt.getNamespaceContext().getBaseIdentifier(); 139 } 140 141 XNodeSet nodes = new XNodeSet(xctxt.getDTMManager()); 142 NodeSetDTM mnl = nodes.mutableNodeset(); 143 DTMIterator iterator = (XObject.CLASS_NODESET == arg.getType()) 144 ? arg.iter() : null; 145 int pos = DTM.NULL; 146 147 while ((null == iterator) || (DTM.NULL != (pos = iterator.nextNode()))) 148 { 149 XMLString ref = (null != iterator) 150 ? xctxt.getDTM(pos).getStringValue(pos) : arg.xstr(); 151 152 if (null == arg1Expr && DTM.NULL != pos) 161 { 162 DTM baseDTM = xctxt.getDTM(pos); 163 base = baseDTM.getDocumentBaseURI(); 164 } 165 166 if (null == ref) 167 continue; 168 169 if (DTM.NULL == docContext) 170 { 171 error(xctxt, XSLTErrorResources.ER_NO_CONTEXT_OWNERDOC, null); } 173 174 int indexOfColon = ref.indexOf(':'); 181 int indexOfSlash = ref.indexOf('/'); 182 183 if ((indexOfColon != -1) && (indexOfSlash != -1) 184 && (indexOfColon < indexOfSlash)) 185 { 186 187 base = null; 189 } 190 191 int newDoc = getDoc(xctxt, context, ref.toString(), base); 192 193 if (DTM.NULL != newDoc) 195 { 196 if (!mnl.contains(newDoc)) 198 { 199 mnl.addElement(newDoc); 200 } 201 } 202 203 if (null == iterator || newDoc == DTM.NULL) 204 break; 205 } 206 207 return nodes; 208 } 209 210 223 int getDoc(XPathContext xctxt, int context, String uri, String base) 224 throws javax.xml.transform.TransformerException 225 { 226 227 SourceTreeManager treeMgr = xctxt.getSourceTreeManager(); 229 Source source; 230 231 int newDoc; 232 try 233 { 234 source = treeMgr.resolveURI(base, uri, xctxt.getSAXLocator()); 235 newDoc = treeMgr.getNode(source); 236 } 237 catch (IOException ioe) 238 { 239 throw new TransformerException (ioe.getMessage(), 240 (SourceLocator )xctxt.getSAXLocator(), ioe); 241 } 242 catch(TransformerException te) 243 { 244 throw new TransformerException (te); 245 } 246 247 if (DTM.NULL != newDoc) 248 return newDoc; 249 250 if (uri.length() == 0) 252 { 253 uri = xctxt.getNamespaceContext().getBaseIdentifier(); 255 try 256 { 257 source = treeMgr.resolveURI(base, uri, xctxt.getSAXLocator()); 258 } 259 catch (IOException ioe) 260 { 261 throw new TransformerException (ioe.getMessage(), 262 (SourceLocator )xctxt.getSAXLocator(), ioe); 263 } 264 } 265 266 String diagnosticsString = null; 267 268 try 269 { 270 if ((null != uri) && (uri.toString().length() > 0)) 271 { 272 newDoc = treeMgr.getSourceTree(source, xctxt.getSAXLocator(), xctxt); 273 274 } 276 else 277 warn(xctxt, XSLTErrorResources.WG_CANNOT_MAKE_URL_FROM, 278 new Object []{ ((base == null) ? "" : base) + uri }); } 280 catch (Throwable throwable) 281 { 282 283 newDoc = DTM.NULL; 285 286 while (throwable 288 instanceof org.apache.xml.utils.WrappedRuntimeException) 289 { 290 throwable = 291 ((org.apache.xml.utils.WrappedRuntimeException) throwable).getException(); 292 } 293 294 if ((throwable instanceof NullPointerException ) 295 || (throwable instanceof ClassCastException )) 296 { 297 throw new org.apache.xml.utils.WrappedRuntimeException( 298 (Exception ) throwable); 299 } 300 301 StringWriter sw = new StringWriter (); 302 PrintWriter diagnosticsWriter = new PrintWriter (sw); 303 304 if (throwable instanceof TransformerException ) 305 { 306 TransformerException spe = (TransformerException ) throwable; 307 308 { 309 Throwable e = spe; 310 311 while (null != e) 312 { 313 if (null != e.getMessage()) 314 { 315 diagnosticsWriter.println(" (" + e.getClass().getName() + "): " 316 + e.getMessage()); 317 } 318 319 if (e instanceof TransformerException ) 320 { 321 TransformerException spe2 = (TransformerException ) e; 322 323 SourceLocator locator = spe2.getLocator(); 324 if ((null != locator) && (null != locator.getSystemId())) 325 diagnosticsWriter.println(" ID: " + locator.getSystemId() 326 + " Line #" + locator.getLineNumber() 327 + " Column #" 328 + locator.getColumnNumber()); 329 330 e = spe2.getException(); 331 332 if (e instanceof org.apache.xml.utils.WrappedRuntimeException) 333 e = ((org.apache.xml.utils.WrappedRuntimeException) e).getException(); 334 } 335 else 336 e = null; 337 } 338 } 339 } 340 else 341 { 342 diagnosticsWriter.println(" (" + throwable.getClass().getName() 343 + "): " + throwable.getMessage()); 344 } 345 346 diagnosticsString = throwable.getMessage(); } 348 349 if (DTM.NULL == newDoc) 350 { 351 352 if (null != diagnosticsString) 354 { 355 warn(xctxt, XSLTErrorResources.WG_CANNOT_LOAD_REQUESTED_DOC, 356 new Object []{ diagnosticsString }); } 358 else 359 warn(xctxt, XSLTErrorResources.WG_CANNOT_LOAD_REQUESTED_DOC, 360 new Object []{ 361 uri == null 362 ? ((base == null) ? "" : base) + uri : uri.toString() }); } 364 else 365 { 366 } 370 371 return newDoc; 372 } 373 374 386 public void error(XPathContext xctxt, String msg, Object args[]) 387 throws javax.xml.transform.TransformerException 388 { 389 390 String formattedMsg = XSLMessages.createMessage(msg, args); 391 ErrorListener errHandler = xctxt.getErrorListener(); 392 TransformerException spe = new TransformerException (formattedMsg, 393 (SourceLocator )xctxt.getSAXLocator()); 394 395 if (null != errHandler) 396 errHandler.error(spe); 397 else 398 System.out.println(formattedMsg); 399 } 400 401 412 public void warn(XPathContext xctxt, String msg, Object args[]) 413 throws javax.xml.transform.TransformerException 414 { 415 416 String formattedMsg = XSLMessages.createWarning(msg, args); 417 ErrorListener errHandler = xctxt.getErrorListener(); 418 TransformerException spe = new TransformerException (formattedMsg, 419 (SourceLocator )xctxt.getSAXLocator()); 420 421 if (null != errHandler) 422 errHandler.warning(spe); 423 else 424 System.out.println(formattedMsg); 425 } 426 427 435 public void checkNumberArgs(int argNum) throws WrongNumberArgsException 436 { 437 if ((argNum < 1) || (argNum > 2)) 438 reportWrongNumberArgs(); 439 } 440 441 447 protected void reportWrongNumberArgs() throws WrongNumberArgsException { 448 throw new WrongNumberArgsException(XSLMessages.createMessage(XSLTErrorResources.ER_ONE_OR_TWO, null)); } 450 451 455 public boolean isNodesetExpr() 456 { 457 return true; 458 } 459 460 } 461 | Popular Tags |