ICU-3186 more error updates, caching entity resolver

X-SVN-Rev: 16355
This commit is contained in:
Steven R. Loomis 2004-09-22 23:41:38 +00:00
parent 8df4a0b93b
commit 6172f70663
4 changed files with 134 additions and 7 deletions

View file

@ -0,0 +1,121 @@
/*
******************************************************************************
* Copyright (C) 2004, International Business Machines Corporation and *
* others. All Rights Reserved. *
******************************************************************************
*
* in shell: (such as .cldrrc)
* export CWDEBUG="-DCLDR_DTD_CACHE=/tmp/cldrdtd/"
* export CWDEFS="-DCLDR_DTD_CACHE_DEBUG=y ${CWDEBUG}"
*
*
* in code:
* docBuilder.setEntityResolver(new CachingEntityResolver());
*
*/
package com.ibm.icu.dev.tool.cldr;
/**
* @author srl
*
* Caching entity resolver
*/
import java.io.*;
import java.io.File;
import java.io.FileReader;
import java.io.BufferedReader;
// Needed JAXP classes
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
// SAX2 imports
import org.xml.sax.ErrorHandler;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
//DOM imports
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;
public class CachingEntityResolver implements EntityResolver {
static final String CLDR_DTD_CACHE = "CLDR_DTD_CACHE";
public InputSource resolveEntity (String publicId, String systemId) {
boolean aDebug = false;
if((System.getProperty("CLDR_DTD_CACHE_DEBUG")!=null) || "y".equals(System.getProperty("CLDR_DTD_CACHE_ADEBUG"))) {
aDebug = true;
}
if(aDebug) {
System.out.println("CRE: " + publicId + " | " + systemId);
}
String theCache = System.getProperty(CLDR_DTD_CACHE);
if((theCache!=null) && (theCache.length()>0)) {
int i;
StringBuffer systemNew = new StringBuffer(systemId);
if(systemId.startsWith("file:")) {
return null;
}
// char c = systemNew.charAt(0);
// if((c=='.')||(c=='/')) {
// return null;
// }
for(i=0;i<systemNew.length();i++) {
char c = systemNew.charAt(i);
if(!Character.isLetterOrDigit(c) && (c!='.')) {
systemNew.setCharAt(i, '_');
}
}
if(aDebug) {
System.out.println(systemNew.toString());
}
File aDir = new File(theCache);
if(!aDir.exists() || !aDir.isDirectory()) {
// doesn't exist or isn't a directory:
System.err.println("CachingEntityResolver: Warning: Cache not used, Directory doesn't exist, Check the value of property " + CLDR_DTD_CACHE + " : " + theCache);
return null;
}
String newName = new String(systemNew);
File t = new File(theCache,newName);
if(t.exists()) {
if(aDebug) {
System.out.println("Using existing: " + t.getPath());
}
} else {
if(aDebug) {
System.out.println(t.getPath() + " doesn't exist. fetching.");
}
try {
BufferedReader r = new BufferedReader(new InputStreamReader(new java.net.URL(systemId).openStream()));
BufferedWriter w = new BufferedWriter(new FileWriter(t.getPath()));
String s;
while((s=r.readLine())!=null) {
w.write(s);
w.newLine();
}
r.close();
w.close();
} catch ( Throwable th ) {
System.err.println(th.toString() + " trying to fetch " + t.getPath());
return null;
}
if(aDebug) {
System.out.println(t.getPath() + " fetched.");
}
}
return new InputSource(t.getPath());
}
return null; // unhelpful
}
}

View file

@ -156,7 +156,7 @@ public class LDML2ICUConverter {
}
}catch (Throwable se) {
System.err.println("ERROR: " + se.toString());
System.err.println(fileName + ": ERROR: " + se.toString());
se.printStackTrace();
System.exit(1);
}
@ -263,7 +263,7 @@ public class LDML2ICUConverter {
writeAliasedResource();
}
catch (Throwable se) {
System.err.println("ERROR: " + se.toString());
System.err.println(xmlfileName + ": ERROR: " + se.toString());
se.printStackTrace();
System.exit(1);
}
@ -2858,7 +2858,7 @@ public class LDML2ICUConverter {
FileOutputStream file = new FileOutputStream(outputFileName);
BufferedOutputStream writer = new BufferedOutputStream(file);
System.out.println("INFO: Creating ICU ResourceBundle: "+outputFileName);
System.err.println(sourceFileName + ": INFO: Creating ICU ResourceBundle: "+outputFileName);
//TODO: fix me
writeHeader(writer,sourceFileName);
@ -2877,7 +2877,7 @@ public class LDML2ICUConverter {
writer.flush();
writer.close();
} catch (Exception ie) {
System.err.println("ERROR :" + ie.toString());
System.err.println(sourceFileName + ": ERROR :" + ie.toString());
return;
}
}

View file

@ -970,9 +970,12 @@ public class LDMLUtilities {
// Local class: cheap non-printing ErrorHandler
// This is used to suppress validation warnings
final String filename2 = filename;
ErrorHandler nullHandler = new ErrorHandler() {
public void warning(SAXParseException e) throws SAXException {System.err.println("WARNING: " + e.getMessage());}
public void error(SAXParseException e) throws SAXException {System.err.println("ERROR: " + e.getMessage());}
public void warning(SAXParseException e) throws SAXException { int col = e.getColumnNumber();
System.err.println(filename2 + ":" + e.getLineNumber() + (col>=0?":" + col:"") + ": WARNING: " + e.getMessage());}
public void error(SAXParseException e) throws SAXException { int col = e.getColumnNumber();
System.err.println(filename2 + ":" + e.getLineNumber() + (col>=0?":" + col:"") + ": ERROR: " + e.getMessage());}
public void fatalError(SAXParseException e) throws SAXException
{
throw e;
@ -984,13 +987,14 @@ public class LDMLUtilities {
{
// First, attempt to parse as XML (preferred)...
DocumentBuilder docBuilder = dfactory.newDocumentBuilder();
docBuilder.setEntityResolver(new CachingEntityResolver());
docBuilder.setErrorHandler(nullHandler);
doc = docBuilder.parse(docSrc);
}
catch (Throwable se)
{
// ... if we couldn't parse as XML, attempt parse as HTML...
System.out.println("ERROR :" + se.getMessage());
System.err.println(filename + ": ERROR :" + se.getMessage());
if(!ignoreError){
throw new RuntimeException(se);
}

View file

@ -31,6 +31,7 @@ import javax.xml.parsers.DocumentBuilderFactory;
// SAX2 imports
import org.xml.sax.ErrorHandler;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
@ -150,6 +151,7 @@ public class XMLValidator {
// First, attempt to parse as XML (preferred)...
DocumentBuilder docBuilder = dfactory.newDocumentBuilder();
docBuilder.setErrorHandler(nullHandler);
docBuilder.setEntityResolver(new CachingEntityResolver());
//if(docBuilder.isValidating()){
//System.out.println("The parser is a validating parser");
//}