下面列出了怎么用javax.xml.transform.SourceLocator的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* <code>systemId</code> returns the system id of the node passed as
* argument. If a node set is passed as argument, the system id of
* the first node in the set is returned.
*
* @param nodeList a <code>NodeList</code> value
* @return a <code>String</code> value
*/
public static String systemId(NodeList nodeList)
{
if (nodeList == null || nodeList.getLength() == 0)
return null;
Node node = nodeList.item(0);
int nodeHandler = ((DTMNodeProxy)node).getDTMNodeNumber();
SourceLocator locator = ((DTMNodeProxy)node).getDTM()
.getSourceLocatorFor(nodeHandler);
if (locator != null)
return locator.getSystemId();
else
return null;
}
/** Retrieve the SourceLocator associated with a specific node.
* This is only meaningful if the XalanProperties.SOURCE_LOCATION flag was
* set True using setProperty; if it was never set, or was set false, we
* will return null.
*
* (We _could_ return a locator with the document's base URI and bogus
* line/column information. Trying that; see the else clause.)
* */
public SourceLocator getSourceLocatorFor(int node)
{
if (m_useSourceLocationProperty)
{
node = makeNodeIdentity(node);
return new NodeLocator(null,
m_sourceSystemId.elementAt(node),
m_sourceLine.elementAt(node),
m_sourceColumn.elementAt(node));
}
else if(m_locator!=null)
{
return new NodeLocator(null,m_locator.getSystemId(),-1,-1);
}
else if(m_systemId!=null)
{
return new NodeLocator(null,m_systemId,-1,-1);
}
return null;
}
/**
* Get the source tree from the input source.
*
* @param source The Source object that should identify the desired node.
* @param locator The location of the caller, for diagnostic purposes.
*
* @return non-null reference to a node.
*
* @throws TransformerException if the Source argument can't be resolved to
* a node.
*/
public int getSourceTree(Source source, SourceLocator locator, XPathContext xctxt)
throws TransformerException
{
int n = getNode(source);
if (DTM.NULL != n)
return n;
n = parseToNode(source, locator, xctxt);
if (DTM.NULL != n)
putDocumentInCache(n, source);
return n;
}
/**
* <code>lineNumber</code> returns the line number of the node
* passed as argument. If a node set is passed as argument, the line
* number of the first node in the set is returned.
*
* NOTE: Xalan does not normally record location information for each node.
* To obtain it, you must set the custom TrAX attribute
* "http://xml.apache.org/xalan/features/source_location"
* true in the TransformerFactory before generating the Transformer and executing
* the stylesheet. Storage cost per node will be noticably increased in this mode.
*
* @param nodeList a <code>NodeList</code> value
* @return an <code>int</code> value. This may be -1 to indicate that the
* line number is not known.
*/
public static int lineNumber(NodeList nodeList)
{
if (nodeList == null || nodeList.getLength() == 0)
return -1;
Node node = nodeList.item(0);
int nodeHandler = ((DTMNodeProxy)node).getDTMNodeNumber();
SourceLocator locator = ((DTMNodeProxy)node).getDTM()
.getSourceLocatorFor(nodeHandler);
if (locator != null)
return locator.getLineNumber();
else
return -1;
}
/**
* Tell the user of an error, and probably throw an
* exception.
*
* @param xctxt The XPath runtime context.
* @param sourceNode Not used.
* @param msg An error msgkey that corresponds to one of the constants found
* in {@link com.sun.org.apache.xpath.internal.res.XPATHErrorResources}, which is
* a key for a format string.
* @param args An array of arguments represented in the format string, which
* may be null.
*
* @throws TransformerException if the current ErrorListoner determines to
* throw an exception.
*/
public void error(
XPathContext xctxt, int sourceNode, String msg, Object[] args)
throws javax.xml.transform.TransformerException
{
String fmsg = XSLMessages.createXPATHMessage(msg, args);
ErrorListener ehandler = xctxt.getErrorListener();
if (null != ehandler)
{
ehandler.fatalError(new TransformerException(fmsg,
(SAXSourceLocator)xctxt.getSAXLocator()));
}
else
{
SourceLocator slocator = xctxt.getSAXLocator();
System.out.println(fmsg + "; file " + slocator.getSystemId()
+ "; line " + slocator.getLineNumber() + "; column "
+ slocator.getColumnNumber());
}
}
/**
* <code>publicId</code> returns the public identifier of the node passed as
* argument. If a node set is passed as argument, the public identifier of
* the first node in the set is returned.
*
* Xalan does not currently record this value, and will return null.
*
* @param nodeList a <code>NodeList</code> value
* @return a <code>String</code> value
*/
public static String publicId(NodeList nodeList)
{
if (nodeList == null || nodeList.getLength() == 0)
return null;
Node node = nodeList.item(0);
int nodeHandler = ((DTMNodeProxy)node).getDTMNodeNumber();
SourceLocator locator = ((DTMNodeProxy)node).getDTM()
.getSourceLocatorFor(nodeHandler);
if (locator != null)
return locator.getPublicId();
else
return null;
}
/**
* <code>publicId</code> returns the public identifier of the node passed as
* argument. If a node set is passed as argument, the public identifier of
* the first node in the set is returned.
*
* Xalan does not currently record this value, and will return null.
*
* @param nodeList a <code>NodeList</code> value
* @return a <code>String</code> value
*/
public static String publicId(NodeList nodeList)
{
if (nodeList == null || nodeList.getLength() == 0)
return null;
Node node = nodeList.item(0);
int nodeHandler = ((DTMNodeProxy)node).getDTMNodeNumber();
SourceLocator locator = ((DTMNodeProxy)node).getDTM()
.getSourceLocatorFor(nodeHandler);
if (locator != null)
return locator.getPublicId();
else
return null;
}
/**
* <code>lineNumber</code> returns the line number of the node
* passed as argument. If a node set is passed as argument, the line
* number of the first node in the set is returned.
*
* NOTE: Xalan does not normally record location information for each node.
* To obtain it, you must set the custom TrAX attribute
* "http://xml.apache.org/xalan/features/source_location"
* true in the TransformerFactory before generating the Transformer and executing
* the stylesheet. Storage cost per node will be noticably increased in this mode.
*
* @param nodeList a <code>NodeList</code> value
* @return an <code>int</code> value. This may be -1 to indicate that the
* line number is not known.
*/
public static int lineNumber(NodeList nodeList)
{
if (nodeList == null || nodeList.getLength() == 0)
return -1;
Node node = nodeList.item(0);
int nodeHandler = ((DTMNodeProxy)node).getDTMNodeNumber();
SourceLocator locator = ((DTMNodeProxy)node).getDTM()
.getSourceLocatorFor(nodeHandler);
if (locator != null)
return locator.getLineNumber();
else
return -1;
}
/** Retrieve the SourceLocator associated with a specific node.
* This is only meaningful if the XalanProperties.SOURCE_LOCATION flag was
* set True using setProperty; if it was never set, or was set false, we
* will return null.
*
* (We _could_ return a locator with the document's base URI and bogus
* line/column information. Trying that; see the else clause.)
* */
public SourceLocator getSourceLocatorFor(int node)
{
if (m_useSourceLocationProperty)
{
node = makeNodeIdentity(node);
return new NodeLocator(null,
m_sourceSystemId.elementAt(node),
m_sourceLine.elementAt(node),
m_sourceColumn.elementAt(node));
}
else if(m_locator!=null)
{
return new NodeLocator(null,m_locator.getSystemId(),-1,-1);
}
else if(m_systemId!=null)
{
return new NodeLocator(null,m_systemId,-1,-1);
}
return null;
}
/**
* Get the source tree from the a base URL and a URL string.
*
* @param base The base URI to use if the urlString is relative.
* @param urlString An absolute or relative URL string.
* @param locator The location of the caller, for diagnostic purposes.
*
* @return should be a non-null reference to the node identified by the
* base and urlString.
*
* @throws TransformerException If the URL can not resolve to a node.
*/
public int getSourceTree(
String base, String urlString, SourceLocator locator, XPathContext xctxt)
throws TransformerException
{
// System.out.println("getSourceTree");
try
{
Source source = this.resolveURI(base, urlString, locator);
// System.out.println("getSourceTree - base: "+base+", urlString: "+urlString+", source: "+source.getSystemId());
return getSourceTree(source, locator, xctxt);
}
catch (IOException ioe)
{
throw new TransformerException(ioe.getMessage(), locator, ioe);
}
/* catch (TransformerException te)
{
throw new TransformerException(te.getMessage(), locator, te);
}*/
}
/**
* Get the source tree from the input source.
*
* @param source The Source object that should identify the desired node.
* @param locator The location of the caller, for diagnostic purposes.
*
* @return non-null reference to a node.
*
* @throws TransformerException if the Source argument can't be resolved to
* a node.
*/
public int getSourceTree(Source source, SourceLocator locator, XPathContext xctxt)
throws TransformerException
{
int n = getNode(source);
if (DTM.NULL != n)
return n;
n = parseToNode(source, locator, xctxt);
if (DTM.NULL != n)
putDocumentInCache(n, source);
return n;
}
/**
* Tell the user of an error, and probably throw an
* exception.
*
* @param xctxt The XPath runtime context.
* @param sourceNode Not used.
* @param msg An error msgkey that corresponds to one of the constants found
* in {@link com.sun.org.apache.xpath.internal.res.XPATHErrorResources}, which is
* a key for a format string.
* @param args An array of arguments represented in the format string, which
* may be null.
*
* @throws TransformerException if the current ErrorListoner determines to
* throw an exception.
*/
public void error(
XPathContext xctxt, int sourceNode, String msg, Object[] args)
throws javax.xml.transform.TransformerException
{
String fmsg = XSLMessages.createXPATHMessage(msg, args);
ErrorListener ehandler = xctxt.getErrorListener();
if (null != ehandler)
{
ehandler.fatalError(new TransformerException(fmsg,
(SAXSourceLocator)xctxt.getSAXLocator()));
}
else
{
SourceLocator slocator = xctxt.getSAXLocator();
System.out.println(fmsg + "; file " + slocator.getSystemId()
+ "; line " + slocator.getLineNumber() + "; column "
+ slocator.getColumnNumber());
}
}
private static String buildErrorMessage(String resource, Exception cause) {
StringBuilder msg = new StringBuilder("Error compiling XSL stylesheet ");
msg.append(resource);
if (cause instanceof TransformerException) {
TransformerException tex = (TransformerException) cause;
msg.append("\n").append(tex.getMessage());
SourceLocator sl = tex.getLocator();
if (sl != null) {
msg.append(" (").append(sl.getSystemId()).append(") ");
msg.append(" at line ").append(sl.getLineNumber());
msg.append(" column ").append(sl.getColumnNumber());
}
}
return msg.toString();
}
/**
* <code>publicId</code> returns the public identifier of the node passed as
* argument. If a node set is passed as argument, the public identifier of
* the first node in the set is returned.
*
* Xalan does not currently record this value, and will return null.
*
* @param nodeList a <code>NodeList</code> value
* @return a <code>String</code> value
*/
public static String publicId(NodeList nodeList)
{
if (nodeList == null || nodeList.getLength() == 0)
return null;
Node node = nodeList.item(0);
int nodeHandler = ((DTMNodeProxy)node).getDTMNodeNumber();
SourceLocator locator = ((DTMNodeProxy)node).getDTM()
.getSourceLocatorFor(nodeHandler);
if (locator != null)
return locator.getPublicId();
else
return null;
}
/**
* <code>publicId</code> returns the public identifier of the current
* context node.
*
* Xalan does not currently record this value, and will return null.
*
* @param context an <code>ExpressionContext</code> value
* @return a <code>String</code> value
*/
public static String publicId(ExpressionContext context)
{
Node contextNode = context.getContextNode();
int nodeHandler = ((DTMNodeProxy)contextNode).getDTMNodeNumber();
SourceLocator locator = ((DTMNodeProxy)contextNode).getDTM()
.getSourceLocatorFor(nodeHandler);
if (locator != null)
return locator.getPublicId();
else
return null;
}
/**
* Constructor SAXSourceLocator
*
*
* @param locator Source locator
*/
public SAXSourceLocator(javax.xml.transform.SourceLocator locator)
{
m_locator = null;
this.setColumnNumber(locator.getColumnNumber());
this.setLineNumber(locator.getLineNumber());
this.setPublicId(locator.getPublicId());
this.setSystemId(locator.getSystemId());
}
/**
* Constructor SAXSourceLocator
*
*
* @param locator Source locator
*/
public SAXSourceLocator(javax.xml.transform.SourceLocator locator)
{
m_locator = null;
this.setColumnNumber(locator.getColumnNumber());
this.setLineNumber(locator.getLineNumber());
this.setPublicId(locator.getPublicId());
this.setSystemId(locator.getSystemId());
}
public static void ensureLocationSet(TransformerException exception)
{
// SourceLocator locator = exception.getLocator();
SourceLocator locator = null;
Throwable cause = exception;
// Try to find the locator closest to the cause.
do
{
if(cause instanceof SAXParseException)
{
locator = new SAXSourceLocator((SAXParseException)cause);
}
else if (cause instanceof TransformerException)
{
SourceLocator causeLocator = ((TransformerException)cause).getLocator();
if(null != causeLocator)
locator = causeLocator;
}
if(cause instanceof TransformerException)
cause = ((TransformerException)cause).getCause();
else if(cause instanceof SAXException)
cause = ((SAXException)cause).getException();
else
cause = null;
}
while(null != cause);
exception.setLocator(locator);
}
/**
* <code>publicId</code> returns the public identifier of the current
* context node.
*
* Xalan does not currently record this value, and will return null.
*
* @param context an <code>ExpressionContext</code> value
* @return a <code>String</code> value
*/
public static String publicId(ExpressionContext context)
{
Node contextNode = context.getContextNode();
int nodeHandler = ((DTMNodeProxy)contextNode).getDTMNodeNumber();
SourceLocator locator = ((DTMNodeProxy)contextNode).getDTM()
.getSourceLocatorFor(nodeHandler);
if (locator != null)
return locator.getPublicId();
else
return null;
}
public SourceLocator getSourceLocatorFor(int node)
{
if (_dom != null) {
return _dom.getSourceLocatorFor(node);
}
else {
return super.getSourceLocatorFor(node);
}
}
/**
* Construct an XPath object.
*
* (Needs review -sc) This method initializes an XPathParser/
* Compiler and compiles the expression.
* @param exprString The XPath expression.
* @param locator The location of the expression, may be null.
* @param prefixResolver A prefix resolver to use to resolve prefixes to
* namespace URIs.
* @param type one of {@link #SELECT} or {@link #MATCH}.
* @param errorListener The error listener, or null if default should be used.
*
* @throws javax.xml.transform.TransformerException if syntax or other error.
*/
public XPath(
String exprString, SourceLocator locator, PrefixResolver prefixResolver, int type,
ErrorListener errorListener)
throws javax.xml.transform.TransformerException
{
initFunctionTable();
if(null == errorListener)
errorListener = new com.sun.org.apache.xml.internal.utils.DefaultErrorHandler();
m_patternString = exprString;
XPathParser parser = new XPathParser(errorListener, locator);
Compiler compiler = new Compiler(errorListener, locator, m_funcTable);
if (SELECT == type)
parser.initXPath(compiler, exprString, prefixResolver);
else if (MATCH == type)
parser.initMatchPattern(compiler, exprString, prefixResolver);
else
throw new RuntimeException(XSLMessages.createXPATHMessage(
XPATHErrorResources.ER_CANNOT_DEAL_XPATH_TYPE,
new Object[]{Integer.toString(type)}));
// System.out.println("----------------");
Expression expr = compiler.compileExpression(0);
// System.out.println("expr: "+expr);
this.setExpression(expr);
if((null != locator) && locator instanceof ExpressionNode)
{
expr.exprSetParent((ExpressionNode)locator);
}
}
/**
* Try to create a DOM source tree from the input source.
*
* @param source The Source object that identifies the source node.
* @param locator The location of the caller, for diagnostic purposes.
*
* @return non-null reference to node identified by the source argument.
*
* @throws TransformerException if the source argument can not be resolved
* to a source node.
*/
public int parseToNode(Source source, SourceLocator locator, XPathContext xctxt)
throws TransformerException
{
try
{
Object xowner = xctxt.getOwnerObject();
DTM dtm;
if(null != xowner && xowner instanceof com.sun.org.apache.xml.internal.dtm.DTMWSFilter)
{
dtm = xctxt.getDTM(source, false,
(com.sun.org.apache.xml.internal.dtm.DTMWSFilter)xowner, false, true);
}
else
{
dtm = xctxt.getDTM(source, false, null, false, true);
}
return dtm.getDocument();
}
catch (Exception e)
{
//e.printStackTrace();
throw new TransformerException(e.getMessage(), locator, e);
}
}
public static void ensureLocationSet(TransformerException exception)
{
// SourceLocator locator = exception.getLocator();
SourceLocator locator = null;
Throwable cause = exception;
// Try to find the locator closest to the cause.
do
{
if(cause instanceof SAXParseException)
{
locator = new SAXSourceLocator((SAXParseException)cause);
}
else if (cause instanceof TransformerException)
{
SourceLocator causeLocator = ((TransformerException)cause).getLocator();
if(null != causeLocator)
locator = causeLocator;
}
if(cause instanceof TransformerException)
cause = ((TransformerException)cause).getCause();
else if(cause instanceof SAXException)
cause = ((SAXException)cause).getException();
else
cause = null;
}
while(null != cause);
exception.setLocator(locator);
}
/**
* <code>systemId</code> returns the system id of the current
* context node.
*
* @param context an <code>ExpressionContext</code> value
* @return a <code>String</code> value
*/
public static String systemId(ExpressionContext context)
{
Node contextNode = context.getContextNode();
int nodeHandler = ((DTMNodeProxy)contextNode).getDTMNodeNumber();
SourceLocator locator = ((DTMNodeProxy)contextNode).getDTM()
.getSourceLocatorFor(nodeHandler);
if (locator != null)
return locator.getSystemId();
else
return null;
}
/**
* Construct an XPath object.
*
* (Needs review -sc) This method initializes an XPathParser/
* Compiler and compiles the expression.
* @param exprString The XPath expression.
* @param locator The location of the expression, may be null.
* @param prefixResolver A prefix resolver to use to resolve prefixes to
* namespace URIs.
* @param type one of {@link #SELECT} or {@link #MATCH}.
* @param errorListener The error listener, or null if default should be used.
*
* @throws javax.xml.transform.TransformerException if syntax or other error.
*/
public XPath(
String exprString, SourceLocator locator,
PrefixResolver prefixResolver, int type,
ErrorListener errorListener, FunctionTable aTable)
throws javax.xml.transform.TransformerException
{
m_funcTable = aTable;
if(null == errorListener)
errorListener = new com.sun.org.apache.xml.internal.utils.DefaultErrorHandler();
m_patternString = exprString;
XPathParser parser = new XPathParser(errorListener, locator);
Compiler compiler = new Compiler(errorListener, locator, m_funcTable);
if (SELECT == type)
parser.initXPath(compiler, exprString, prefixResolver);
else if (MATCH == type)
parser.initMatchPattern(compiler, exprString, prefixResolver);
else
throw new RuntimeException(XSLMessages.createXPATHMessage(
XPATHErrorResources.ER_CANNOT_DEAL_XPATH_TYPE,
new Object[]{Integer.toString(type)}));
//"Can not deal with XPath type: " + type);
// System.out.println("----------------");
Expression expr = compiler.compile(0);
// System.out.println("expr: "+expr);
this.setExpression(expr);
if((null != locator) && locator instanceof ExpressionNode)
{
expr.exprSetParent((ExpressionNode)locator);
}
}
/**
* Construct an XPath object.
*
* (Needs review -sc) This method initializes an XPathParser/
* Compiler and compiles the expression.
* @param exprString The XPath expression.
* @param locator The location of the expression, may be null.
* @param prefixResolver A prefix resolver to use to resolve prefixes to
* namespace URIs.
* @param type one of {@link #SELECT} or {@link #MATCH}.
* @param errorListener The error listener, or null if default should be used.
*
* @throws javax.xml.transform.TransformerException if syntax or other error.
*/
public XPath(
String exprString, SourceLocator locator, PrefixResolver prefixResolver, int type,
ErrorListener errorListener)
throws javax.xml.transform.TransformerException
{
initFunctionTable();
if(null == errorListener)
errorListener = new com.sun.org.apache.xml.internal.utils.DefaultErrorHandler();
m_patternString = exprString;
XPathParser parser = new XPathParser(errorListener, locator);
Compiler compiler = new Compiler(errorListener, locator, m_funcTable);
if (SELECT == type)
parser.initXPath(compiler, exprString, prefixResolver);
else if (MATCH == type)
parser.initMatchPattern(compiler, exprString, prefixResolver);
else
throw new RuntimeException(XSLMessages.createXPATHMessage(XPATHErrorResources.ER_CANNOT_DEAL_XPATH_TYPE, new Object[]{Integer.toString(type)})); //"Can not deal with XPath type: " + type);
// System.out.println("----------------");
Expression expr = compiler.compile(0);
// System.out.println("expr: "+expr);
this.setExpression(expr);
if((null != locator) && locator instanceof ExpressionNode)
{
expr.exprSetParent((ExpressionNode)locator);
}
}
private static void printLocation(PrintWriter diagnosticsWriter, Throwable throwable) {
try {
Class<?> errorHandler =
Class.forName("com.sun.org.apache.xml.internal.utils.DefaultErrorHandler");
Method m = errorHandler.getMethod("printLocation", PrintWriter.class, Throwable.class);
m.invoke(null, diagnosticsWriter, throwable);
} catch (Throwable t) {
SourceLocator locator = null;
Throwable cause = throwable;
// Try to find the locator closest to the cause.
do {
if (cause instanceof TransformerException) {
SourceLocator causeLocator = ((TransformerException) cause).getLocator();
if (null != causeLocator) {
locator = causeLocator;
}
cause = ((TransformerException) cause).getCause();
} else if (cause instanceof SAXException) {
cause = ((SAXException) cause).getException();
} else {
cause = cause.getCause();
}
} while (null != cause);
if (null != locator) {
// m_pw.println("Parser fatal error: "+exception.getMessage());
String id = (null != locator.getPublicId())
? locator.getPublicId()
: (null != locator.getSystemId())
? locator.getSystemId() : "SystemId Unknown"; //"SystemId Unknown";
diagnosticsWriter.print(id + "; " + "line: " + locator.getLineNumber()
+ "; column: " + locator.getColumnNumber() + "; ");
}
diagnosticsWriter.print("(" + throwable + ": unknown location)");
}
}
public static void ensureLocationSet(TransformerException exception)
{
// SourceLocator locator = exception.getLocator();
SourceLocator locator = null;
Throwable cause = exception;
// Try to find the locator closest to the cause.
do
{
if(cause instanceof SAXParseException)
{
locator = new SAXSourceLocator((SAXParseException)cause);
}
else if (cause instanceof TransformerException)
{
SourceLocator causeLocator = ((TransformerException)cause).getLocator();
if(null != causeLocator)
locator = causeLocator;
}
if(cause instanceof TransformerException)
cause = ((TransformerException)cause).getCause();
else if(cause instanceof SAXException)
cause = ((SAXException)cause).getException();
else
cause = null;
}
while(null != cause);
exception.setLocator(locator);
}
/**
* Try to create a DOM source tree from the input source.
*
* @param source The Source object that identifies the source node.
* @param locator The location of the caller, for diagnostic purposes.
*
* @return non-null reference to node identified by the source argument.
*
* @throws TransformerException if the source argument can not be resolved
* to a source node.
*/
public int parseToNode(Source source, SourceLocator locator, XPathContext xctxt)
throws TransformerException
{
try
{
Object xowner = xctxt.getOwnerObject();
DTM dtm;
if(null != xowner && xowner instanceof com.sun.org.apache.xml.internal.dtm.DTMWSFilter)
{
dtm = xctxt.getDTM(source, false,
(com.sun.org.apache.xml.internal.dtm.DTMWSFilter)xowner, false, true);
}
else
{
dtm = xctxt.getDTM(source, false, null, false, true);
}
return dtm.getDocument();
}
catch (Exception e)
{
//e.printStackTrace();
throw new TransformerException(e.getMessage(), locator, e);
}
}
/**
* Wrap an existing exception in a DTMException.
*
* @param message The error or warning message, or null to
* use the message from the embedded exception.
* @param locator The locator object for the error or warning.
* @param e Any exception
*/
public DTMException(String message, SourceLocator locator,
Throwable e) {
super(message);
this.containedException = e;
this.locator = locator;
}