/* ****************************************************************************** * Copyright (C) 2004-2005, 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 org.unicode.cldr.util; /** * @author Steven R Loomis * * Caching entity resolver */ import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.InputStreamReader; import java.net.MalformedURLException; import org.xml.sax.EntityResolver; import org.xml.sax.InputSource; /** * Use this class to cache DTDs, speeding up tools. */ public class CachingEntityResolver implements EntityResolver { static final String CLDR_DTD_CACHE = "CLDR_DTD_CACHE"; static final String CLDR_DTD_OVERRIDE = "CLDR_DTD_OVERRIDE"; private static String gCacheDir = null; private static String gOverrideDir = System.getProperty(CLDR_DTD_OVERRIDE); private static boolean gCheckedEnv = false; private static boolean gDebug = false; // TODO: synch? /** * Create the cache dir if it doesn't exist. * delete all regular files within the cache dir. */ public static void createAndEmptyCacheDir() { if(getCacheDir() == null) { return; } File cacheDir = new File(getCacheDir()); cacheDir.mkdir(); File cachedFiles[] = cacheDir.listFiles(); if(cachedFiles != null) { for(int i=0;i0) { // File f = new File(tmpdir); // if(f.canWrite()) { // } // } // for (Object s : System.getProperties().keySet() ) { // //System.err.println("tmp was " + System.getProperty("java.tmp")); // System.err.println(s); // } } if(gDebug) { System.out.println("CRE: " + CLDR_DTD_CACHE + " = " + gCacheDir); } if((gCacheDir==null)||(gCacheDir.length()<=0)) { gCacheDir = null; } if((gOverrideDir==null)||(gOverrideDir.length()<=0)) { gOverrideDir = null; } gCheckedEnv=true; } return gCacheDir; } public InputSource resolveEntity (String publicId, String systemId) { boolean aDebug = gDebug; if((System.getProperty("CLDR_DTD_CACHE_DEBUG")!=null) || "y".equals(System.getProperty("CLDR_DTD_CACHE_ADEBUG"))) { aDebug = true; } String theCache = getCacheDir(); if(aDebug) { System.out.println("CRE: " + publicId + " | " + systemId + ", cache=" + theCache+", override="+gOverrideDir); } if(theCache!=null) { int i; if(systemId.startsWith("/")) { File xFile = new File(systemId); if(xFile.canRead()) { String newUrl; try { newUrl = xFile.toURI().toURL().toString(); if(gDebug) System.err.println("CRE: redir to " + newUrl); InputSource is = new InputSource(newUrl); is.setEncoding("UTF8"); return is; } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } if(systemId.startsWith("file:") || !systemId.startsWith("http://")) { // File xFile = new File(systemId); // if(xFile.canRead()) { return new InputSource(systemId); // } // return null; } StringBuffer systemNew = new StringBuffer(systemId); // char c = systemNew.charAt(0); // if((c=='.')||(c=='/')) { // return null; // } for(i=0;i