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: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.cnd.modelimpl.trace;
043:
044: import java.io.*;
045: import antlr.collections.*;
046:
047: import java.util.zip.*;
048: import org.netbeans.modules.cnd.modelimpl.debug.TraceFlags;
049:
050: /**
051: *
052: * @author Vladimir Kvashin
053: */
054: public class Cache {
055:
056: private boolean useZip;
057:
058: private interface Delegate {
059: public void writeAst(AST tree, File file) throws IOException;
060:
061: public AST readAst(File file) throws IOException,
062: ClassNotFoundException;
063: }
064:
065: private static class AstWrapper implements Serializable {
066: public AST ast;
067:
068: public AstWrapper(AST ast) {
069: this .ast = ast;
070: }
071: }
072:
073: private abstract class Base implements Delegate {
074:
075: abstract protected InputStream getFileInputStream(File srcFile)
076: throws IOException;
077:
078: abstract protected OutputStream getFileOutputStream(File srcFile)
079: throws IOException;
080:
081: public void writeAst(AST tree, File srcFile) throws IOException {
082: ObjectOutput out = new ObjectOutputStream(
083: getFileOutputStream(srcFile));
084: AstWrapper w = new AstWrapper(tree);
085: out.writeObject(w);
086: out.close();
087: }
088:
089: public AST readAst(File srcFile) throws IOException,
090: ClassNotFoundException {
091: AstWrapper w = null;
092: ObjectInputStream in = new ObjectInputStream(
093: getFileInputStream(srcFile));
094: w = (AstWrapper) in.readObject();
095: in.close();
096: return w.ast;
097: }
098: }
099:
100: private class Plain extends Base implements Delegate {
101:
102: private File cacheDir;
103:
104: public Plain() {
105: cacheDir = new File("test-model-cache"); // NOI18N
106: cacheDir.mkdirs();
107: }
108:
109: protected InputStream getFileInputStream(File srcFile)
110: throws IOException {
111: File cache = getCacheFile(srcFile);
112: return new BufferedInputStream(new FileInputStream(cache),
113: TraceFlags.BUF_SIZE);
114: }
115:
116: protected OutputStream getFileOutputStream(File srcFile)
117: throws IOException {
118: File cache = getCacheFile(srcFile);
119: return new FileOutputStream(cache);
120: }
121:
122: protected File getCacheFile(File sourceFile) {
123: return new File(cacheDir, getEntryName(sourceFile));
124: }
125:
126: protected String getEntryName(File sourceFile) {
127: //return sourceFile.getName() + ".ast";
128: return sourceFile.getAbsolutePath().substring(1) + ".ast"; // NOI18N
129: }
130:
131: }
132:
133: private class Zipped extends Plain implements Delegate {
134:
135: private ZipFile zip;
136:
137: public Zipped() throws IOException {
138: zip = new ZipFile(new File("test-model-cache.zip"),
139: ZipFile.OPEN_READ); // NOI18N
140: }
141:
142: protected InputStream getFileInputStream(File srcFile)
143: throws IOException {
144: String entryName = getEntryName(srcFile);
145: ZipEntry entry = zip.getEntry(entryName);
146: if (entry == null) {
147: throw new ZipException("Entry not found: " + entryName); // NOI18N
148: } else {
149: InputStream is = zip.getInputStream(entry);
150: return is;
151: }
152: }
153:
154: }
155:
156: Delegate delegate;
157:
158: public Cache(boolean useZip) throws IOException {
159: delegate = useZip ? (Delegate) new Zipped()
160: : (Delegate) new Plain();
161: }
162:
163: public void writeAst(AST tree, File sourcFile) throws IOException {
164: delegate.writeAst(tree, sourcFile);
165: }
166:
167: public AST readAst(File sourcFile) throws IOException,
168: ClassNotFoundException {
169: return delegate.readAst(sourcFile);
170: }
171:
172: }
|