/plugin-module-codegen-api/src/main/java/com/atlassian/plugins/codegen/ModuleDescriptor.java

https://bitbucket.org/mmeinhold/amps · Java · 62 lines · 48 code · 10 blank · 4 comment · 0 complexity · 1347501f1bf37148819281180e8088c5 MD5 · raw file

  1. package com.atlassian.plugins.codegen;
  2. import static com.google.common.base.Preconditions.checkNotNull;
  3. import org.dom4j.Document;
  4. import org.dom4j.DocumentHelper;
  5. import org.dom4j.Element;
  6. /**
  7. * Describes a plugin module element that should be added to the plugin XML file.
  8. * This is provided as an arbitrary XML fragment string.
  9. */
  10. public class ModuleDescriptor implements PluginProjectChange
  11. {
  12. private final Element content;
  13. public static ModuleDescriptor moduleDescriptor(String content)
  14. {
  15. return new ModuleDescriptor(parseXml(content));
  16. }
  17. public static ModuleDescriptor moduleDescriptor(Element content)
  18. {
  19. return new ModuleDescriptor(content);
  20. }
  21. private ModuleDescriptor(Element content)
  22. {
  23. this.content = checkNotNull(content, "content");
  24. }
  25. public String getType()
  26. {
  27. return content.getName();
  28. }
  29. public Element getContent()
  30. {
  31. return content;
  32. }
  33. @Override
  34. public String toString()
  35. {
  36. return "[module: " + getType() + "]";
  37. }
  38. private static Element parseXml(String content)
  39. {
  40. try
  41. {
  42. Document doc = DocumentHelper.parseText(content);
  43. Element root = doc.getRootElement();
  44. root.detach();
  45. return root;
  46. }
  47. catch (Exception e)
  48. {
  49. throw new IllegalArgumentException("Invalid XML content for module descriptor", e);
  50. }
  51. }
  52. }