| 1 | /******************************************************************************* | 
| 2 |  * Copyright (c) 2009 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.pde.api.tools.internal.search; | 
| 12 |   | 
| 13 | import java.io.BufferedWriter; | 
| 14 | import java.io.File; | 
| 15 | import java.io.FileWriter; | 
| 16 | import java.io.IOException; | 
| 17 |   | 
| 18 | import org.eclipse.core.runtime.CoreException; | 
| 19 | import org.eclipse.pde.api.tools.internal.provisional.search.IApiSearchRequestor; | 
| 20 | import org.eclipse.pde.api.tools.internal.provisional.search.IMetadata; | 
| 21 | import org.eclipse.pde.api.tools.internal.util.Util; | 
| 22 | import org.w3c.dom.Document; | 
| 23 | import org.w3c.dom.Element; | 
| 24 |   | 
| 25 | /** | 
| 26 |  * Implementation of {@link IMetadata} for API use scans | 
| 27 |  *  | 
| 28 |  * @since 1.0.1 | 
| 29 |  */ | 
| 30 | public class UseMetadata implements IMetadata { | 
| 31 |   | 
| 32 |         /** | 
| 33 |          * XML tag name for the date the scan was run | 
| 34 |          */ | 
| 35 |         public static final String RUNATDATE = "runatdate"; //$NON-NLS-1$ | 
| 36 |         /** | 
| 37 |          * XML tag name for the search flags | 
| 38 |          */ | 
| 39 |         public static final String FLAGS = "flags"; //$NON-NLS-1$ | 
| 40 |         /** | 
| 41 |          * XML tag name for the baseline location | 
| 42 |          */ | 
| 43 |         public static final String BASELINELOCATION = "baselinelocation"; //$NON-NLS-1$ | 
| 44 |         /** | 
| 45 |          * XML tag name for the report location | 
| 46 |          */ | 
| 47 |         public static final String REPORTLOCATION = "reportlocation"; //$NON-NLS-1$ | 
| 48 |         /** | 
| 49 |          * XML tag name for a scope pattern | 
| 50 |          */ | 
| 51 |         public static final String SCOPEPATTERN = "scopepattern"; //$NON-NLS-1$ | 
| 52 |         /** | 
| 53 |          * XML tag name reference pattern | 
| 54 |          */ | 
| 55 |         public static final String REFERENCEPATTERN = "referencepattern"; //$NON-NLS-1$ | 
| 56 |         /** | 
| 57 |          * XML tag name for API patterns | 
| 58 |          */ | 
| 59 |         public static final String APIPATTERNS = "apipatterns"; //$NON-NLS-1$ | 
| 60 |         /** | 
| 61 |          * XML tag name for internal patterns | 
| 62 |          */ | 
| 63 |         public static final String INTERNALPATTERNS = "internalpatterns"; //$NON-NLS-1$ | 
| 64 |         /** | 
| 65 |          * XML tag name for archive patterns | 
| 66 |          */ | 
| 67 |         public static final String ARCHIVEPATTERNS = "archivepatterns"; //$NON-NLS-1$ | 
| 68 |         /** | 
| 69 |          * XML tag name for a pattern | 
| 70 |          */ | 
| 71 |         public static final String PATTERN = "pattern"; //$NON-NLS-1$ | 
| 72 |         /** | 
| 73 |          * XML tag name for the value | 
| 74 |          */ | 
| 75 |         public static final String VALUE = "value"; //$NON-NLS-1$ | 
| 76 |         /** | 
| 77 |          * Root tag for the metadata file | 
| 78 |          */ | 
| 79 |         public static final String METADATA = "metadata"; //$NON-NLS-1$ | 
| 80 |         /** | 
| 81 |          * XML tag name for the description field | 
| 82 |          */ | 
| 83 |         public static final String DESCRIPTION = "description"; //$NON-NLS-1$ | 
| 84 |          | 
| 85 |         int searchflags = 0; | 
| 86 |         String[] apipatterns = null, intpatterns = null, archivepatterns = null; | 
| 87 |         String baselinelocation = null,  | 
| 88 |                         reportlocation = null,  | 
| 89 |                         scopepattern = null,  | 
| 90 |                         refpattern = null,  | 
| 91 |                         runatdate = null, | 
| 92 |                         description = null; | 
| 93 |          | 
| 94 |         /** | 
| 95 |          * Constructor | 
| 96 |          */ | 
| 97 |         public UseMetadata() { | 
| 98 |                 //create an empty metadata object | 
| 99 |         } | 
| 100 |          | 
| 101 |         /** | 
| 102 |          * Constructor | 
| 103 |          * @param searchflags | 
| 104 |          * @param scopepattern | 
| 105 |          * @param refpattern | 
| 106 |          * @param baselinelocation | 
| 107 |          * @param reportlocation | 
| 108 |          * @param apipatterns | 
| 109 |          * @param internalpatterns | 
| 110 |          * @param archivepatterns | 
| 111 |          * @param runatdate | 
| 112 |          * @param description | 
| 113 |          */ | 
| 114 |         public UseMetadata(int searchflags, String scopepattern, String refpattern,  | 
| 115 |                         String baselinelocation, String reportlocation, String[] apipatterns, String[] internalpatterns,  | 
| 116 |                         String[] archivepatterns, String runatdate, String description) { | 
| 117 |                 this.searchflags = searchflags; | 
| 118 |                 this.scopepattern = scopepattern; | 
| 119 |                 this.refpattern = refpattern; | 
| 120 |                 this.baselinelocation = baselinelocation; | 
| 121 |                 this.reportlocation = reportlocation; | 
| 122 |                 this.apipatterns = apipatterns; | 
| 123 |                 this.intpatterns = internalpatterns; | 
| 124 |                 this.archivepatterns = archivepatterns; | 
| 125 |                 this.runatdate = runatdate; | 
| 126 |                 this.description = description; | 
| 127 |         } | 
| 128 |          | 
| 129 |         /* (non-Javadoc) | 
| 130 |          * @see org.eclipse.pde.api.tools.internal.provisional.search.IMetadata#serializeToFile(java.io.File) | 
| 131 |          */ | 
| 132 |         public void serializeToFile(File file) throws IOException, CoreException { | 
| 133 |                 BufferedWriter writer = null; | 
| 134 |                 try { | 
| 135 |                         Document doc = Util.newDocument(); | 
| 136 |                         Element root = doc.createElement(METADATA); | 
| 137 |                         doc.appendChild(root); | 
| 138 |                         Element child = doc.createElement(FLAGS); | 
| 139 |                         root.appendChild(child); | 
| 140 |                         child.setAttribute(VALUE, Integer.toString(this.searchflags)); | 
| 141 |                         child = doc.createElement(RUNATDATE); | 
| 142 |                         root.appendChild(child); | 
| 143 |                         child.setAttribute(VALUE, this.runatdate); | 
| 144 |                         child = doc.createElement(DESCRIPTION); | 
| 145 |                         root.appendChild(child); | 
| 146 |                         child.setAttribute(VALUE, this.description); | 
| 147 |                         child = doc.createElement(BASELINELOCATION); | 
| 148 |                         root.appendChild(child); | 
| 149 |                         child.setAttribute(VALUE, this.baselinelocation); | 
| 150 |                         child = doc.createElement(REPORTLOCATION); | 
| 151 |                         root.appendChild(child); | 
| 152 |                         child.setAttribute(VALUE, this.reportlocation); | 
| 153 |                         child = doc.createElement(SCOPEPATTERN); | 
| 154 |                         root.appendChild(child); | 
| 155 |                         child.setAttribute(VALUE, this.scopepattern); | 
| 156 |                         child = doc.createElement(REFERENCEPATTERN); | 
| 157 |                         root.appendChild(child); | 
| 158 |                         child.setAttribute(VALUE, this.refpattern); | 
| 159 |                         child = doc.createElement(APIPATTERNS); | 
| 160 |                         root.appendChild(child); | 
| 161 |                         Element sub = null; | 
| 162 |                         if(this.apipatterns != null) { | 
| 163 |                                 for (int i = 0; i < this.apipatterns.length; i++) { | 
| 164 |                                         sub = doc.createElement(PATTERN); | 
| 165 |                                         child.appendChild(sub); | 
| 166 |                                         sub.setAttribute(VALUE, apipatterns[i]); | 
| 167 |                                 } | 
| 168 |                         } | 
| 169 |                         child = doc.createElement(INTERNALPATTERNS); | 
| 170 |                         root.appendChild(child); | 
| 171 |                         if(this.intpatterns != null) { | 
| 172 |                                 for (int i = 0; i < this.intpatterns.length; i++) { | 
| 173 |                                         sub = doc.createElement(PATTERN); | 
| 174 |                                         child.appendChild(sub); | 
| 175 |                                         sub.setAttribute(VALUE, intpatterns[i]); | 
| 176 |                                 } | 
| 177 |                         } | 
| 178 |                         child = doc.createElement(ARCHIVEPATTERNS); | 
| 179 |                         root.appendChild(child); | 
| 180 |                         if(this.archivepatterns != null) { | 
| 181 |                                 for (int i = 0; i < this.archivepatterns.length; i++) { | 
| 182 |                                         sub = doc.createElement(PATTERN); | 
| 183 |                                         child.appendChild(sub); | 
| 184 |                                         sub.setAttribute(VALUE, archivepatterns[i]); | 
| 185 |                                 } | 
| 186 |                         } | 
| 187 |                         writer = new BufferedWriter(new FileWriter(file)); | 
| 188 |                         writer.write(Util.serializeDocument(doc)); | 
| 189 |                         writer.flush(); | 
| 190 |                 } | 
| 191 |                 finally { | 
| 192 |                         if(writer != null) { | 
| 193 |                                 writer.close(); | 
| 194 |                         } | 
| 195 |                 } | 
| 196 |         } | 
| 197 |   | 
| 198 |         /** | 
| 199 |          * @return true if the search8 flags include searching for API references, | 
| 200 |          * false otherwise | 
| 201 |          */ | 
| 202 |         public boolean includesAPI() { | 
| 203 |                 return (this.searchflags & IApiSearchRequestor.INCLUDE_API) != 0; | 
| 204 |         } | 
| 205 |          | 
| 206 |         /** | 
| 207 |          * @return true if the search flags include searching for internal references,  | 
| 208 |          * false otherwise | 
| 209 |          */ | 
| 210 |         public boolean includesInternal() { | 
| 211 |                 return (this.searchflags & IApiSearchRequestor.INCLUDE_INTERNAL) != 0; | 
| 212 |         } | 
| 213 |          | 
| 214 |         /** | 
| 215 |          * Allows the run-at date to be set. This method accepts <code>null</code> | 
| 216 |          * @param date the date to set | 
| 217 |          */ | 
| 218 |         public void setRunAtDate(String date) { | 
| 219 |                 this.runatdate = date; | 
| 220 |         } | 
| 221 |          | 
| 222 |         /** | 
| 223 |          * Returns the run-at date set in this metadata or <code>null</code> if none. | 
| 224 |          * @return the run-at date or <code>null</code> | 
| 225 |          */ | 
| 226 |         public String getRunAtDate() { | 
| 227 |                 return this.runatdate; | 
| 228 |         } | 
| 229 |          | 
| 230 |         /** | 
| 231 |          * Returns the human-readable description of the scan | 
| 232 |          * @return the description | 
| 233 |          */ | 
| 234 |         public String getDescription() { | 
| 235 |                 return this.description; | 
| 236 |         } | 
| 237 |          | 
| 238 |         /** | 
| 239 |          * Allows the human-readable description to be set. This method accepts <code>null</code> | 
| 240 |          * @param description the description to set | 
| 241 |          */ | 
| 242 |         public void setDescription(String description) { | 
| 243 |                 this.description = description; | 
| 244 |         } | 
| 245 |          | 
| 246 |         /** | 
| 247 |          * Allows the combined search flags to be set. | 
| 248 |          * @param flags the search flags to set | 
| 249 |          */ | 
| 250 |         public void setSearchflags(int flags) { | 
| 251 |                 this.searchflags = flags; | 
| 252 |         } | 
| 253 |   | 
| 254 |         /** | 
| 255 |          * Returns the collection of API patterns set in this metadata or <code>null</code> if none. | 
| 256 |          * @return the API patterns or <code>null</code> | 
| 257 |          */ | 
| 258 |         public String[] getApiPatterns() { | 
| 259 |                 return apipatterns; | 
| 260 |         } | 
| 261 |          | 
| 262 |         /** | 
| 263 |          * Allows the API patterns to be set. This method accepts <code>null</code> | 
| 264 |          * @param patterns the patterns to set | 
| 265 |          */ | 
| 266 |         public void setApiPatterns(String[] patterns) { | 
| 267 |                 this.apipatterns = patterns; | 
| 268 |         } | 
| 269 |   | 
| 270 |         /** | 
| 271 |          * Returns the collection of internal patterns set in this metadata or <code>null</code> if none. | 
| 272 |          * @return the internal patterns or <code>null</code> | 
| 273 |          */ | 
| 274 |         public String[] getInternalPatterns() { | 
| 275 |                 return this.intpatterns; | 
| 276 |         } | 
| 277 |   | 
| 278 |         /** | 
| 279 |          * Allows the internal patterns to be set. This method accepts <code>null</code>. | 
| 280 |          * @param patterns the internal patterns to set | 
| 281 |          */ | 
| 282 |         public void setInternalPatterns(String[] patterns) { | 
| 283 |                 this.intpatterns = patterns; | 
| 284 |         } | 
| 285 |   | 
| 286 |         /** | 
| 287 |          * Returns the collection of archive patterns set in this metadata or <code>null</code> if none. | 
| 288 |          * @return the archive patterns or <code>null</code> | 
| 289 |          */ | 
| 290 |         public String[] getArchivePatterns() { | 
| 291 |                 return this.archivepatterns; | 
| 292 |         } | 
| 293 |   | 
| 294 |         /** | 
| 295 |          * Allows the set of archive patterns to be set. This method accepts <code>null</code> | 
| 296 |          * @param patterns the archive patterns to set | 
| 297 |          */ | 
| 298 |         public void setArchivePatterns(String[] patterns) { | 
| 299 |                 this.archivepatterns = patterns; | 
| 300 |         } | 
| 301 |   | 
| 302 |         /** | 
| 303 |          * Returns the baseline location set in this metadata or <code>null</code> if none. | 
| 304 |          * @return the baseline location or <code>null</code> | 
| 305 |          */ | 
| 306 |         public String getBaselineLocation() { | 
| 307 |                 return this.baselinelocation; | 
| 308 |         } | 
| 309 |   | 
| 310 |         /** | 
| 311 |          * Allows the baseline location to be set. This method accepts <code>null</code>. | 
| 312 |          * @param location the new location | 
| 313 |          */ | 
| 314 |         public void setBaselineLocation(String location) { | 
| 315 |                 this.baselinelocation = location; | 
| 316 |         } | 
| 317 |   | 
| 318 |         /** | 
| 319 |          * Returns the report location set in this metadata or <code>null</code> if none. | 
| 320 |          * @return the report location or <code>null</code> | 
| 321 |          */ | 
| 322 |         public String getReportLocation() { | 
| 323 |                 return this.reportlocation; | 
| 324 |         } | 
| 325 |   | 
| 326 |         /** | 
| 327 |          * Allows the report location to be set. This method accepts <code>null</code>. | 
| 328 |          * @param location the new report location | 
| 329 |          */ | 
| 330 |         public void setReportLocation(String location) { | 
| 331 |                 this.reportlocation = location; | 
| 332 |         } | 
| 333 |   | 
| 334 |         /** | 
| 335 |          * Allows the reference pattern to be set. This method accepts <code>null</code> | 
| 336 |          * @param pattern the new pattern | 
| 337 |          */ | 
| 338 |         public void setReferencePattern(String pattern) { | 
| 339 |                 this.refpattern = pattern; | 
| 340 |         } | 
| 341 |          | 
| 342 |         /** | 
| 343 |          * Returns the reference pattern set in this metadata or <code>null</code> if none. | 
| 344 |          * @return the reference pattern or <code>null</code> | 
| 345 |          */ | 
| 346 |         public String getReferencePattern() { | 
| 347 |                 return this.refpattern; | 
| 348 |         } | 
| 349 |          | 
| 350 |         /** | 
| 351 |          * Allows the scope pattern to be set. This method accepts <code>null</code> | 
| 352 |          * @param pattern the new pattern | 
| 353 |          */ | 
| 354 |         public void setScopePattern(String pattern) { | 
| 355 |                 this.scopepattern = pattern; | 
| 356 |         } | 
| 357 |          | 
| 358 |         /** | 
| 359 |          * Returns the scope pattern set in this metadata or <code>null</code> if none. | 
| 360 |          * @return the scope pattern or <code>null</code> | 
| 361 |          */ | 
| 362 |         public String getScopePattern() { | 
| 363 |                 return this.scopepattern; | 
| 364 |         } | 
| 365 | } |