PageRenderTime 76ms CodeModel.GetById 44ms RepoModel.GetById 0ms app.codeStats 0ms

/tools/fits/0.6.0/src/edu/harvard/hul/ois/fits/tools/fileutility/FileUtility.java

https://github.com/openplanets/ref
Java | 253 lines | 160 code | 24 blank | 69 comment | 33 complexity | 6be046116bb030197fd20168ccb66097 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.fileutility;
  20. import java.io.File;
  21. import java.util.ArrayList;
  22. import java.util.Arrays;
  23. import java.util.List;
  24. import java.util.regex.Matcher;
  25. import java.util.regex.Pattern;
  26. import org.jdom.Document;
  27. import org.jdom.Element;
  28. import edu.harvard.hul.ois.fits.Fits;
  29. import edu.harvard.hul.ois.fits.exceptions.FitsToolCLIException;
  30. import edu.harvard.hul.ois.fits.exceptions.FitsToolException;
  31. import edu.harvard.hul.ois.fits.tools.ToolBase;
  32. import edu.harvard.hul.ois.fits.tools.ToolInfo;
  33. import edu.harvard.hul.ois.fits.tools.ToolOutput;
  34. import edu.harvard.hul.ois.fits.tools.utils.CommandLine;
  35. public class FileUtility extends ToolBase {
  36. private boolean osIsWindows = false;
  37. private boolean osHasTool = false;
  38. private List<String> WIN_COMMAND = new ArrayList<String>(Arrays.asList(Fits.FITS_TOOLS+"file_utility_windows/bin/file.exe"));
  39. private List<String> UNIX_COMMAND = new ArrayList<String>(Arrays.asList("file"));
  40. private List<String> FILE_TEST_COMMAND = new ArrayList<String>(Arrays.asList("which", "file"));
  41. private final static String WIN_FILE_DATE = "6/7/2008";
  42. private boolean enabled = true;
  43. public final static String xslt = Fits.FITS_XML+"fileutility/fileutility_to_fits.xslt";
  44. public FileUtility() throws FitsToolException{
  45. String osName = System.getProperty("os.name");
  46. info = new ToolInfo();
  47. String versionOutput = null;
  48. List<String> infoCommand = new ArrayList<String>();
  49. info.setName("file utility");
  50. if (osName.startsWith("Windows")) {
  51. //use provided Windows File Utility
  52. osIsWindows = true;
  53. info.setDate(WIN_FILE_DATE);
  54. infoCommand.addAll(WIN_COMMAND);
  55. }
  56. else if (testOSForCommand()){
  57. osHasTool = true;
  58. //use file command in operating system
  59. infoCommand.addAll(UNIX_COMMAND);
  60. }
  61. else {
  62. //Tool cannot be used on this system
  63. throw new FitsToolException("File Utility cannot be used on this system");
  64. }
  65. infoCommand.add("-v");
  66. versionOutput = CommandLine.exec(infoCommand,null);
  67. String[] lines = versionOutput.split("\n");
  68. String firstLine = lines[0];
  69. String[] nameVersion = firstLine.split("-");
  70. info.setVersion(nameVersion[nameVersion.length-1].trim());
  71. info.setNote(lines[1]);
  72. }
  73. public ToolOutput extractInfo(File file) throws FitsToolException {
  74. List<String> execCommand = new ArrayList<String>();
  75. if (osIsWindows) {
  76. //use provided Windows File Utility
  77. execCommand.addAll(WIN_COMMAND);
  78. execCommand.add("-e");
  79. execCommand.add("cdf");
  80. execCommand.add(file.getPath());
  81. }
  82. else if(osHasTool) {
  83. //use file command in operating system
  84. execCommand.addAll(UNIX_COMMAND);
  85. execCommand.add("-e");
  86. execCommand.add("cdf");
  87. execCommand.add(file.getPath());
  88. }
  89. else {
  90. //Tool cannot be used on this system
  91. return null;
  92. }
  93. execCommand.add("-b");
  94. String execOut = CommandLine.exec(execCommand,null);
  95. if(execOut != null && execOut.length() > 0) {
  96. execOut = execOut.trim();
  97. }
  98. else {
  99. execOut = "";
  100. }
  101. execCommand.add("--mime");
  102. String execMimeOut = CommandLine.exec(execCommand,null);
  103. if(execMimeOut != null && execMimeOut.length() > 0) {
  104. execMimeOut = execMimeOut.trim();
  105. }
  106. else {
  107. execMimeOut = "";
  108. }
  109. String format = null;
  110. String mime = null;
  111. String charset = null;
  112. List<String> linebreaks = new ArrayList<String>();
  113. //if mime indicates plain text
  114. if(execMimeOut.startsWith("text/") && execMimeOut.contains("charset=")) {
  115. //mime = "text/plain";
  116. mime = execMimeOut.substring(0,execMimeOut.indexOf("; charset="));
  117. charset = execMimeOut.substring(execMimeOut.indexOf("=")+1);
  118. charset = charset.toUpperCase();
  119. /*if(execOut.contains("ASCII text") ||
  120. execOut.contains("Unicode text, UTF-32") ||
  121. execOut.contains("UTF-8 Unicode") ||
  122. execOut.contains("UTF-16 Unicode") ||
  123. execOut.contains("Non-ISO extended-ASCII text") ||
  124. execOut.contains("ISO-8859")) {
  125. format = "Plain text";
  126. }*/
  127. format = "Plain text";
  128. Pattern p = Pattern.compile("(.*) with (.*) line terminators");
  129. Matcher m = p.matcher(execOut);
  130. if(m.matches()) {
  131. String endings = m.group(2);
  132. String[] breaks = endings.split(",");
  133. for(String b : breaks) {
  134. if(b.equals("CRLF")) {
  135. b = "CR/LF";
  136. }
  137. linebreaks.add(b);
  138. }
  139. }
  140. }
  141. else if(execMimeOut.contains("charset=")) {
  142. format = execOut;
  143. mime = execMimeOut.substring(0,execMimeOut.indexOf("; charset="));
  144. }
  145. //else use output for format
  146. else {
  147. format = execOut;
  148. mime = execMimeOut;
  149. }
  150. Document rawOut = createXml(mime,format,charset,linebreaks,execOut+"\n"+execMimeOut);
  151. Document fitsXml = transform(xslt,rawOut);
  152. /*
  153. XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
  154. try {
  155. outputter.output(fitsXml, System.out);
  156. } catch (IOException e) {
  157. // TODO Auto-generated catch block
  158. e.printStackTrace();
  159. }
  160. */
  161. output = new ToolOutput(this,fitsXml,rawOut);
  162. return output;
  163. }
  164. public boolean testOSForCommand() throws FitsToolCLIException {
  165. String output = CommandLine.exec(FILE_TEST_COMMAND,null);
  166. if(output == null || output.length() == 0) {
  167. return false;
  168. }
  169. else {
  170. return true;
  171. }
  172. }
  173. private Document createXml(String mime_s, String format_s, String charset_s, List<String> linebreaks, String rawOutput_s) throws FitsToolException {
  174. //xml root
  175. Element root = new Element("fileUtilityOutput");
  176. //rawoutput
  177. Element rawOutput = new Element("rawOutput");
  178. rawOutput.setText(rawOutput_s);
  179. root.addContent(rawOutput);
  180. //mimetype
  181. Element mime = new Element("mimetype");
  182. mime.setText(mime_s);
  183. root.addContent(mime);
  184. //format
  185. Element format = new Element("format");
  186. format.setText(format_s);
  187. root.addContent(format);
  188. //charset
  189. if(charset_s != null) {
  190. Element charset = new Element("charset");
  191. charset.setText(charset_s);
  192. root.addContent(charset);
  193. }
  194. if(linebreaks.size() > 0) {
  195. for(String l : linebreaks) {
  196. Element linebreak = new Element("linebreak");
  197. linebreak.setText(l);
  198. root.addContent(linebreak);
  199. }
  200. }
  201. return new Document(root);
  202. }
  203. /*
  204. public boolean isIdentityKnown(FileIdentity identity) {
  205. //identity and mimetype must not be null or empty strings for an identity to be "known"
  206. if(identity == null
  207. || identity.getMime() == null
  208. || identity.getMime().length() == 0
  209. || identity.getFormat() == null
  210. || identity.getFormat().length() == 0) {
  211. return false;
  212. }
  213. String format = identity.getFormat();
  214. String mime = identity.getMime();
  215. if(format.equals("data") || format.equals("Unknown Binary") || mime.equals("application/octet-stream")) {
  216. return false;
  217. }
  218. else {
  219. return true;
  220. }
  221. }
  222. */
  223. public boolean isEnabled() {
  224. return enabled;
  225. }
  226. public void setEnabled(boolean value) {
  227. enabled = value;
  228. }
  229. }