| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
package org.apache.creadur.whisker.fromxml; |
| 20 | |
|
| 21 | |
import java.io.IOException; |
| 22 | |
import java.io.InputStream; |
| 23 | |
import java.util.ArrayList; |
| 24 | |
import java.util.Calendar; |
| 25 | |
import java.util.Collection; |
| 26 | |
import java.util.Collections; |
| 27 | |
import java.util.HashMap; |
| 28 | |
import java.util.HashSet; |
| 29 | |
import java.util.List; |
| 30 | |
import java.util.Map; |
| 31 | |
import java.util.SortedSet; |
| 32 | |
import java.util.TreeSet; |
| 33 | |
|
| 34 | |
import org.apache.commons.lang3.StringUtils; |
| 35 | |
import org.apache.creadur.whisker.model.ByOrganisation; |
| 36 | |
import org.apache.creadur.whisker.model.License; |
| 37 | |
import org.apache.creadur.whisker.model.Organisation; |
| 38 | |
import org.apache.creadur.whisker.model.Resource; |
| 39 | |
import org.apache.creadur.whisker.model.WithLicense; |
| 40 | |
import org.apache.creadur.whisker.model.WithinDirectory; |
| 41 | |
import org.apache.creadur.whisker.model.Descriptor; |
| 42 | |
import org.jdom.Document; |
| 43 | |
import org.jdom.Element; |
| 44 | |
import org.jdom.JDOMException; |
| 45 | |
import org.jdom.input.SAXBuilder; |
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | 338044 | public class JDomBuilder { |
| 51 | |
|
| 52 | |
private static final String COPYRIGHT_NOTICE_NAME = "copyright-notice"; |
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
private static final String LICENSE_ELEMENT_NAME = "license"; |
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
private static final String PRIMARY_LICENSE_NAME = "primary-license"; |
| 61 | |
|
| 62 | |
private static final String ORGANISATION_ELEMENT_NAME = "organisation"; |
| 63 | |
|
| 64 | |
private static final String RESOURCE_ELEMENT_NAME = "resource"; |
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
public Resource resource(Element element) throws UnexpectedElementException { |
| 73 | 464 | if (RESOURCE_ELEMENT_NAME.equals(element.getName())) { |
| 74 | 462 | return new Resource(StringUtils.trim(element.getAttributeValue("name")), |
| 75 | |
StringUtils.trim(element.getAttributeValue("notice")), |
| 76 | |
StringUtils.trim(element.getAttributeValue("source"))); |
| 77 | |
} else { |
| 78 | 2 | throw unexpectedElementException(element, RESOURCE_ELEMENT_NAME); |
| 79 | |
} |
| 80 | |
} |
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
private UnexpectedElementException unexpectedElementException(Element element, |
| 89 | |
final String expectedElement) { |
| 90 | 4 | return new UnexpectedElementException(expectedElement, element.getName()); |
| 91 | |
} |
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
public Organisation organisation(Element element) throws UnexpectedElementException { |
| 100 | 65284 | if (ORGANISATION_ELEMENT_NAME.equals(element.getName())) { |
| 101 | 65282 | return new Organisation( |
| 102 | |
element.getAttributeValue("id"), |
| 103 | |
element.getAttributeValue("name"), |
| 104 | |
element.getAttributeValue("url")); |
| 105 | |
} else { |
| 106 | 2 | throw unexpectedElementException(element, ORGANISATION_ELEMENT_NAME); |
| 107 | |
} |
| 108 | |
} |
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
@SuppressWarnings("unchecked") |
| 115 | |
public Collection<Resource> collectResources(Element element) { |
| 116 | 75438 | final Collection<Resource> resources = new TreeSet<Resource>(); |
| 117 | 75438 | for (Element resourceElement: (List<Element>) element.getChildren("resource")) { |
| 118 | 448 | resources.add(new JDomBuilder().resource(resourceElement)); |
| 119 | |
} |
| 120 | 75438 | return Collections.unmodifiableCollection(resources); |
| 121 | |
} |
| 122 | |
|
| 123 | |
|
| 124 | |
|
| 125 | |
|
| 126 | |
|
| 127 | |
|
| 128 | |
|
| 129 | |
public Organisation organisation(final Element element, |
| 130 | |
final Map<String, Organisation> organisationsById) throws MissingIDException { |
| 131 | 75428 | final String id = element.getAttributeValue("id"); |
| 132 | 75428 | if (organisationsById.containsKey(id)) { |
| 133 | 75424 | return organisationsById.get(id); |
| 134 | |
} else { |
| 135 | 4 | throw new MissingIDException(ORGANISATION_ELEMENT_NAME, element.getName(), id); |
| 136 | |
} |
| 137 | |
} |
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | |
|
| 145 | |
public ByOrganisation byOrganisation(final Element element, final Organisation organisation) { |
| 146 | 75428 | return new ByOrganisation(organisation, collectResources(element)); |
| 147 | |
} |
| 148 | |
|
| 149 | |
|
| 150 | |
|
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
|
| 156 | |
public ByOrganisation byOrganisation(final Element byOrganisation, |
| 157 | |
final Map<String, Organisation> organisationsById) throws MissingIDException { |
| 158 | 75422 | return byOrganisation(byOrganisation, organisation(byOrganisation, organisationsById)); |
| 159 | |
} |
| 160 | |
|
| 161 | |
|
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | |
|
| 166 | |
|
| 167 | |
@SuppressWarnings("unchecked") |
| 168 | |
public SortedSet<ByOrganisation> collectByOrganisations(final Element parent, |
| 169 | |
final Map<String, Organisation> map) { |
| 170 | 141864 | final SortedSet<ByOrganisation> results = new TreeSet<ByOrganisation>(); |
| 171 | 141864 | if (parent != null) { |
| 172 | 76376 | for (final Element byOrgElement: (List<Element>) parent.getChildren("by-organisation")) { |
| 173 | 75416 | results.add(byOrganisation(byOrgElement, map)); |
| 174 | |
} |
| 175 | |
} |
| 176 | 141864 | return Collections.unmodifiableSortedSet(results); |
| 177 | |
} |
| 178 | |
|
| 179 | |
|
| 180 | |
|
| 181 | |
|
| 182 | |
|
| 183 | |
|
| 184 | |
public License license(Element element) { |
| 185 | 65308 | final Element text = element.getChild("text"); |
| 186 | 65308 | return new License("yes".equalsIgnoreCase(element.getAttributeValue("requires-source")), |
| 187 | |
text == null ? "" : text.getText(), |
| 188 | |
expectedParameters(element), |
| 189 | |
element.getAttributeValue("id"), |
| 190 | |
element.getAttributeValue("url"), |
| 191 | |
element.getAttributeValue("name")); |
| 192 | |
} |
| 193 | |
|
| 194 | |
@SuppressWarnings("unchecked") |
| 195 | |
private Collection<String> expectedParameters(final Element element) { |
| 196 | 65308 | final Collection<String> results = new HashSet<String>(); |
| 197 | 65308 | final Element templateElement = element.getChild("template"); |
| 198 | 65308 | if (templateElement != null) { |
| 199 | 6 | for (Element parameterNameElement: (List<Element>) templateElement.getChildren("parameter-name")) { |
| 200 | 6 | results.add(parameterNameElement.getTextTrim()); |
| 201 | |
} |
| 202 | |
} |
| 203 | 65308 | return results; |
| 204 | |
} |
| 205 | |
|
| 206 | |
|
| 207 | |
|
| 208 | |
|
| 209 | |
|
| 210 | |
|
| 211 | |
|
| 212 | |
|
| 213 | |
public License license(final Element element, final Map<String, License> licenses) throws MissingIDException { |
| 214 | 75654 | final String id = element.getAttributeValue("id"); |
| 215 | 75654 | if (licenses.containsKey(id)) { |
| 216 | 75650 | return licenses.get(id); |
| 217 | |
} else { |
| 218 | 4 | throw new MissingIDException(LICENSE_ELEMENT_NAME, element.getName(), id); |
| 219 | |
} |
| 220 | |
} |
| 221 | |
|
| 222 | |
|
| 223 | |
|
| 224 | |
|
| 225 | |
|
| 226 | |
|
| 227 | |
|
| 228 | |
|
| 229 | |
|
| 230 | |
public WithLicense withLicense(Element element, |
| 231 | |
Map<String, License> licenses, |
| 232 | |
Map<String, Organisation> organisations) throws MissingIDException { |
| 233 | 75648 | return new WithLicense(license(element, licenses), copyrightNotice(element), |
| 234 | |
parameters(element), collectByOrganisations(element, organisations)); |
| 235 | |
} |
| 236 | |
|
| 237 | |
|
| 238 | |
|
| 239 | |
|
| 240 | |
|
| 241 | |
|
| 242 | |
private String copyrightNotice(final Element element) { |
| 243 | |
final String result; |
| 244 | 75648 | final Element copyrightNoticeElement = element.getChild(COPYRIGHT_NOTICE_NAME); |
| 245 | 75648 | if (copyrightNoticeElement == null) { |
| 246 | 75644 | result = null; |
| 247 | |
} else { |
| 248 | 4 | result = copyrightNoticeElement.getTextTrim(); |
| 249 | |
} |
| 250 | 75648 | return result; |
| 251 | |
} |
| 252 | |
|
| 253 | |
|
| 254 | |
|
| 255 | |
|
| 256 | |
|
| 257 | |
|
| 258 | |
|
| 259 | |
@SuppressWarnings("unchecked") |
| 260 | |
public Map<String, String> parameters(Element element) throws DuplicateElementException { |
| 261 | 75666 | final Map<String, String> results = new HashMap<String, String>(); |
| 262 | 75666 | final Element licenseParametersElement = element.getChild("license-parameters"); |
| 263 | 75666 | if (licenseParametersElement != null) { |
| 264 | 274 | for (Element parameterElement: (List<Element>) licenseParametersElement.getChildren("parameter")) { |
| 265 | 16518 | final String name = parameterElement.getChild("name").getTextTrim(); |
| 266 | 16518 | if (results.containsKey(name)) { |
| 267 | 2 | throw new DuplicateElementException("Duplicate parameter '" + name + "'"); |
| 268 | |
} |
| 269 | 16516 | results.put(name, |
| 270 | |
parameterElement.getChild("value").getTextTrim()); |
| 271 | 16516 | } |
| 272 | |
} |
| 273 | 75664 | return results; |
| 274 | |
} |
| 275 | |
|
| 276 | |
|
| 277 | |
|
| 278 | |
|
| 279 | |
|
| 280 | |
|
| 281 | |
|
| 282 | |
|
| 283 | |
@SuppressWarnings("unchecked") |
| 284 | |
public Collection<WithLicense> withLicenses(Map<String, License> licenses, |
| 285 | |
Map<String, Organisation> organisations, Element parent) { |
| 286 | 66202 | final List<WithLicense> results = new ArrayList<WithLicense>(); |
| 287 | 66202 | for (Element withLicenseElement: (List<Element>) parent.getChildren("with-license")) { |
| 288 | 75380 | results.add(new JDomBuilder().withLicense(withLicenseElement, licenses, organisations)); |
| 289 | |
} |
| 290 | 66202 | Collections.sort(results); |
| 291 | 66202 | return results; |
| 292 | |
} |
| 293 | |
|
| 294 | |
|
| 295 | |
|
| 296 | |
|
| 297 | |
|
| 298 | |
|
| 299 | |
|
| 300 | |
public Collection<ByOrganisation> publicDomain( |
| 301 | |
final Map<String, Organisation> organisations, final Element parent) { |
| 302 | 66202 | return new JDomBuilder().collectByOrganisations(parent.getChild("public-domain"), organisations); |
| 303 | |
} |
| 304 | |
|
| 305 | |
|
| 306 | |
|
| 307 | |
|
| 308 | |
|
| 309 | |
|
| 310 | |
|
| 311 | |
|
| 312 | |
public WithinDirectory withinDirectory(Element element, |
| 313 | |
Map<String, License> licenses, |
| 314 | |
Map<String, Organisation> organisations) { |
| 315 | 65690 | return new WithinDirectory(element.getAttributeValue("dir"), |
| 316 | |
withLicenses(licenses, organisations, element), publicDomain(organisations, element)); |
| 317 | |
} |
| 318 | |
|
| 319 | |
|
| 320 | |
|
| 321 | |
|
| 322 | |
|
| 323 | |
|
| 324 | |
public Map<String, Organisation> mapOrganisations(Document document) { |
| 325 | 516 | final Map<String, Organisation> organisationsById = new HashMap<String, Organisation>(); |
| 326 | |
|
| 327 | 516 | final Element childOrganisations = document.getRootElement().getChild("organisations"); |
| 328 | 516 | if (childOrganisations != null) { |
| 329 | |
@SuppressWarnings("unchecked") |
| 330 | 510 | final List<Element> organisations = (List<Element>) childOrganisations.getChildren("organisation"); |
| 331 | 510 | for (final Element element: organisations) { |
| 332 | 65280 | new JDomBuilder().organisation(element).storeIn(organisationsById); |
| 333 | |
} |
| 334 | |
} |
| 335 | 516 | return Collections.unmodifiableMap(organisationsById); |
| 336 | |
} |
| 337 | |
|
| 338 | |
|
| 339 | |
|
| 340 | |
|
| 341 | |
|
| 342 | |
|
| 343 | |
public Map<String, License> mapLicenses(Document document) { |
| 344 | 518 | final Map<String, License> results = new HashMap<String, License>(); |
| 345 | 518 | final Element licensesChild = document.getRootElement().getChild("licenses"); |
| 346 | 518 | if (licensesChild != null) { |
| 347 | |
@SuppressWarnings("unchecked") |
| 348 | 514 | final List<Element> children = (List<Element>) licensesChild.getChildren(); |
| 349 | 514 | for (final Element element: children) { |
| 350 | 65282 | new JDomBuilder().license(element).storeIn(results); |
| 351 | |
} |
| 352 | |
} |
| 353 | 518 | return Collections.unmodifiableMap(results); |
| 354 | |
|
| 355 | |
} |
| 356 | |
|
| 357 | |
|
| 358 | |
|
| 359 | |
|
| 360 | |
|
| 361 | |
|
| 362 | |
|
| 363 | |
public License primaryLicense(Document document, |
| 364 | |
Map<String, License> licenses) { |
| 365 | 6 | final String idAttributeValue = getPrimaryLicenseElement(document).getAttributeValue("id"); |
| 366 | 6 | final License results = licenses.get(idAttributeValue); |
| 367 | 6 | if (results == null) { |
| 368 | 2 | throw new MissingIDException(LICENSE_ELEMENT_NAME, PRIMARY_LICENSE_NAME, idAttributeValue); |
| 369 | |
} |
| 370 | 4 | return results; |
| 371 | |
} |
| 372 | |
|
| 373 | |
|
| 374 | |
|
| 375 | |
|
| 376 | |
|
| 377 | |
|
| 378 | |
private Element getPrimaryLicenseElement(final Document document) { |
| 379 | 12 | return document.getRootElement().getChild(PRIMARY_LICENSE_NAME); |
| 380 | |
} |
| 381 | |
|
| 382 | |
|
| 383 | |
|
| 384 | |
|
| 385 | |
|
| 386 | |
|
| 387 | |
|
| 388 | |
public String primaryCopyrightNotice(final Document document) { |
| 389 | |
final String result; |
| 390 | 6 | final Element copyrightElement = |
| 391 | |
getPrimaryLicenseElement(document).getChild(COPYRIGHT_NOTICE_NAME); |
| 392 | 6 | if (copyrightElement == null) { |
| 393 | 2 | result = null; |
| 394 | |
} else { |
| 395 | 4 | result = copyrightElement.getTextTrim(); |
| 396 | |
} |
| 397 | 6 | return result; |
| 398 | |
} |
| 399 | |
|
| 400 | |
|
| 401 | |
|
| 402 | |
|
| 403 | |
|
| 404 | |
|
| 405 | |
|
| 406 | |
public Map<String, String> mapNotices(Document document) { |
| 407 | 518 | final Map<String, String> results = new HashMap<String, String>(); |
| 408 | 518 | final Element noticesElement = document.getRootElement().getChild("notices"); |
| 409 | 518 | if (noticesElement != null){ |
| 410 | |
@SuppressWarnings("unchecked") |
| 411 | 512 | final List<Element> children = (List<Element>) noticesElement.getChildren(); |
| 412 | 512 | for (final Element element: children) { |
| 413 | 65280 | results.put(element.getAttributeValue("id"), element.getTextTrim()); |
| 414 | |
} |
| 415 | |
} |
| 416 | 518 | return Collections.unmodifiableMap(results); |
| 417 | |
|
| 418 | |
} |
| 419 | |
|
| 420 | |
|
| 421 | |
|
| 422 | |
|
| 423 | |
|
| 424 | |
|
| 425 | |
|
| 426 | |
public String primaryNotice(Document document) { |
| 427 | |
final String result; |
| 428 | 8 | final Element primaryNoticeElement = document.getRootElement().getChild("primary-notice"); |
| 429 | 8 | if (primaryNoticeElement == null) { |
| 430 | 4 | result = null; |
| 431 | |
} else { |
| 432 | 4 | result = primaryNoticeElement.getText() |
| 433 | |
.replace("${year}", Integer.toString(Calendar.getInstance().get(Calendar.YEAR))); |
| 434 | |
} |
| 435 | 8 | return result; |
| 436 | |
} |
| 437 | |
|
| 438 | |
|
| 439 | |
|
| 440 | |
|
| 441 | |
|
| 442 | |
|
| 443 | |
|
| 444 | |
public String primaryOrganisationId(final Document document) { |
| 445 | |
final String result; |
| 446 | 6 | final Element primaryOrganisationElement = document.getRootElement().getChild("primary-organisation"); |
| 447 | 6 | if (primaryOrganisationElement == null) { |
| 448 | 4 | result = null; |
| 449 | |
} else { |
| 450 | 2 | result = primaryOrganisationElement.getAttributeValue("id"); |
| 451 | |
} |
| 452 | 6 | return result; |
| 453 | |
} |
| 454 | |
|
| 455 | |
private WithinDirectory directory(final Element element, final Map<String, License> licenses, |
| 456 | |
final Map<String, Organisation> organisations) { |
| 457 | 65284 | return new JDomBuilder().withinDirectory(element, licenses, organisations); |
| 458 | |
} |
| 459 | |
|
| 460 | |
|
| 461 | |
|
| 462 | |
|
| 463 | |
|
| 464 | |
|
| 465 | |
|
| 466 | |
@SuppressWarnings("PMD.EmptyIfStmt") |
| 467 | |
public Collection<WithinDirectory> collectContents(final Document document, final Map<String, License> licenses, |
| 468 | |
final Map<String, Organisation> organisations) throws DuplicateElementException { |
| 469 | 516 | final Collection<WithinDirectory> results = new TreeSet<WithinDirectory>(); |
| 470 | |
@SuppressWarnings("unchecked") |
| 471 | 516 | final List<Element> children = document.getRootElement().getChildren("within"); |
| 472 | 516 | for (Element element: children) { |
| 473 | 65284 | if (results.add(directory(element, licenses, organisations))) { |
| 474 | |
|
| 475 | |
} else { |
| 476 | 2 | throw new DuplicateElementException("Duplicate parameter '" + element.getAttribute("dir").getValue() + "'"); |
| 477 | |
} |
| 478 | |
} |
| 479 | 514 | return results; |
| 480 | |
} |
| 481 | |
|
| 482 | |
|
| 483 | |
|
| 484 | |
|
| 485 | |
|
| 486 | |
|
| 487 | |
public Descriptor build(final Document document) { |
| 488 | 2 | final Map<String, Organisation> organisations = mapOrganisations(document); |
| 489 | 2 | final Map<String, License> licenses = mapLicenses(document); |
| 490 | 2 | final Map<String, String> notices = mapNotices(document); |
| 491 | 2 | final License primaryLicense = primaryLicense(document, licenses); |
| 492 | 2 | final String primaryCopyrightNotice = primaryCopyrightNotice(document); |
| 493 | 2 | final String primaryNotice = primaryNotice(document); |
| 494 | 2 | final String primaryOrganisationId = primaryOrganisationId(document); |
| 495 | 2 | final Collection<WithinDirectory> contents = collectContents(document, licenses, organisations); |
| 496 | 2 | return new Descriptor(primaryLicense, primaryCopyrightNotice, |
| 497 | |
primaryOrganisationId, primaryNotice, |
| 498 | |
licenses, notices, organisations, contents); |
| 499 | |
} |
| 500 | |
|
| 501 | |
public Descriptor build(final InputStream xmlStream) throws JDOMException, IOException { |
| 502 | 0 | return build(new SAXBuilder().build(xmlStream)); |
| 503 | |
} |
| 504 | |
} |