1 11 package org.eclipse.core.internal.content; 12 13 import java.io.*; 14 import java.lang.ref.SoftReference ; 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 24 public class ContentTypeHandler implements IContentType { 25 26 30 private class DummyContentDescription implements IContentDescription { 31 public String getCharset() { 32 return null; 33 } 34 35 public IContentType getContentType() { 36 return ContentTypeHandler.this; 37 } 38 39 public Object 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 value) { 48 } 50 } 51 52 private int generation; 53 String id; 54 private SoftReference targetRef; 55 56 ContentTypeHandler(ContentType target, int generation) { 57 this.id = target.getId(); 58 this.targetRef = new SoftReference (target); 59 this.generation = generation; 60 } 61 62 public void addFileSpec(String 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 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 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 [] getFileSpecs(int type) { 105 final IContentType target = getTarget(); 106 return (target != null) ? target.getFileSpecs(type) : new String [0]; 107 } 108 109 public String getId() { 110 return id; 111 } 112 113 public String 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 final IContentTypeSettings settings = target.getSettings(context); 124 return settings == target ? this : settings; 126 } 127 128 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 (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 fileName) { 150 final IContentType target = getTarget(); 151 return (target != null) ? target.isAssociatedWith(fileName) : false; 152 } 153 154 public boolean isAssociatedWith(String 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 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 userCharset) throws CoreException { 173 final IContentType target = getTarget(); 174 if (target != null) 175 target.setDefaultCharset(userCharset); 176 } 177 178 public String toString() { 179 return id; 180 } 181 182 } 183 | Popular Tags |