1 19 20 package org.netbeans.modules.editor.settings.storage; 21 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 import java.io.OutputStream ; 25 import java.util.logging.Level ; 26 import java.util.logging.Logger ; 27 import org.openide.filesystems.FileLock; 28 29 import org.openide.filesystems.FileObject; 30 import org.openide.xml.EntityCatalog; 31 import org.openide.xml.XMLUtil; 32 import org.w3c.dom.Document ; 33 import org.xml.sax.InputSource ; 34 import org.xml.sax.SAXException ; 35 import org.xml.sax.SAXParseException ; 36 import org.xml.sax.XMLReader ; 37 import org.xml.sax.helpers.DefaultHandler ; 38 39 40 public final class XMLStorage { 41 42 private static final Logger LOG = Logger.getLogger(XMLStorage.class.getName()); 43 44 private XMLStorage() { 45 46 } 47 48 public static void save(FileObject fo, Document doc) { 49 assert fo != null : "FileObject can't be null"; assert doc != null : "Document can't be null"; 52 try { 53 FileLock lock = fo.lock(); 54 try { 55 OutputStream os = fo.getOutputStream(lock); 56 try { 57 XMLUtil.write(doc, os, "UTF-8"); } finally { 59 os.close(); 60 } 61 } finally { 62 lock.releaseLock(); 63 } 64 } catch (IOException ex) { 65 LOG.log(Level.WARNING, "Can't save editor settings to " + fo.getPath(), ex); } 67 } 68 69 public static Object load(FileObject fo, Handler handler) { 70 assert fo != null : "Settings file must not be null"; 72 handler.setProcessedFile(fo); 73 try { 74 XMLReader reader = XMLUtil.createXMLReader(true); 75 reader.setEntityResolver(EntityCatalog.getDefault()); 76 reader.setContentHandler(handler); 77 reader.setErrorHandler(handler); 78 79 InputStream is = fo.getInputStream(); 80 try { 81 reader.parse(new InputSource (is)); 82 } finally { 83 is.close(); 84 } 85 } catch (Exception ex) { 86 LOG.log(Level.WARNING, "Invalid or corrupted file: " + fo.getPath(), ex); } finally { 88 handler.setProcessedFile(null); 89 } 90 91 return handler.getResult(); 92 } 93 94 public static class Handler extends DefaultHandler { 95 private FileObject file; 96 private boolean isModuleFile; 97 98 private Object result; 99 100 public void setResult (Object result) { 101 this.result = result; 102 } 103 104 public Object getResult () { 105 return result; 106 } 107 108 public void warning(SAXParseException e) throws SAXException { 109 log("warning", e); } 111 112 public void error(SAXParseException e) throws SAXException { 113 log("error", e); } 115 116 public void fatalError(SAXParseException e) throws SAXException { 117 log("fatal error", e); throw e; 119 } 120 121 private void log(String errorType, SAXParseException e) { 122 if (file == null) { 123 LOG.log(Level.FINE, "XML parser " + errorType, e); } else { 125 Level level; 126 if (isModuleFile()) { level = Level.WARNING; } else { 129 level = Level.FINE; } 131 132 LOG.log(level, "XML parser " + errorType + " in file " + file.getPath(), e); } 134 } 135 136 protected final FileObject getProcessedFile() { 137 return this.file; 138 } 139 140 protected final boolean isModuleFile() { 141 return this.isModuleFile; 142 } 143 144 private void setProcessedFile(FileObject f) { 145 this.file = f; 146 if (this.file != null) { 147 FileObject parent = this.file.getParent(); 148 this.isModuleFile = parent != null && parent.getNameExt().contains("Default"); } else { 150 this.isModuleFile = false; 151 } 152 } 153 } } 155 | Popular Tags |