1 11 package org.eclipse.pde.internal.core.site; 12 13 import java.io.PrintWriter ; 14 import java.util.Locale ; 15 import java.util.Vector ; 16 17 import org.eclipse.core.runtime.CoreException; 18 import org.eclipse.pde.core.IModelChangedEvent; 19 import org.eclipse.pde.core.IWritable; 20 import org.eclipse.pde.internal.core.isite.ISite; 21 import org.eclipse.pde.internal.core.isite.ISiteArchive; 22 import org.eclipse.pde.internal.core.isite.ISiteCategoryDefinition; 23 import org.eclipse.pde.internal.core.isite.ISiteDescription; 24 import org.eclipse.pde.internal.core.isite.ISiteFeature; 25 import org.w3c.dom.Node ; 26 import org.w3c.dom.NodeList ; 27 28 public class Site extends SiteObject implements ISite { 29 private static final long serialVersionUID = 1L; 30 final static String INDENT = " "; private Vector features = new Vector (); 32 private Vector archives = new Vector (); 33 private Vector categoryDefs = new Vector (); 34 private String type; 35 private String url; 36 private String mirrorsUrl; 37 private ISiteDescription description; 38 39 42 public void setType(String type) throws CoreException { 43 ensureModelEditable(); 44 Object oldValue = this.type; 45 this.type = type; 46 firePropertyChanged(P_TYPE, oldValue, type); 47 } 48 49 52 public String getType() { 53 return type; 54 } 55 56 59 public void setURL(String url) throws CoreException { 60 ensureModelEditable(); 61 Object oldValue = this.url; 62 this.url = url; 63 firePropertyChanged(P_URL, oldValue, url); 64 } 65 66 69 public String getURL() { 70 return url; 71 } 72 73 76 public void setMirrorsURL(String url) throws CoreException { 77 ensureModelEditable(); 78 Object oldValue = this.mirrorsUrl; 79 this.mirrorsUrl = url; 80 firePropertyChanged(P_MIRRORS_URL, oldValue, url); 81 } 82 83 86 public String getMirrorsURL() { 87 return mirrorsUrl; 88 } 89 90 93 public ISiteDescription getDescription() { 94 return description; 95 } 96 97 100 public void setDescription(ISiteDescription description) 101 throws CoreException { 102 ensureModelEditable(); 103 Object oldValue = this.description; 104 this.description = description; 105 firePropertyChanged(P_DESCRIPTION, oldValue, description); 106 } 107 108 111 public void addFeatures(ISiteFeature[] newFeatures) throws CoreException { 112 ensureModelEditable(); 113 for (int i = 0; i < newFeatures.length; i++) { 114 ISiteFeature feature = newFeatures[i]; 115 ((SiteFeature) feature).setInTheModel(true); 116 features.add(newFeatures[i]); 117 } 118 fireStructureChanged(newFeatures, IModelChangedEvent.INSERT); 119 } 120 121 124 public void addArchives(ISiteArchive[] archs) throws CoreException { 125 ensureModelEditable(); 126 for (int i = 0; i < archs.length; i++) { 127 ISiteArchive archive = archs[i]; 128 ((SiteArchive) archive).setInTheModel(true); 129 archives.add(archs[i]); 130 } 131 fireStructureChanged(archs, IModelChangedEvent.INSERT); 132 } 133 134 137 public void addCategoryDefinitions(ISiteCategoryDefinition[] defs) 138 throws CoreException { 139 ensureModelEditable(); 140 for (int i = 0; i < defs.length; i++) { 141 ISiteCategoryDefinition def = defs[i]; 142 ((SiteCategoryDefinition) def).setInTheModel(true); 143 categoryDefs.add(defs[i]); 144 } 145 fireStructureChanged(defs, IModelChangedEvent.INSERT); 146 } 147 148 151 public void removeFeatures(ISiteFeature[] newFeatures) 152 throws CoreException { 153 ensureModelEditable(); 154 for (int i = 0; i < newFeatures.length; i++) { 155 ISiteFeature feature = newFeatures[i]; 156 ((SiteFeature) feature).setInTheModel(false); 157 features.remove(newFeatures[i]); 158 } 159 fireStructureChanged(newFeatures, IModelChangedEvent.REMOVE); 160 } 161 162 165 public void removeArchives(ISiteArchive[] archs) throws CoreException { 166 ensureModelEditable(); 167 for (int i = 0; i < archs.length; i++) { 168 ISiteArchive archive = archs[i]; 169 ((SiteArchive) archive).setInTheModel(false); 170 archives.remove(archs[i]); 171 } 172 fireStructureChanged(archs, IModelChangedEvent.REMOVE); 173 } 174 175 178 public void removeCategoryDefinitions(ISiteCategoryDefinition[] defs) 179 throws CoreException { 180 ensureModelEditable(); 181 for (int i = 0; i < defs.length; i++) { 182 ISiteCategoryDefinition def = defs[i]; 183 ((SiteCategoryDefinition) def).setInTheModel(false); 184 categoryDefs.remove(defs[i]); 185 } 186 fireStructureChanged(defs, IModelChangedEvent.REMOVE); 187 } 188 189 192 public ISiteFeature[] getFeatures() { 193 return (ISiteFeature[]) features.toArray( 194 new ISiteFeature[features.size()]); 195 } 196 197 200 public ISiteArchive[] getArchives() { 201 return (ISiteArchive[]) archives.toArray( 202 new ISiteArchive[archives.size()]); 203 } 204 205 208 public ISiteCategoryDefinition[] getCategoryDefinitions() { 209 return (ISiteCategoryDefinition[]) categoryDefs.toArray( 210 new ISiteCategoryDefinition[categoryDefs.size()]); 211 } 212 protected void reset() { 213 archives.clear(); 214 categoryDefs.clear(); 215 features.clear(); 216 description = null; 217 type = null; 218 url = null; 219 mirrorsUrl = null; 220 } 221 protected void parse(Node node) { 222 type = getNodeAttribute(node, "type"); url = getNodeAttribute(node, "url"); mirrorsUrl = getNodeAttribute(node, "mirrorsURL"); NodeList children = node.getChildNodes(); 226 for (int i = 0; i < children.getLength(); i++) { 227 Node child = children.item(i); 228 if (child.getNodeType() == Node.ELEMENT_NODE) { 229 parseChild(child); 230 } 231 } 232 } 233 234 protected void parseChild(Node child) { 235 String tag = child.getNodeName().toLowerCase(Locale.ENGLISH); 236 if (tag.equals("feature")) { ISiteFeature feature = getModel().getFactory().createFeature(); 238 ((SiteFeature) feature).parse(child); 239 ((SiteFeature) feature).setInTheModel(true); 240 features.add(feature); 241 } else if (tag.equals("archive")) { ISiteArchive archive = getModel().getFactory().createArchive(); 243 ((SiteArchive) archive).parse(child); 244 ((SiteArchive) archive).setInTheModel(true); 245 archives.add(archive); 246 } else if (tag.equals("category-def")) { ISiteCategoryDefinition def = 248 getModel().getFactory().createCategoryDefinition(); 249 ((SiteCategoryDefinition) def).parse(child); 250 ((SiteCategoryDefinition) def).setInTheModel(true); 251 categoryDefs.add(def); 252 } else if (tag.equals("description")) { if (description != null) 254 return; 255 description = getModel().getFactory().createDescription(this); 256 ((SiteDescription) description).parse(child); 257 ((SiteDescription) description).setInTheModel(true); 258 } 259 } 260 public void restoreProperty(String name, Object oldValue, Object newValue) 261 throws CoreException { 262 if (name.equals(P_TYPE)) { 263 setType(newValue != null ? newValue.toString() : null); 264 } else if (name.equals(P_URL)) { 265 setURL(newValue != null ? newValue.toString() : null); 266 } else if (name.equals(P_MIRRORS_URL)) { 267 setMirrorsURL(newValue != null ? newValue.toString() : null); 268 } else if ( 269 name.equals(P_DESCRIPTION) 270 && newValue instanceof ISiteDescription) { 271 setDescription((ISiteDescription) newValue); 272 } else 273 super.restoreProperty(name, oldValue, newValue); 274 } 275 public void write(String indent, PrintWriter writer) { 276 writer.print(indent + "<site"); String indent2 = indent + INDENT; 278 String indenta = indent + INDENT + INDENT; 279 writeIfDefined(indenta, writer, "type", getType()); writeIfDefined(indenta, writer, "url", getURL()); writeIfDefined(indenta, writer, "mirrorsURL", getMirrorsURL()); writer.println(">"); 284 if (description != null) { 285 description.write(indent2, writer); 286 } 287 writeChildren(indent2, features, writer); 288 writeChildren(indent2, archives, writer); 289 writeChildren(indent2, categoryDefs, writer); 290 writer.println(indent + "</site>"); } 292 293 public boolean isValid() { 294 for (int i=0; i<features.size(); i++) { 295 ISiteFeature feature = (ISiteFeature)features.get(i); 296 if (!feature.isValid()) return false; 297 } 298 for (int i=0; i<archives.size(); i++) { 299 ISiteArchive archive = (ISiteArchive)archives.get(i); 300 if (!archive.isValid()) return false; 301 } 302 for (int i=0; i<categoryDefs.size(); i++) { 303 ISiteCategoryDefinition def = (ISiteCategoryDefinition)categoryDefs.get(i); 304 if (!def.isValid()) return false; 305 } 306 return true; 307 } 308 309 310 311 private void writeChildren( 312 String indent, 313 Vector children, 314 PrintWriter writer) { 315 for (int i = 0; i < children.size(); i++) { 316 IWritable writable = (IWritable) children.get(i); 317 writable.write(indent, writer); 318 } 319 } 320 private void writeIfDefined( 321 String indent, 322 PrintWriter writer, 323 String attName, 324 String attValue) { 325 if (attValue == null || attValue.length() <= 0) 326 return; 327 writer.println(); 328 writer.print(indent + attName + "=\"" + SiteObject.getWritableString(attValue) + "\""); } 330 331 } 332 | Popular Tags |