下面列出了org.xml.sax.SAXParseException#getSystemId ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/** Creates an XMLParseException from a SAXParseException. */
protected static XMLParseException createXMLParseException(SAXParseException exception) {
final String fPublicId = exception.getPublicId();
final String fExpandedSystemId = exception.getSystemId();
final int fLineNumber = exception.getLineNumber();
final int fColumnNumber = exception.getColumnNumber();
XMLLocator location = new XMLLocator() {
public String getPublicId() { return fPublicId; }
public String getExpandedSystemId() { return fExpandedSystemId; }
public String getBaseSystemId() { return null; }
public String getLiteralSystemId() { return null; }
public int getColumnNumber() { return fColumnNumber; }
public int getLineNumber() { return fLineNumber; }
public int getCharacterOffset() { return -1; }
public String getEncoding() { return null; }
public String getXMLVersion() { return null; }
};
return new XMLParseException(location, exception.getMessage(),exception);
}
private void throwIOException(Exception e) throws IOException {
if (e instanceof IOException) {
throw (IOException) e;
}
if (e instanceof TransformerException) {
TransformerException te = MCRErrorListener.unwrapException((TransformerException) e);
String myMessageAndLocation = MCRErrorListener.getMyMessageAndLocation(te);
throw new IOException("Error while XSL Transformation: " + myMessageAndLocation, e);
}
if (e instanceof SAXParseException) {
SAXParseException spe = (SAXParseException) e;
String id = spe.getSystemId() != null ? spe.getSystemId() : spe.getPublicId();
int line = spe.getLineNumber();
int column = spe.getColumnNumber();
String msg = new MessageFormat("Error on {0}:{1} while parsing {2}", Locale.ROOT)
.format(new Object[] { line, column, id });
throw new IOException(msg, e);
}
throw new IOException(e);
}
public void error(SAXParseException e) throws SAXException {
if (errorCount >= ERROR_COUNT_LIMIT) {
// Ignore all errors after reaching the limit
return;
} else if (errorCount == 0) {
// Print a warning before the first error
System.err.println(SAXMessageFormatter.formatMessage(locale,
"errorHandlerNotSet", new Object [] {errorCount}));
}
String systemId = e.getSystemId();
if (systemId == null) {
systemId = "null";
}
String message = "Error: URI=" + systemId +
" Line=" + e.getLineNumber() +
": " + e.getMessage();
System.err.println(message);
errorCount++;
}
/**
* Returns a string of the location.
*/
private String getLocationString(SAXParseException ex) {
StringBuilder str = new StringBuilder();
String systemId = ex.getSystemId();
if (systemId != null) {
int index = systemId.lastIndexOf('/');
if (index != -1) {
systemId = systemId.substring(index + 1);
}
str.append(systemId);
}
str.append(':');
str.append(ex.getLineNumber());
str.append(':');
str.append(ex.getColumnNumber());
return str.toString();
}
/** Returns a string of the location. */
private String getLocationString(SAXParseException ex) {
StringBuffer str = new StringBuffer();
String systemId = ex.getSystemId();
if (systemId != null) {
int index = systemId.lastIndexOf('/');
if (index != -1) {
systemId = systemId.substring(index + 1);
}
str.append(systemId);
} else {
str.append(getResource().getName());
}
str.append(':');
str.append(ex.getLineNumber());
str.append(':');
str.append(ex.getColumnNumber());
return str.toString();
}
/** Returns a string of the location. */
private String getLocationString(SAXParseException ex) {
StringBuffer str = new StringBuffer();
String systemId = ex.getSystemId();
if (systemId != null) {
int index = systemId.lastIndexOf('/');
if (index != -1) {
systemId = systemId.substring(index + 1);
}
str.append(systemId);
} else {
str.append(getResource().getName());
}
str.append(':');
str.append(ex.getLineNumber());
str.append(':');
str.append(ex.getColumnNumber());
return str.toString();
}
/** Returns a string of the location. */
private String getLocationString(SAXParseException ex) {
StringBuffer str = new StringBuffer();
String systemId = ex.getSystemId();
if (systemId != null) {
int index = systemId.lastIndexOf('/');
if (index != -1) {
systemId = systemId.substring(index + 1);
}
str.append(systemId);
} else {
str.append(getResource().getName());
}
str.append(':');
str.append(ex.getLineNumber());
str.append(':');
str.append(ex.getColumnNumber());
return str.toString();
}
public void error(SAXParseException e) throws SAXException {
if (errorCount >= ERROR_COUNT_LIMIT) {
// Ignore all errors after reaching the limit
return;
} else if (errorCount == 0) {
// Print a warning before the first error
System.err.println(SAXMessageFormatter.formatMessage(locale,
"errorHandlerNotSet", new Object [] {errorCount}));
}
String systemId = e.getSystemId();
if (systemId == null) {
systemId = "null";
}
String message = "Error: URI=" + systemId +
" Line=" + e.getLineNumber() +
": " + e.getMessage();
System.err.println(message);
errorCount++;
}
public void error(SAXParseException e) throws SAXException {
if (errorCount >= ERROR_COUNT_LIMIT) {
// Ignore all errors after reaching the limit
return;
} else if (errorCount == 0) {
// Print a warning before the first error
System.err.println(SAXMessageFormatter.formatMessage(locale,
"errorHandlerNotSet", new Object [] {errorCount}));
}
String systemId = e.getSystemId();
if (systemId == null) {
systemId = "null";
}
String message = "Error: URI=" + systemId +
" Line=" + e.getLineNumber() +
": " + e.getMessage();
System.err.println(message);
errorCount++;
}
public void error(SAXParseException e) throws SAXException {
if (errorCount >= ERROR_COUNT_LIMIT) {
// Ignore all errors after reaching the limit
return;
} else if (errorCount == 0) {
// Print a warning before the first error
System.err.println(SAXMessageFormatter.formatMessage(locale,
"errorHandlerNotSet", new Object [] {errorCount}));
}
String systemId = e.getSystemId();
if (systemId == null) {
systemId = "null";
}
String message = "Error: URI=" + systemId +
" Line=" + e.getLineNumber() +
": " + e.getMessage();
System.err.println(message);
errorCount++;
}
/**
* Prints the error message.
* @param type error type
* @param ex exception that need to be printed
*/
private void printError(String type, SAXParseException ex) {
System.err.print("[" + type + "] ");
String systemId = ex.getSystemId();
if (systemId != null) {
int index = systemId.lastIndexOf('/');
if (index != -1)
systemId = systemId.substring(index + 1);
System.err.print(systemId);
}
System.err.print(':' + ex.getLineNumber());
System.err.print(':' + ex.getColumnNumber());
System.err.println(": " + ex.getMessage());
System.err.flush();
}
public void error(SAXParseException e) {
if(debug)
e.printStackTrace();
hasError = true;
if((e.getSystemId() == null && e.getPublicId() == null) && (e.getCause() instanceof UnknownHostException)) {
print(WscompileMessages.WSIMPORT_ERROR_MESSAGE(e.toString()), e);
} else {
print(WscompileMessages.WSIMPORT_ERROR_MESSAGE(e.getMessage()), e);
}
}
@Test
public void isValidPattern() throws IOException {
File tempFile = File.createTempFile("xml-", "-xxe");
tempFile.deleteOnExit();
try (OutputStream out = new FileOutputStream(tempFile)) {
out.write("<appinfo>you should not see me!!!</appinfo>".getBytes(StandardCharsets.UTF_8));
}
try {
XMLTypeValidator.XMLTypeValidatorFactory.createXMLTypeValidator(
"<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n" +
"<!DOCTYPE foo [ <!ELEMENT foo ANY >\n" +
"<!ENTITY xxe SYSTEM \"file://" + tempFile.getCanonicalPath() + "\" >]>\n" +
"<creds>\n" +
" <user>&xxe;</user>\n" +
" <pass>mypass</pass>\n" +
"</creds>"
);
} catch (Throwable e) {
if (e.getCause() instanceof SAXParseException) {
SAXParseException xxe = (SAXParseException) e.getCause();
// here comes the nasty XXE verification
String message = xxe.getMessage();
if (xxe.getSystemId() == null && message.contains(tempFile.getName()) && message.contains("accessExternalDTD")) {
// we're safe, the parsed failed to load the XXE
} else {
fail("XML got access to FS: " + message);
}
}
}
}
/**
* Returns the human readable string representation of the
* {@link org.xml.sax.Locator} part of the specified
* {@link SAXParseException}.
*
* @return non-null valid object.
*/
protected final String getLocationString( SAXParseException e ) {
if(e.getLineNumber()!=-1 || e.getSystemId()!=null) {
int line = e.getLineNumber();
return Messages.format( Messages.LINE_X_OF_Y,
line==-1?"?":Integer.toString( line ),
getShortName( e.getSystemId() ) );
} else {
return Messages.format( Messages.UNKNOWN_LOCATION );
}
}
public void parseFile(String filePath) {
initPart();
fileName = new File(filePath).getName();
initSingletonId(filePath);
String myerror = "";
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setValidating(false);
SAXParser parser = factory.newSAXParser();
InputSource inp = new InputSource(new FileReader(filePath));
parser.parse(inp, this);
/*
System.out.println("eventTermArrayList.size(); = " + eventTermArrayList.size());
System.out.println("timeLinks = " + timeLinks.size());
System.out.println("locationLinks = " + locationLinks.size());
System.out.println("plotLinks = " + plotLinks.size());
*/
} catch (SAXParseException err) {
myerror = "\n** Parsing error" + ", line " + err.getLineNumber()
+ ", uri " + err.getSystemId();
myerror += "\n" + err.getMessage();
System.out.println("myerror = " + myerror);
} catch (SAXException e) {
Exception x = e;
if (e.getException() != null)
x = e.getException();
myerror += "\nSAXException --" + x.getMessage();
System.out.println("myerror = " + myerror);
} catch (Exception eee) {
eee.printStackTrace();
myerror += "\nException --" + eee.getMessage();
System.out.println("myerror = " + myerror);
}
//System.out.println("myerror = " + myerror);
}
public void error(SAXParseException e) {
if(debug)
e.printStackTrace();
hasError = true;
if((e.getSystemId() == null && e.getPublicId() == null) && (e.getCause() instanceof UnknownHostException)) {
print(WscompileMessages.WSIMPORT_ERROR_MESSAGE(e.toString()), e);
} else {
print(WscompileMessages.WSIMPORT_ERROR_MESSAGE(e.getMessage()), e);
}
}
/**
* Returns the human readable string representation of the
* {@link org.xml.sax.Locator} part of the specified
* {@link SAXParseException}.
*
* @return non-null valid object.
*/
protected final String getLocationString( SAXParseException e ) {
if(e.getLineNumber()!=-1 || e.getSystemId()!=null) {
int line = e.getLineNumber();
return Messages.format( Messages.LINE_X_OF_Y,
line==-1?"?":Integer.toString( line ),
getShortName( e.getSystemId() ) );
} else {
return Messages.format( Messages.UNKNOWN_LOCATION );
}
}
private String getParseExceptionInfo(SAXParseException spe) {
return "URI=" + spe.getSystemId() + " Line="
+ spe.getLineNumber() + ": " + spe.getMessage();
}
private void appendMessage(StringBuffer messageBuffer,ValidationErrorHandler.ErrorRecord error) {
SAXParseException e = error.getException();
final String publicId = e.getPublicId();
final String systemId = e.getSystemId();
final int lineNum = e.getLineNumber();
final int colNum = e.getColumnNumber();
final String location;
if ( publicId == null && systemId == null && lineNum == -1 && colNum == -1 )
{
location = "";
}
else
{
final StringBuffer loc = new StringBuffer();
String sep = "";
if ( publicId != null )
{
loc.append( "Public ID " );
loc.append( publicId );
sep = ", ";
}
if ( systemId != null )
{
loc.append( sep );
loc.append( systemId );
sep = ", ";
}
if ( lineNum != -1 )
{
loc.append( sep );
loc.append( "line " );
loc.append( lineNum );
sep = ", ";
}
if ( colNum != -1 )
{
loc.append( sep );
loc.append( " column " );
loc.append( colNum );
sep = ", ";
}
location = loc.toString();
}
messageBuffer.append("While parsing ");
messageBuffer.append(error.getContext().getPath());
messageBuffer.append(( "".equals( location ) ? "" : ", at " + location ));
messageBuffer.append(": " );
messageBuffer.append(error.getType().toString());
messageBuffer.append(": " );
messageBuffer.append(e.getMessage());
String lineSep = System.getProperty("line.separator");
messageBuffer.append(lineSep);
}
private String getLocation(final SAXParseException e) {
final String exceptionId = e.getPublicId() == null ? e.getSystemId() : e.getPublicId();
return exceptionId + " [" + e.getLineNumber() + "," + e.getColumnNumber() + "] ";
}