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.csm.core;
043:
044: import java.io.DataInput;
045: import java.io.DataOutput;
046: import java.io.IOException;
047: import org.netbeans.modules.cnd.api.model.CsmFriendFunction;
048: import org.netbeans.modules.cnd.modelimpl.csm.ClassForwardDeclarationImpl;
049: import org.netbeans.modules.cnd.modelimpl.csm.ClassImpl;
050: import org.netbeans.modules.cnd.modelimpl.csm.ConstructorDDImpl;
051: import org.netbeans.modules.cnd.modelimpl.csm.ConstructorDefinitionImpl;
052: import org.netbeans.modules.cnd.modelimpl.csm.ConstructorImpl;
053: import org.netbeans.modules.cnd.modelimpl.csm.DestructorDDImpl;
054: import org.netbeans.modules.cnd.modelimpl.csm.DestructorDefinitionImpl;
055: import org.netbeans.modules.cnd.modelimpl.csm.DestructorImpl;
056: import org.netbeans.modules.cnd.modelimpl.csm.EnumImpl;
057: import org.netbeans.modules.cnd.modelimpl.csm.EnumeratorImpl;
058: import org.netbeans.modules.cnd.modelimpl.csm.FieldImpl;
059: import org.netbeans.modules.cnd.modelimpl.csm.FriendClassImpl;
060: import org.netbeans.modules.cnd.modelimpl.csm.FriendFunctionDDImpl;
061: import org.netbeans.modules.cnd.modelimpl.csm.FriendFunctionDefinitionImpl;
062: import org.netbeans.modules.cnd.modelimpl.csm.FriendFunctionImpl;
063: import org.netbeans.modules.cnd.modelimpl.csm.FriendFunctionImplEx;
064: import org.netbeans.modules.cnd.modelimpl.csm.FunctionDDImpl;
065: import org.netbeans.modules.cnd.modelimpl.csm.FunctionDefinitionImpl;
066: import org.netbeans.modules.cnd.modelimpl.csm.FunctionImpl;
067: import org.netbeans.modules.cnd.modelimpl.csm.FunctionImplEx;
068: import org.netbeans.modules.cnd.modelimpl.csm.IncludeImpl;
069: import org.netbeans.modules.cnd.modelimpl.csm.MacroImpl;
070: import org.netbeans.modules.cnd.modelimpl.csm.MethodDDImpl;
071: import org.netbeans.modules.cnd.modelimpl.csm.MethodImpl;
072: import org.netbeans.modules.cnd.modelimpl.csm.NamespaceAliasImpl;
073: import org.netbeans.modules.cnd.modelimpl.csm.NamespaceDefinitionImpl;
074: import org.netbeans.modules.cnd.modelimpl.csm.NamespaceImpl;
075: import org.netbeans.modules.cnd.modelimpl.csm.ParameterImpl;
076: import org.netbeans.modules.cnd.modelimpl.csm.TypedefImpl;
077: import org.netbeans.modules.cnd.modelimpl.csm.UsingDeclarationImpl;
078: import org.netbeans.modules.cnd.modelimpl.csm.UsingDirectiveImpl;
079: import org.netbeans.modules.cnd.modelimpl.csm.VariableDefinitionImpl;
080: import org.netbeans.modules.cnd.modelimpl.csm.VariableImpl;
081: import org.netbeans.modules.cnd.modelimpl.uid.UIDObjectFactory;
082: import org.netbeans.modules.cnd.repository.spi.Persistent;
083: import org.netbeans.modules.cnd.repository.spi.PersistentFactory;
084: import org.netbeans.modules.cnd.repository.support.AbstractObjectFactory;
085: import org.netbeans.modules.cnd.repository.support.SelfPersistent;
086:
087: /**
088: * objects factory
089: * @author Vladimir Voskresensky
090: */
091: public final class CsmObjectFactory extends AbstractObjectFactory
092: implements PersistentFactory {
093:
094: private static final CsmObjectFactory instance = new CsmObjectFactory();
095:
096: private CsmObjectFactory() {
097: }
098:
099: public static CsmObjectFactory instance() {
100: return instance;
101: }
102:
103: public boolean canWrite(Persistent obj) {
104: if (obj instanceof FileImpl) {
105: return ((FileImpl) obj).getBuffer().isFileBased();
106: } else {
107: return true;
108: }
109: }
110:
111: protected int getHandler(Object object) {
112: assert object != null;
113: int aHandler;
114: if (object instanceof LibProjectImpl) {
115: aHandler = LIB_PROJECT_IMPL;
116: } else if (object instanceof ProjectImpl) {
117: aHandler = PROJECT_IMPL;
118: } else if (object instanceof FileContainer) {
119: aHandler = FILES_CONTAINER;
120: } else if (object instanceof GraphContainer) {
121: aHandler = GRAPH_CONTAINER;
122: } else if (object instanceof FileImpl) {
123: aHandler = FILE_IMPL;
124: // } else if (object instanceof Unresolved.UnresolvedFile) {
125: // aHandler = UNRESOLVED_FILE;
126: // } else if (object instanceof Unresolved.UnresolvedClass) {
127: // aHandler = UNRESOLVED_CLASS;
128: } else if (object instanceof EnumImpl) {
129: aHandler = ENUM_IMPL;
130: } else if (object instanceof ClassImpl) {
131: aHandler = CLASS_IMPL;
132: } else if (object instanceof TypedefImpl) {
133: if (object instanceof ClassImpl.MemberTypedef) {
134: aHandler = MEMBER_TYPEDEF;
135: } else {
136: aHandler = TYPEDEF_IMPL;
137: }
138: } else if (object instanceof NamespaceImpl) {
139: aHandler = NAMESPACE_IMPL;
140: } else if (object instanceof NamespaceDefinitionImpl) {
141: aHandler = NAMESPACE_DEF_IMPL;
142: } else if (object instanceof NamespaceAliasImpl) {
143: aHandler = NAMESPACE_ALIAS_IMPL;
144: } else if (object instanceof UsingDeclarationImpl) {
145: aHandler = USING_DECLARATION_IMPL;
146: } else if (object instanceof UsingDirectiveImpl) {
147: aHandler = USING_DIRECTIVE_IMPL;
148: } else if (object instanceof ClassForwardDeclarationImpl) {
149: aHandler = CLASS_FORWARD_DECLARATION_IMPL;
150: } else if (object instanceof FunctionImpl) {
151: // we have several FunctionImpl subclasses
152: if (object instanceof FunctionImplEx) {
153: if (object instanceof FunctionDefinitionImpl) {
154: // we have several FunctionDefinitionImpl subclasses
155: if (object instanceof DestructorDefinitionImpl) {
156: aHandler = DESTRUCTOR_DEF_IMPL;
157: } else if (object instanceof ConstructorDefinitionImpl) {
158: aHandler = CONSTRUCTOR_DEF_IMPL;
159: } else {
160: if (object instanceof CsmFriendFunction) {
161: aHandler = FRIEND_FUNCTION_DEF_IMPL;
162: } else {
163: aHandler = FUNCTION_DEF_IMPL;
164: }
165: }
166: } else {
167: if (object instanceof CsmFriendFunction) {
168: aHandler = FRIEND_FUNCTION_IMPL_EX;
169: } else {
170: aHandler = FUNCTION_IMPL_EX;
171: }
172: }
173: } else if (object instanceof MethodImpl) {
174: // we have several MethodImpl subclusses
175: if (object instanceof MethodDDImpl) {
176: // we have two MethodDDImpl classses:
177: if (object instanceof DestructorDDImpl) {
178: aHandler = DESTRUCTOR_DEF_DECL_IMPL;
179: } else if (object instanceof ConstructorDDImpl) {
180: aHandler = CONSTRUCTOR_DEF_DECL_IMPL;
181: } else {
182: aHandler = METHOD_DEF_DECL_IMPL;
183: }
184: } else if (object instanceof ConstructorImpl) {
185: aHandler = CONSTRUCTOR_IMPL;
186: } else if (object instanceof DestructorImpl) {
187: aHandler = DESTRUCTOR_IMPL;
188: } else {
189: aHandler = METHOD_IMPL;
190: }
191: } else if (object instanceof FunctionDDImpl) {
192: if (object instanceof CsmFriendFunction) {
193: aHandler = FRIEND_FUNCTION_DEF_DECL_IMPL;
194: } else {
195: aHandler = FUNCTION_DEF_DECL_IMPL;
196: }
197: } else {
198: if (object instanceof CsmFriendFunction) {
199: aHandler = FRIEND_FUNCTION_IMPL;
200: } else {
201: aHandler = FUNCTION_IMPL;
202: }
203: }
204: } else if (object instanceof VariableImpl) {
205: // we have several VariableImpl subclasses
206: if (object instanceof VariableDefinitionImpl) {
207: aHandler = VARIABLE_DEF_IMPL;
208: } else if (object instanceof FieldImpl) {
209: aHandler = FIELD_IMPL;
210: } else if (object instanceof ParameterImpl) {
211: aHandler = PARAMETER_IMPL;
212: } else {
213: aHandler = VARIABLE_IMPL;
214: }
215: } else if (object instanceof EnumeratorImpl) {
216: aHandler = ENUMERATOR_IMPL;
217: } else if (object instanceof IncludeImpl) {
218: aHandler = INCLUDE_IMPL;
219: } else if (object instanceof MacroImpl) {
220: aHandler = MACRO_IMPL;
221: } else if (object instanceof FriendClassImpl) {
222: aHandler = FRIEND_CLASS_IMPL;
223: } else if (object instanceof DeclarationContainer) {
224: aHandler = DECLARATION_CONTAINER;
225: } else {
226: throw new IllegalArgumentException(
227: "instance of unknown class "
228: + object.getClass().getName()); //NOI18N
229: }
230: return aHandler;
231: }
232:
233: protected SelfPersistent createObject(int handler, DataInput stream)
234: throws IOException {
235: SelfPersistent obj;
236:
237: switch (handler) {
238: case PROJECT_IMPL:
239: obj = new ProjectImpl(stream);
240: break;
241:
242: case LIB_PROJECT_IMPL:
243: obj = new LibProjectImpl(stream);
244: break;
245:
246: case FILES_CONTAINER:
247: obj = new FileContainer(stream);
248: break;
249:
250: case GRAPH_CONTAINER:
251: obj = new GraphContainer(stream);
252: break;
253:
254: case FILE_IMPL:
255: obj = new FileImpl(stream);
256: break;
257:
258: // case UNRESOLVED_FILE:
259: // obj = new Unresolved.UnresolvedFile(stream);
260: // break;
261: //
262: // case UNRESOLVED_CLASS:
263: // obj = new Unresolved.UnresolvedClass(stream);
264: // break;
265:
266: case ENUM_IMPL:
267: obj = new EnumImpl(stream);
268: break;
269:
270: case CLASS_IMPL:
271: obj = new ClassImpl(stream);
272: break;
273:
274: case TYPEDEF_IMPL:
275: obj = new TypedefImpl(stream);
276: break;
277:
278: case MEMBER_TYPEDEF:
279: obj = new ClassImpl.MemberTypedef(stream);
280: break;
281:
282: case NAMESPACE_IMPL:
283: obj = new NamespaceImpl(stream);
284: break;
285:
286: case NAMESPACE_DEF_IMPL:
287: obj = new NamespaceDefinitionImpl(stream);
288: break;
289:
290: case NAMESPACE_ALIAS_IMPL:
291: obj = new NamespaceAliasImpl(stream);
292: break;
293:
294: case USING_DECLARATION_IMPL:
295: obj = new UsingDeclarationImpl(stream);
296: break;
297:
298: case USING_DIRECTIVE_IMPL:
299: obj = new UsingDirectiveImpl(stream);
300: break;
301:
302: case CLASS_FORWARD_DECLARATION_IMPL:
303: obj = new ClassForwardDeclarationImpl(stream);
304: break;
305:
306: case FUNCTION_IMPL:
307: obj = new FunctionImpl(stream);
308: break;
309:
310: case FUNCTION_IMPL_EX:
311: obj = new FunctionImplEx(stream);
312: break;
313:
314: case DESTRUCTOR_DEF_IMPL:
315: obj = new DestructorDefinitionImpl(stream);
316: break;
317:
318: case CONSTRUCTOR_DEF_IMPL:
319: obj = new ConstructorDefinitionImpl(stream);
320: break;
321:
322: case CONSTRUCTOR_DEF_DECL_IMPL:
323: obj = new ConstructorDDImpl(stream);
324: break;
325:
326: case FUNCTION_DEF_IMPL:
327: obj = new FunctionDefinitionImpl(stream);
328: break;
329:
330: case DESTRUCTOR_DEF_DECL_IMPL:
331: obj = new DestructorDDImpl(stream);
332: break;
333:
334: case METHOD_DEF_DECL_IMPL:
335: obj = new MethodDDImpl(stream);
336: break;
337:
338: case CONSTRUCTOR_IMPL:
339: obj = new ConstructorImpl(stream);
340: break;
341:
342: case DESTRUCTOR_IMPL:
343: obj = new DestructorImpl(stream);
344: break;
345:
346: case METHOD_IMPL:
347: obj = new MethodImpl(stream);
348: break;
349:
350: case FUNCTION_DEF_DECL_IMPL:
351: obj = new FunctionDDImpl(stream);
352: break;
353:
354: case VARIABLE_DEF_IMPL:
355: obj = new VariableDefinitionImpl(stream);
356: break;
357:
358: case FIELD_IMPL:
359: obj = new FieldImpl(stream);
360: break;
361:
362: case PARAMETER_IMPL:
363: obj = new ParameterImpl(stream);
364: break;
365:
366: case VARIABLE_IMPL:
367: obj = new VariableImpl(stream);
368: break;
369:
370: case ENUMERATOR_IMPL:
371: obj = new EnumeratorImpl(stream);
372: break;
373:
374: case INCLUDE_IMPL:
375: obj = new IncludeImpl(stream);
376: break;
377:
378: case MACRO_IMPL:
379: obj = new MacroImpl(stream);
380: break;
381:
382: case FRIEND_CLASS_IMPL:
383: obj = new FriendClassImpl(stream);
384: break;
385:
386: case FRIEND_FUNCTION_IMPL:
387: obj = new FriendFunctionImpl(stream);
388: break;
389:
390: case FRIEND_FUNCTION_IMPL_EX:
391: obj = new FriendFunctionImplEx(stream);
392: break;
393:
394: case FRIEND_FUNCTION_DEF_IMPL:
395: obj = new FriendFunctionDefinitionImpl(stream);
396: break;
397:
398: case FRIEND_FUNCTION_DEF_DECL_IMPL:
399: obj = new FriendFunctionDDImpl(stream);
400: break;
401:
402: case DECLARATION_CONTAINER:
403: obj = new DeclarationContainer(stream);
404: break;
405:
406: default:
407: throw new IllegalArgumentException("unknown handler"
408: + handler); //NOI18N
409: }
410: return obj;
411: }
412:
413: public void write(DataOutput out, Persistent obj)
414: throws IOException {
415: SelfPersistent persistentObj = (SelfPersistent) obj;
416: super .writeSelfPersistent(persistentObj, out);
417: }
418:
419: public Persistent read(DataInput in) throws IOException {
420: SelfPersistent persistentObj = super .readSelfPersistent(in);
421: assert persistentObj == null
422: || persistentObj instanceof Persistent;
423: return (Persistent) persistentObj;
424: }
425:
426: ///////////////////////////////////////////////////////////////////////////////////
427: // handlers to identify different classes of projects
428:
429: private static final int FIRST_INDEX = UIDObjectFactory.LAST_INDEX + 1;
430:
431: private static final int PROJECT_IMPL = FIRST_INDEX;
432: private static final int LIB_PROJECT_IMPL = PROJECT_IMPL + 1;
433: private static final int FILES_CONTAINER = LIB_PROJECT_IMPL + 1;
434: private static final int GRAPH_CONTAINER = FILES_CONTAINER + 1;
435: private static final int DECLARATION_CONTAINER = GRAPH_CONTAINER + 1;
436: private static final int FILE_IMPL = DECLARATION_CONTAINER + 1;
437: private static final int ENUM_IMPL = FILE_IMPL + 1;
438: private static final int CLASS_IMPL = ENUM_IMPL + 1;
439: // private static final int UNRESOLVED_FILE = CLASS_IMPL + 1;
440: // private static final int UNRESOLVED_CLASS = UNRESOLVED_FILE + 1;
441: // private static final int TYPEDEF_IMPL = UNRESOLVED_CLASS + 1;
442: private static final int TYPEDEF_IMPL = CLASS_IMPL + 1;
443: private static final int MEMBER_TYPEDEF = TYPEDEF_IMPL + 1;
444: private static final int NAMESPACE_IMPL = MEMBER_TYPEDEF + 1;
445: private static final int NAMESPACE_DEF_IMPL = NAMESPACE_IMPL + 1;
446: private static final int NAMESPACE_ALIAS_IMPL = NAMESPACE_DEF_IMPL + 1;
447: private static final int USING_DECLARATION_IMPL = NAMESPACE_ALIAS_IMPL + 1;
448: private static final int USING_DIRECTIVE_IMPL = USING_DECLARATION_IMPL + 1;
449: private static final int CLASS_FORWARD_DECLARATION_IMPL = USING_DIRECTIVE_IMPL + 1;
450: private static final int FRIEND_CLASS_IMPL = CLASS_FORWARD_DECLARATION_IMPL + 1;
451:
452: // functions
453: private static final int FUNCTION_IMPL = FRIEND_CLASS_IMPL + 1;
454: private static final int FUNCTION_IMPL_EX = FUNCTION_IMPL + 1;
455:
456: //// function definitons
457: private static final int DESTRUCTOR_DEF_IMPL = FUNCTION_IMPL_EX + 1;
458: private static final int CONSTRUCTOR_DEF_IMPL = DESTRUCTOR_DEF_IMPL + 1;
459: private static final int FUNCTION_DEF_IMPL = CONSTRUCTOR_DEF_IMPL + 1;
460:
461: //// friends
462: private static final int FRIEND_FUNCTION_IMPL = FUNCTION_DEF_IMPL + 1;
463: private static final int FRIEND_FUNCTION_IMPL_EX = FRIEND_FUNCTION_IMPL + 1;
464: private static final int FRIEND_FUNCTION_DEF_IMPL = FRIEND_FUNCTION_IMPL_EX + 1;
465: private static final int FRIEND_FUNCTION_DEF_DECL_IMPL = FRIEND_FUNCTION_DEF_IMPL + 1;
466:
467: //// methods
468: private static final int CONSTRUCTOR_DEF_DECL_IMPL = FRIEND_FUNCTION_DEF_DECL_IMPL + 1;
469: private static final int DESTRUCTOR_DEF_DECL_IMPL = CONSTRUCTOR_DEF_DECL_IMPL + 1;
470: private static final int METHOD_DEF_DECL_IMPL = DESTRUCTOR_DEF_DECL_IMPL + 1;
471: private static final int CONSTRUCTOR_IMPL = METHOD_DEF_DECL_IMPL + 1;
472: private static final int DESTRUCTOR_IMPL = CONSTRUCTOR_IMPL + 1;
473: private static final int METHOD_IMPL = DESTRUCTOR_IMPL + 1;
474:
475: private static final int FUNCTION_DEF_DECL_IMPL = METHOD_IMPL + 1;
476: // end of functions
477:
478: // variables
479: private static final int VARIABLE_IMPL = FUNCTION_DEF_DECL_IMPL + 1;
480: private static final int VARIABLE_DEF_IMPL = VARIABLE_IMPL + 1;
481: private static final int FIELD_IMPL = VARIABLE_DEF_IMPL + 1;
482: private static final int PARAMETER_IMPL = FIELD_IMPL + 1;
483:
484: private static final int ENUMERATOR_IMPL = PARAMETER_IMPL + 1;
485:
486: private static final int INCLUDE_IMPL = ENUMERATOR_IMPL + 1;
487: private static final int MACRO_IMPL = INCLUDE_IMPL + 1;
488:
489: // index to be used in another factory (but only in one)
490: // to start own indeces from the next after LAST_INDEX
491: public static final int LAST_INDEX = MACRO_IMPL;
492: }
|