/tools/fits/0.6.0/src/edu/harvard/hul/ois/fits/tools/utils/XmlUtils.java

https://github.com/openplanets/ref · Java · 174 lines · 93 code · 13 blank · 68 comment · 19 complexity · 29db8eee22585444a3818688a9aeacb3 MD5 · raw file

  1. /*
  2. * Copyright 2009 Harvard University Library
  3. *
  4. * This file is part of FITS (File Information Tool Set).
  5. *
  6. * FITS is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * FITS is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with FITS. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package edu.harvard.hul.ois.fits.tools.utils;
  20. import java.util.ArrayList;
  21. import java.util.List;
  22. import org.jdom.Attribute;
  23. import org.jdom.Document;
  24. import org.jdom.Element;
  25. import org.jdom.JDOMException;
  26. import org.jdom.Namespace;
  27. import org.jdom.xpath.XPath;
  28. import edu.harvard.hul.ois.fits.Fits;
  29. import edu.harvard.hul.ois.fits.identity.ExternalIdentifier;
  30. import edu.harvard.hul.ois.fits.identity.FileIdentity;
  31. import edu.harvard.hul.ois.fits.tools.ToolInfo;
  32. public class XmlUtils {
  33. /**
  34. * Returns the first value of the element in the provided dom object. Returns
  35. * empty string if not found.
  36. * @param dom
  37. * @param element
  38. * @return the element value or empty string
  39. */
  40. public static String getDomValue(Document dom, String element) {
  41. try {
  42. Element e = (Element)XPath.selectSingleNode(dom,"//"+element);
  43. if(e != null) {
  44. return e.getText();
  45. }
  46. } catch (JDOMException e) {
  47. e.printStackTrace();
  48. }
  49. return null;
  50. }
  51. /**
  52. * concatenates the values of all elements children into a single string and returns it
  53. * @param dom
  54. * @param element
  55. * @return
  56. */
  57. public static String getChildDomValues(Document dom, String element) {
  58. String s = "";
  59. try {
  60. Element e = (Element)XPath.selectSingleNode(dom,"//"+element);
  61. if(e != null) {
  62. for(Element ee : (List<Element>)e.getChildren()) {
  63. s = s + ee.getText() + " ";
  64. }
  65. return s;
  66. }
  67. } catch (JDOMException e) {
  68. e.printStackTrace();
  69. }
  70. return null;
  71. }
  72. /*
  73. public static Document wrapInDom(String elementName, String text) {
  74. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  75. DocumentBuilder docBuilder = null;
  76. try {
  77. docBuilder = factory.newDocumentBuilder();
  78. } catch (ParserConfigurationException e) {
  79. e.printStackTrace();
  80. }
  81. Document dom = docBuilder.newDocument();
  82. Element element = dom.createElement(elementName);
  83. CDATASection cdata = dom.createCDATASection(text);
  84. element.appendChild(cdata);
  85. dom.appendChild(element);
  86. return dom;
  87. }
  88. */
  89. /*
  90. public static void printToConsole(Document dom) {
  91. XMLOutputter outputter = new XMLOutputter();
  92. try {
  93. outputter.output(dom, System.out);
  94. } catch (IOException e) {
  95. e.printStackTrace();
  96. }
  97. }*/
  98. /*
  99. public static Element getChildWithAttribute(Element parent, String attName, String attValue) {
  100. for(Element e:(List<Element>)parent.getChildren()) {
  101. Attribute attr = e.getAttribute(attName);
  102. if(attr != null && attr.getValue().equalsIgnoreCase(attValue)) {
  103. return e;
  104. }
  105. }
  106. return null;
  107. }*/
  108. public static Element getChildWithAttribute(Element e, Attribute a) {
  109. Element foundE = null;
  110. Attribute aa = e.getAttribute(a.getName());
  111. if(aa != null && aa.getValue().equalsIgnoreCase(a.getValue())) {
  112. return e;
  113. }
  114. for(Element ee:(List<Element>)e.getChildren()) {
  115. foundE = getChildWithAttribute(ee,a);
  116. if(foundE != null) {
  117. break;
  118. }
  119. }
  120. return foundE;
  121. }
  122. public static List<FileIdentity> getFileIdentities(Document dom, ToolInfo info) {
  123. List<FileIdentity> identities = new ArrayList<FileIdentity>();
  124. try {
  125. XPath xpath = XPath.newInstance("//fits:identity");
  126. Namespace ns = Namespace.getNamespace("fits",Fits.fitsXmlNamespace);
  127. xpath.addNamespace(ns);
  128. List<Element> identElements = xpath.selectNodes(dom);
  129. for(Element element : identElements) {
  130. Attribute formatAttr = element.getAttribute("format");
  131. Attribute mimetypeAttr = element.getAttribute("mimetype");
  132. Element versionElement = element.getChild("version",ns);
  133. String format = null;
  134. String mimetype = null;
  135. String version = null;
  136. if(formatAttr != null) {
  137. format = formatAttr.getValue();
  138. }
  139. if(mimetypeAttr != null) {
  140. mimetype = mimetypeAttr.getValue();
  141. }
  142. if(versionElement != null) {
  143. version = versionElement.getText();
  144. }
  145. FileIdentity identity = new FileIdentity(mimetype,format,version,info);
  146. List<Element> xIDElements = element.getChildren("externalIdentifier",ns);
  147. for(Element xIDElement : xIDElements) {
  148. String type = xIDElement.getAttributeValue("type");
  149. String value = xIDElement.getText();
  150. ExternalIdentifier xid = new ExternalIdentifier(type,value,info);
  151. identity.addExternalIdentifier(xid);
  152. }
  153. identities.add(identity);
  154. }
  155. } catch (JDOMException e) {
  156. e.printStackTrace();
  157. }
  158. return identities;
  159. }
  160. }