1 11 12 package org.eclipse.ui.internal.intro.impl.model; 13 14 import java.util.Hashtable ; 15 import java.util.Vector ; 16 17 import org.eclipse.core.runtime.FileLocator; 18 import org.eclipse.core.runtime.IConfigurationElement; 19 import org.eclipse.core.runtime.IPath; 20 import org.eclipse.core.runtime.Path; 21 import org.eclipse.ui.internal.intro.impl.model.loader.IntroContentParser; 22 import org.eclipse.ui.internal.intro.impl.model.util.BundleUtil; 23 import org.eclipse.ui.internal.intro.impl.model.util.ModelUtil; 24 import org.osgi.framework.Bundle; 25 import org.w3c.dom.Document ; 26 import org.w3c.dom.Element ; 27 import org.w3c.dom.Node ; 28 import org.w3c.dom.NodeList ; 29 30 38 public class IntroExtensionContent extends AbstractIntroElement { 39 40 protected static final String TAG_CONTAINER_EXTENSION = "extensionContent"; protected static final String TAG_CONTAINER_REPLACE = "replacementContent"; 43 public static final int TYPE_CONTRIBUTION = 0; 44 public static final int TYPE_REPLACEMENT = 1; 45 46 protected static final String ATT_PATH = "path"; protected static final String ATT_ID = "id"; private static final String ATT_STYLE = "style"; private static final String ATT_ALT_STYLE = "alt-style"; private static final String ATT_CONTENT = "content"; 52 private static final Element[] EMPTY_ELEMENT_ARRAY = new Element[0]; 53 54 private String path; 55 private String content; 56 private String contentFile; 57 private String contentId; 58 private String anchorId; 59 60 private Element element; 61 private String base; 62 63 private Vector styles = new Vector (); 64 private Hashtable altStyles = new Hashtable (); 65 66 IntroExtensionContent(Element element, Bundle bundle, String base, IConfigurationElement configExtElement) { 67 super(element, bundle); 68 path = getAttribute(element, ATT_PATH); 69 content = getAttribute(element, ATT_CONTENT); 70 anchorId = getAttribute(element, ATT_ID); 71 this.element = element; 72 this.base = base; 73 74 init(element, bundle, base); 76 77 if (content != null) { 79 IPath subBase = ModelUtil.getParentFolderPath(content); 83 String newBase = new Path(base).append(subBase).toString(); 84 extractFileAndId(bundle); 85 contentFile = BundleUtil.getResolvedResourceLocation(base, contentFile, 86 bundle); 87 this.base = newBase; 88 } 89 90 String contributor = configExtElement.getContributor().getName(); 92 ExtensionMap.getInstance().putPluginId(anchorId, contributor); 93 } 94 95 public String getId() { 96 return anchorId; 97 } 98 99 100 108 private void init(Element element, Bundle bundle, String base) { 109 String [] styleValues = getAttributeList(element, ATT_STYLE); 110 if (styleValues != null && styleValues.length > 0) { 111 for (int i = 0; i < styleValues.length; i++) { 112 String style = styleValues[i]; 113 style = BundleUtil.getResolvedResourceLocation(base, style, 114 bundle); 115 addStyle(style); 116 } 117 } 118 119 String [] altStyleValues = getAttributeList(element, ATT_ALT_STYLE); 120 if (altStyleValues != null && altStyleValues.length > 0) { 121 for (int i = 0; i < altStyleValues.length; i++) { 122 String style = altStyleValues[i]; 123 style = BundleUtil.getResolvedResourceLocation(base, style, 124 bundle); 125 addAltStyle(style, bundle); 126 } 127 } 128 } 129 130 136 protected void addStyle(String style) { 137 if (styles.contains(style)) 138 return; 139 styles.add(style); 140 } 141 142 143 149 protected void addAltStyle(String altStyle, Bundle bundle) { 150 if (altStyles.containsKey(altStyle)) 151 return; 152 altStyles.put(altStyle, bundle); 153 } 154 155 159 public int getExtensionType() { 160 return TAG_CONTAINER_REPLACE.equals(element.getNodeName()) ? TYPE_REPLACEMENT : TYPE_CONTRIBUTION; 161 } 162 163 166 public String getPath() { 167 return path; 168 } 169 170 175 public int getType() { 176 return AbstractIntroElement.CONTAINER_EXTENSION; 177 } 178 179 protected Element[] getChildren() { 180 NodeList nodeList = element.getChildNodes(); 181 Vector vector = new Vector (); 182 for (int i = 0; i < nodeList.getLength(); i++) { 183 Node node = nodeList.item(i); 184 if (node.getNodeType() == Node.ELEMENT_NODE) 185 vector.add(node); 186 } 187 Element[] filteredElements = new Element[vector.size()]; 188 vector.copyInto(filteredElements); 189 this.element = null; 191 return filteredElements; 192 } 193 194 public boolean isXHTMLContent() { 195 return content != null ? true : false; 196 } 197 198 206 public Element[] getElements() { 207 if (isXHTMLContent()) { 209 IntroContentParser parser = new IntroContentParser(contentFile); 210 Document dom = parser.getDocument(); 211 if (dom != null) { 212 if (parser.hasXHTMLContent()) { 215 if (contentId != null) { 216 return new Element[] { ModelUtil.getElementById(dom, contentId) }; 218 } 219 else { 220 Element extensionBody = ModelUtil.getBodyElement(dom); 222 return ModelUtil.getElementsByTagName(extensionBody, "*"); } 224 } 225 } 226 } 227 return EMPTY_ELEMENT_ARRAY; 228 } 229 230 233 protected Hashtable getAltStyles() { 234 return altStyles; 235 } 236 237 240 protected String [] getStyles() { 241 String [] stylesArray = new String [styles.size()]; 242 styles.copyInto(stylesArray); 243 return stylesArray; 244 } 245 246 249 public String getContent() { 250 return content; 251 } 252 253 public String getBase() { 254 return base; 255 } 256 257 265 private void extractFileAndId(Bundle bundle) { 266 IPath resourcePath = new Path(base + content); 268 if (FileLocator.find(bundle, resourcePath, null) != null) { 269 contentFile = content; 271 } 272 else { 273 int lastSlashIndex = content.lastIndexOf('/'); 275 if (lastSlashIndex != -1) { 276 contentFile = content.substring(0, lastSlashIndex); 277 contentId = content.substring(lastSlashIndex + 1); 278 } 279 else { 280 contentFile = content; 282 } 283 } 284 } 285 } 286 | Popular Tags |