001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * If you wish your version of this file to be governed by only the CDDL
025: * or only the GPL Version 2, indicate your decision by adding
026: * "[Contributor] elects to include this software in this distribution
027: * under the [CDDL or GPL Version 2] license." If you do not indicate a
028: * single choice of license, a recipient has the option to distribute
029: * your version of this file under either the CDDL, the GPL Version 2 or
030: * to extend the choice of license to its licensees as provided above.
031: * However, if you add GPL Version 2 code and therefore, elected the GPL
032: * Version 2 license, then the option applies only if the new code is
033: * made subject to such option by the copyright holder.
034: *
035: * Contributor(s):
036: *
037: * Portions Copyrighted 2007 Sun Microsystems, Inc.
038: */
039:
040: package org.netbeans.modules.editor.settings.storage.spi;
041:
042: import java.util.Map;
043: import java.util.Set;
044: import java.util.logging.Level;
045: import java.util.logging.Logger;
046: import org.netbeans.modules.editor.settings.storage.EditorSettingsImpl;
047: import org.netbeans.modules.editor.settings.storage.SpiPackageAccessor;
048: import org.openide.filesystems.FileObject;
049: import org.xml.sax.InputSource;
050: import org.xml.sax.SAXException;
051: import org.xml.sax.SAXParseException;
052: import org.xml.sax.ext.LexicalHandler;
053: import org.xml.sax.helpers.DefaultHandler;
054:
055: /**
056: *
057: * @author Vita Stejskal
058: */
059: public abstract class StorageReader<K extends Object, V extends Object>
060: extends DefaultHandler implements LexicalHandler {
061:
062: private static final Logger LOG = Logger
063: .getLogger(StorageReader.class.getName());
064:
065: /**
066: * @since 1.17
067: */
068: protected StorageReader(FileObject processedFile, String mimePath) {
069: assert processedFile != null : "The processedFile parameter must not be null"; //NOI18N
070: assert mimePath != null : "The mimePath parameter must not be null"; //NOI18N
071:
072: this .file = processedFile;
073: this .mimePath = mimePath;
074:
075: boolean moduleFile = false;
076: boolean defaultProfile = false;
077:
078: FileObject parent = this .file.getParent();
079: if (parent != null) {
080: moduleFile = parent.getNameExt().contains("Default"); //NOI18N
081: parent = parent.getParent();
082: if (parent != null) {
083: defaultProfile = parent.getNameExt().contains(
084: EditorSettingsImpl.DEFAULT_PROFILE);
085: }
086: }
087: this .isModuleFile = moduleFile;
088: this .isDefaultProfile = defaultProfile;
089: }
090:
091: // DefaultHandler implementation
092:
093: @Override
094: public void warning(SAXParseException e) throws SAXException {
095: log("warning", e); //NOI18N
096: }
097:
098: @Override
099: public void error(SAXParseException e) throws SAXException {
100: log("error", e); //NOI18N
101: }
102:
103: @Override
104: public void fatalError(SAXParseException e) throws SAXException {
105: throw log("fatal error", e); //NOI18N
106: }
107:
108: @Override
109: public InputSource resolveEntity(String pubid, String sysid) {
110: return new InputSource(new java.io.ByteArrayInputStream(
111: new byte[0]));
112: }
113:
114: // LexicalHandler implementation
115:
116: public void startCDATA() throws SAXException {
117: }
118:
119: public void endCDATA() throws SAXException {
120: }
121:
122: public void comment(char[] ch, int start, int length)
123: throws SAXException {
124: }
125:
126: public void endDTD() throws SAXException {
127: }
128:
129: public void endEntity(String name) throws SAXException {
130: }
131:
132: public void startDTD(String name, String publicId, String systemId)
133: throws SAXException {
134: }
135:
136: public void startEntity(String name) throws SAXException {
137: }
138:
139: // XMLStorage.StorageReader interface
140:
141: public abstract Map<K, V> getAdded();
142:
143: public abstract Set<K> getRemoved();
144:
145: protected final FileObject getProcessedFile() {
146: return this .file;
147: }
148:
149: protected final boolean isModuleFile() {
150: return this .isModuleFile;
151: }
152:
153: protected final boolean isDefaultProfile() {
154: return isDefaultProfile;
155: }
156:
157: /**
158: * @since 1.17
159: */
160: protected final String getMimePath() {
161: return mimePath;
162: }
163:
164: // Private implementation
165:
166: private final FileObject file;
167: private final boolean isModuleFile;
168: private final boolean isDefaultProfile;
169: private final String mimePath;
170:
171: private SAXException log(String errorType, SAXParseException e) {
172: Level level;
173: String message;
174:
175: if (file == null) {
176: level = Level.FINE;
177: message = "XML parser " + errorType;
178: } else {
179: if (isModuleFile()) { //NOI18N
180: level = Level.WARNING; // warnings for module layer supplied files
181: } else {
182: level = Level.FINE; // user files, can be from previous versions
183: }
184:
185: message = "XML parser " + errorType + " in file "
186: + file.getPath();
187: }
188:
189: SAXException saxe = new SAXException(message);
190: saxe.initCause(e);
191: LOG.log(level, message, saxe); //NOI18N
192:
193: return saxe;
194: }
195:
196: // package accessor trick
197:
198: static {
199: SpiPackageAccessor.register(new SpiPackageAccessorImpl());
200: }
201:
202: private static final class SpiPackageAccessorImpl extends
203: SpiPackageAccessor {
204: } // End of SpiPackageAccessorImpl class
205: }
|