Revision: 39fe9f717670 Branch: default Author: Brandon Roller <brandon.r.roller@xxxxxxxxx> Date: Mon Jul 22 09:17:20 2013 Log: Added an entity resolver for online resources http://code.google.com/p/brailleblaster/source/detail?r=39fe9f717670&repo=newdesign Added: /src/main/org/brailleblaster/document/MyReader.java /src/main/org/brailleblaster/document/Resolver.java Modified: /src/main/org/brailleblaster/BBIni.java /src/main/org/brailleblaster/document/BBDocument.java /src/main/org/brailleblaster/document/Normalizer.java /src/main/org/brailleblaster/wordprocessor/BBMenu.java /src/main/org/brailleblaster/wordprocessor/DocumentManager.java ======================================= --- /dev/null+++ /src/main/org/brailleblaster/document/MyReader.java Mon Jul 22 09:17:20 2013
@@ -0,0 +1,17 @@ +package org.brailleblaster.document; + +import org.brailleblaster.BBIni; +import org.brailleblaster.wordprocessor.Encodings; +import org.xml.sax.InputSource; + +public class MyReader extends InputSource{ + public MyReader(String publicId, String systemId){ + this.setEncoding(Encodings.UTF_8.encoding()); + this.setPublicId(publicId); + this.setSystemId(systemId); + } + + public void checkForId(){ + + } +} ======================================= --- /dev/null+++ /src/main/org/brailleblaster/document/Resolver.java Mon Jul 22 09:17:20 2013
@@ -0,0 +1,55 @@ +package org.brailleblaster.document; + +import java.io.File; + +import org.brailleblaster.BBIni; + +import org.xml.sax.EntityResolver; +import org.xml.sax.InputSource; + +public class Resolver implements EntityResolver {+ static String dtdPath = BBIni.getProgramDataPath() + BBIni.getFileSep() + "DTDs";
+ String dtdName; + String originalPublicId; + String originalSystemId; + + public InputSource resolveEntity(String publicId, String systemId) { + originalSystemId = systemId; + originalPublicId = publicId; + if (isOnlineResource(systemId) && checkForSystemId(systemId)) { + return new MyReader(publicId, dtdName); + } else { + // use default + return null; + } + } + + private boolean isOnlineResource(String systemId){ + if(systemId.length() >= 4 && systemId.substring(0, 4).contains("http")) + return true; + else + return false; + } + + private boolean checkForSystemId(String systemId){ + dtdName = dtdPath + BBIni.getFileSep() + getDTD(systemId); + File f = new File(dtdName); + if(f.exists()){ + return true; + } + else + return false; + } + + private String getDTD(String systemId){ + return systemId.substring(systemId.lastIndexOf("/") + 1); + } + + public String getOriginalSystemId(){ + return originalSystemId; + } + + public String getOriginalpubId(){ + return originalPublicId; + } +} ======================================= --- /src/main/org/brailleblaster/BBIni.java Sun Jul 21 10:44:14 2013 +++ /src/main/org/brailleblaster/BBIni.java Mon Jul 22 09:17:20 2013 @@ -182,13 +182,14 @@ // Default Config File. /////////////////////// - + //Temporary fix, should be removed once log file handle issue is resolved String tempFolder = BBHome + fileSep + "temp"; fu.deleteDirectory(new File(tempFolder)); tempFilesPath = BBHome + fileSep + "temp" + fileSep + instanceId; File temps = new File(tempFilesPath); + if (!temps.exists()){ temps.mkdirs(); } @@ -270,7 +271,7 @@ return url; } - + private void makeUserProgramData() { String basePath = userProgramDataPath + fileSep; helpMakeUPD(basePath + "liblouis" + fileSep + "tables"); =======================================--- /src/main/org/brailleblaster/document/BBDocument.java Wed Jul 17 12:21:04 2013 +++ /src/main/org/brailleblaster/document/BBDocument.java Mon Jul 22 09:17:20 2013
@@ -42,6 +42,7 @@ import nu.xom.Attribute; import nu.xom.Builder; +import nu.xom.DocType; import nu.xom.Document; import nu.xom.Element; import nu.xom.Elements; @@ -69,6 +70,8 @@ private FileUtils fu = new FileUtils(); static Logger logger = BBIni.getLogger(); private ArrayList<String>missingSemanticsList; + private String systemId; + private String publicId; public BBDocument(DocumentManager dm){ this.missingSemanticsList = new ArrayList<String>(); @@ -101,7 +104,6 @@ String ext = completePath.substring (extPos); if (ext.equalsIgnoreCase ("xml")) {String tempPath = BBIni.getTempFilesPath() + completePath.substring(completePath.lastIndexOf(BBIni.getFileSep()), completePath.lastIndexOf(".")) + "_temp.xml";
-if(normalizeFile(completePath, tempPath) && lutdml.translateFile (configFileWithPath, tempPath, outFile, logFile, configWithUTD, 0)){
deleteFile(tempPath); return buildDOM(outFile); @@ -118,7 +120,9 @@ return buildDOM(outFile); } else if (ext.equalsIgnoreCase ("utd")) { - return buildDOM(completePath);+ String tempPath = BBIni.getTempFilesPath() + completePath.substring(completePath.lastIndexOf(BBIni.getFileSep()), completePath.lastIndexOf(".")) + "_temp.utd";
+ normalizeUTD(completePath, tempPath); + return buildDOM(tempPath); } else {throw new IllegalArgumentException (completePath + " not .xml, .txt, or .brf");
@@ -169,9 +173,14 @@ }private boolean normalizeFile(String originalFilePath, String tempFilePath){
- Normalizer n = new Normalizer(originalFilePath); + Normalizer n = new Normalizer(this, originalFilePath); return n.createNewNormalizedFile(tempFilePath); } ++ private boolean normalizeUTD(String originalFilePath, String tempFilePath){
+ Normalizer n = new Normalizer(this, originalFilePath); + return n.createNewUTDFile(tempFilePath); + } public void updateDOM(MapList list, Message message){ switch(message.type){ @@ -423,6 +432,7 @@ public Document getNewXML(){ Document d = new Document(this.doc); + setOriginalDocType(d); removeAllBraille(d.getRootElement()); return d; } @@ -623,4 +633,17 @@ File f = new File(path); f.delete(); } + + public void setPublicId(String id){ + publicId = id; + } + + public void setSystemId(String id){ + systemId = id; + } + + public void setOriginalDocType(Document d) { + if(this.doc.getDocType() == null)+ d.setDocType(new DocType(this.getRootElement().getLocalName(), publicId, systemId));
+ } } =======================================--- /src/main/org/brailleblaster/document/Normalizer.java Wed Jul 17 12:13:32 2013 +++ /src/main/org/brailleblaster/document/Normalizer.java Mon Jul 22 09:17:20 2013
@@ -11,6 +11,7 @@ import org.brailleblaster.util.Notify; import org.w3c.dom.Document; +import org.w3c.dom.DocumentType; import org.w3c.dom.NodeList; import org.w3c.dom.Element; import org.w3c.dom.Text; @@ -19,7 +20,6 @@ import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.ParserConfigurationException; - import javax.xml.transform.Transformer; import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerException; @@ -31,14 +31,23 @@ File f; Document doc; static Logger log = BBIni.getLogger(); + String originalPubId; + String originalSystemId; + DocumentType docType; - public Normalizer(String path){ + public Normalizer(BBDocument bbDoc, String path){ this.f = new File(path); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder; + Resolver res = new Resolver(); + try { dBuilder = dbFactory.newDocumentBuilder(); + dBuilder.setEntityResolver(res); this.doc = dBuilder.parse(this.f); + docType = this.doc.getDoctype(); + bbDoc.setPublicId(res.getOriginalpubId()); + bbDoc.setSystemId(res.getOriginalSystemId()); } catch(ConnectException e){new Notify("Brailleblaster failed to access necessary materials from online. Please check your internet connection and try again.");
@@ -72,6 +81,14 @@ normalize(); return write(this.doc, path); } + + return false; + } + + public boolean createNewUTDFile(String path){ + if(this.doc != null){ + return write(this.doc, path); + } return false; } @@ -111,6 +128,7 @@TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer; transformer = transformerFactory.newTransformer(); + DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(new File(path)); transformer.transform(source, result); =======================================--- /src/main/org/brailleblaster/wordprocessor/BBMenu.java Tue Jul 16 11:14:37 2013 +++ /src/main/org/brailleblaster/wordprocessor/BBMenu.java Mon Jul 22 09:17:20 2013
@@ -46,6 +46,7 @@ import org.eclipse.swt.widgets.Menu; import org.eclipse.swt.widgets.MenuItem; + class BBMenu { //This class contains all the menus. final Menu menuBar; @@ -120,6 +121,7 @@ MenuItem tutorialsItem; MenuItem checkUpdatesItem; MenuItem aboutItem; + WPManager wordProc; ArrayList<String> recentDocsList = null; Menu subMen; =======================================--- /src/main/org/brailleblaster/wordprocessor/DocumentManager.java Wed Jul 17 12:21:04 2013 +++ /src/main/org/brailleblaster/wordprocessor/DocumentManager.java Mon Jul 22 09:17:20 2013
@@ -145,7 +145,8 @@ if(workingFilePath.endsWith("xml")){ createXMLFile(workingFilePath); } - else if(workingFilePath.endsWith("utd")) { + else if(workingFilePath.endsWith("utd")) { + this.document.setOriginalDocType(this.document.getDOM()); FileOutputStream os = new FileOutputStream(workingFilePath); Serializer serializer = new Serializer(os, "UTF-8"); serializer.write(this.document.getDOM()); @@ -494,6 +495,7 @@ this.documentName = filePath; } else if(ext.equals("utd")) { + this.document.setOriginalDocType(this.document.getDOM()); FileOutputStream os = new FileOutputStream(filePath); Serializer serializer = new Serializer(os, "UTF-8"); serializer.write(this.document.getDOM());