KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > content > ContentTypeHandler


1 /*******************************************************************************
2  * Copyright (c) 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.core.internal.content;
12
13 import java.io.*;
14 import java.lang.ref.SoftReference JavaDoc;
15 import org.eclipse.core.runtime.CoreException;
16 import org.eclipse.core.runtime.QualifiedName;
17 import org.eclipse.core.runtime.content.*;
18 import org.eclipse.core.runtime.preferences.IScopeContext;
19
20 /**
21  * The only content types exposed to clients. Allows the content type registry to change
22  * underneath preserving handlers kept by clients.
23  */

24 public class ContentTypeHandler implements IContentType {
25
26     /**
27      * A dummy description object to be returned by getDescription when this
28      * handler's target cannot be determined.
29      */

30     private class DummyContentDescription implements IContentDescription {
31         public String JavaDoc getCharset() {
32             return null;
33         }
34
35         public IContentType getContentType() {
36             return ContentTypeHandler.this;
37         }
38
39         public Object JavaDoc getProperty(QualifiedName key) {
40             return null;
41         }
42
43         public boolean isRequested(QualifiedName key) {
44             return false;
45         }
46
47         public void setProperty(QualifiedName key, Object JavaDoc value) {
48             // don't do anything
49
}
50     }
51
52     private int generation;
53     String JavaDoc id;
54     private SoftReference JavaDoc targetRef;
55
56     ContentTypeHandler(ContentType target, int generation) {
57         this.id = target.getId();
58         this.targetRef = new SoftReference JavaDoc(target);
59         this.generation = generation;
60     }
61
62     public void addFileSpec(String JavaDoc fileSpec, int type) throws CoreException {
63         final IContentType target = getTarget();
64         if (target != null)
65             target.addFileSpec(fileSpec, type);
66     }
67
68     public boolean equals(Object JavaDoc another) {
69         if (another instanceof ContentType)
70             return id.equals(((ContentType) another).id);
71         if (another instanceof ContentTypeHandler)
72             return id.equals(((ContentTypeHandler) another).id);
73         return false;
74     }
75
76     public IContentType getBaseType() {
77         final ContentType target = getTarget();
78         if (target == null)
79             return null;
80         final ContentType baseType = (ContentType) target.getBaseType();
81         return (baseType != null) ? new ContentTypeHandler(baseType, baseType.getCatalog().getGeneration()) : null;
82     }
83
84     public String JavaDoc getDefaultCharset() {
85         final IContentType target = getTarget();
86         return (target != null) ? target.getDefaultCharset() : null;
87     }
88
89     public IContentDescription getDefaultDescription() {
90         final IContentType target = getTarget();
91         return (target != null) ? target.getDefaultDescription() : new DummyContentDescription();
92     }
93
94     public IContentDescription getDescriptionFor(InputStream contents, QualifiedName[] options) throws IOException {
95         final IContentType target = getTarget();
96         return (target != null) ? target.getDescriptionFor(contents, options) : null;
97     }
98
99     public IContentDescription getDescriptionFor(Reader contents, QualifiedName[] options) throws IOException {
100         final IContentType target = getTarget();
101         return (target != null) ? target.getDescriptionFor(contents, options) : null;
102     }
103
104     public String JavaDoc[] getFileSpecs(int type) {
105         final IContentType target = getTarget();
106         return (target != null) ? target.getFileSpecs(type) : new String JavaDoc[0];
107     }
108
109     public String JavaDoc getId() {
110         return id;
111     }
112
113     public String JavaDoc getName() {
114         final IContentType target = getTarget();
115         return (target != null) ? target.getName() : id;
116     }
117
118     public IContentTypeSettings getSettings(IScopeContext context) throws CoreException {
119         final ContentType target = getTarget();
120         if (target == null)
121             return null;
122         // the content type may returned itself as the settings object (instance scope context)
123
final IContentTypeSettings settings = target.getSettings(context);
124         // in that case, return this same handler; otherwise, just return the settings
125
return settings == target ? this : settings;
126     }
127
128     /**
129      * Returns the content type this handler represents.
130      * Note that this handles the case of aliasing.
131      *
132      * Public for testing purposes only.
133      */

134     public ContentType getTarget() {
135         ContentType target = (ContentType) targetRef.get();
136         ContentTypeCatalog catalog = ContentTypeManager.getInstance().getCatalog();
137         if (target == null || catalog.getGeneration() != generation) {
138             target = catalog.getContentType(id);
139             targetRef = new SoftReference JavaDoc(target);
140             generation = catalog.getGeneration();
141         }
142         return target == null ? null : target.getAliasTarget(true);
143     }
144
145     public int hashCode() {
146         return id.hashCode();
147     }
148
149     public boolean isAssociatedWith(String JavaDoc fileName) {
150         final IContentType target = getTarget();
151         return (target != null) ? target.isAssociatedWith(fileName) : false;
152     }
153
154     public boolean isAssociatedWith(String JavaDoc fileName, IScopeContext context) {
155         final IContentType target = getTarget();
156         return (target != null) ? target.isAssociatedWith(fileName, context) : false;
157     }
158
159     public boolean isKindOf(IContentType another) {
160         if (another instanceof ContentTypeHandler)
161             another = ((ContentTypeHandler) another).getTarget();
162         final IContentType target = getTarget();
163         return (target != null) ? target.isKindOf(another) : false;
164     }
165
166     public void removeFileSpec(String JavaDoc fileSpec, int type) throws CoreException {
167         final IContentType target = getTarget();
168         if (target != null)
169             target.removeFileSpec(fileSpec, type);
170     }
171
172     public void setDefaultCharset(String JavaDoc userCharset) throws CoreException {
173         final IContentType target = getTarget();
174         if (target != null)
175             target.setDefaultCharset(userCharset);
176     }
177
178     public String JavaDoc toString() {
179         return id;
180     }
181
182 }
183
Popular Tags