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.cache.impl;
043:
044: import antlr.collections.AST;
045: import java.io.IOException;
046: import java.io.ObjectInputStream;
047: import java.io.ObjectOutputStream;
048:
049: /**
050: * Misc. static cache-related utilitiy finctions
051: * @author Vladimir Kvasihn
052: */
053: public class CacheUtil {
054:
055: public static String mangleName(CharSequence fileName, char filler) {
056: StringBuilder sb = new StringBuilder();
057: for (int i = 0; i < fileName.length(); i++) {
058: char c = fileName.charAt(i);
059: if (c == '\\' || c == '/' || c == ':' || c == ' ') {
060: c = filler;
061: }
062: if (i > 0 || c != filler) { // don't add first filler
063: sb.append(c);
064: }
065: }
066: return sb.toString();
067: }
068:
069: static public void writeAST(ObjectOutputStream out, AST ast)
070: throws IOException {
071: out.writeObject(ast);
072: if (ast != null) {
073: // the tree structure has a lot of siblings =>
074: // StackOverflow exceptions during serialization of "next" field
075: // we try to prevent it by using own procedure of writing
076: // tree structure
077: writeTree(out, ast);
078: }
079: }
080:
081: // symmetric to writeObject
082: static public AST readAST(ObjectInputStream in) throws IOException,
083: ClassNotFoundException {
084: AST ast = (AST) in.readObject();
085: if (ast != null) {
086: // read tree structure into this node
087: readTree(in, ast);
088: }
089: return ast;
090: }
091:
092: ////////////////////////////////////////////////////////////////////////////
093: // we have StackOverflow when serialize AST due to it's tree structure:
094: // to many recurse calls to writeObject on writing "next" field
095: // let's try to reduce depth of recursion by depth of tree
096:
097: private static final int CHILD = 1;
098: private static final int SIBLING = 2;
099: private static final int END_AST = 3;
100:
101: static private void writeTree(ObjectOutputStream out, AST root)
102: throws IOException {
103: assert (root != null) : "there must be something to write";
104: AST node = root;
105: do {
106: AST child = node.getFirstChild();
107: if (child != null) {
108: // due to not huge depth of the tree
109: // write child without optimization
110: out.writeInt(CHILD);
111: writeAST(out, child);
112: }
113: node = node.getNextSibling();
114: if (node != null) {
115: // we don't want to use recursion on writing sibling
116: // to prevent StackOverflow,
117: // we use while loop for writing siblings
118: out.writeInt(SIBLING);
119: // write node data
120: out.writeObject(node);
121: }
122: } while (node != null);
123: out.writeInt(END_AST);
124: }
125:
126: static private void readTree(ObjectInputStream in, AST root)
127: throws IOException, ClassNotFoundException {
128: assert (root != null) : "there must be something to read";
129: AST node = root;
130: do {
131: int kind = in.readInt();
132: switch (kind) {
133: case END_AST:
134: return;
135: case CHILD:
136: node.setFirstChild(readAST(in));
137: break;
138: case SIBLING:
139: AST sibling = (AST) in.readObject();
140: node.setNextSibling(sibling);
141: node = sibling;
142: break;
143: default:
144: assert (false);
145: }
146: } while (node != null);
147: }
148: }
|