PageRenderTime 38ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://fits.googlecode.com/
Java | 255 lines | 162 code | 24 blank | 69 comment | 34 complexity | bee661aafe7ace8226639d15c3b43fe9 MD5 | raw file
Possible License(s): GPL-3.0, Apache-2.0, LGPL-3.0, BSD-3-Clause
  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. if(info.getVersion().startsWith("5")) {
  86. execCommand.add("-e");
  87. execCommand.add("cdf");
  88. }
  89. execCommand.add(file.getPath());
  90. }
  91. else {
  92. //Tool cannot be used on this system
  93. return null;
  94. }
  95. execCommand.add("-b");
  96. String execOut = CommandLine.exec(execCommand,null);
  97. if(execOut != null && execOut.length() > 0) {
  98. execOut = execOut.trim();
  99. }
  100. else {
  101. execOut = "";
  102. }
  103. execCommand.add("--mime");
  104. String execMimeOut = CommandLine.exec(execCommand,null);
  105. if(execMimeOut != null && execMimeOut.length() > 0) {
  106. execMimeOut = execMimeOut.trim();
  107. }
  108. else {
  109. execMimeOut = "";
  110. }
  111. String format = null;
  112. String mime = null;
  113. String charset = null;
  114. List<String> linebreaks = new ArrayList<String>();
  115. //if mime indicates plain text
  116. if(execMimeOut.startsWith("text/") && execMimeOut.contains("charset=")) {
  117. //mime = "text/plain";
  118. mime = execMimeOut.substring(0,execMimeOut.indexOf("; charset="));
  119. charset = execMimeOut.substring(execMimeOut.indexOf("=")+1);
  120. charset = charset.toUpperCase();
  121. /*if(execOut.contains("ASCII text") ||
  122. execOut.contains("Unicode text, UTF-32") ||
  123. execOut.contains("UTF-8 Unicode") ||
  124. execOut.contains("UTF-16 Unicode") ||
  125. execOut.contains("Non-ISO extended-ASCII text") ||
  126. execOut.contains("ISO-8859")) {
  127. format = "Plain text";
  128. }*/
  129. format = "Plain text";
  130. Pattern p = Pattern.compile("(.*) with (.*) line terminators");
  131. Matcher m = p.matcher(execOut);
  132. if(m.matches()) {
  133. String endings = m.group(2);
  134. String[] breaks = endings.split(",");
  135. for(String b : breaks) {
  136. if(b.equals("CRLF")) {
  137. b = "CR/LF";
  138. }
  139. linebreaks.add(b);
  140. }
  141. }
  142. }
  143. else if(execMimeOut.contains("charset=")) {
  144. format = execOut;
  145. mime = execMimeOut.substring(0,execMimeOut.indexOf("; charset="));
  146. }
  147. //else use output for format
  148. else {
  149. format = execOut;
  150. mime = execMimeOut;
  151. }
  152. Document rawOut = createXml(mime,format,charset,linebreaks,execOut+"\n"+execMimeOut);
  153. Document fitsXml = transform(xslt,rawOut);
  154. /*
  155. XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
  156. try {
  157. outputter.output(fitsXml, System.out);
  158. } catch (IOException e) {
  159. // TODO Auto-generated catch block
  160. e.printStackTrace();
  161. }
  162. */
  163. output = new ToolOutput(this,fitsXml,rawOut);
  164. return output;
  165. }
  166. public boolean testOSForCommand() throws FitsToolCLIException {
  167. String output = CommandLine.exec(FILE_TEST_COMMAND,null);
  168. if(output == null || output.length() == 0) {
  169. return false;
  170. }
  171. else {
  172. return true;
  173. }
  174. }
  175. private Document createXml(String mime_s, String format_s, String charset_s, List<String> linebreaks, String rawOutput_s) throws FitsToolException {
  176. //xml root
  177. Element root = new Element("fileUtilityOutput");
  178. //rawoutput
  179. Element rawOutput = new Element("rawOutput");
  180. rawOutput.setText(rawOutput_s);
  181. root.addContent(rawOutput);
  182. //mimetype
  183. Element mime = new Element("mimetype");
  184. mime.setText(mime_s);
  185. root.addContent(mime);
  186. //format
  187. Element format = new Element("format");
  188. format.setText(format_s);
  189. root.addContent(format);
  190. //charset
  191. if(charset_s != null) {
  192. Element charset = new Element("charset");
  193. charset.setText(charset_s);
  194. root.addContent(charset);
  195. }
  196. if(linebreaks.size() > 0) {
  197. for(String l : linebreaks) {
  198. Element linebreak = new Element("linebreak");
  199. linebreak.setText(l);
  200. root.addContent(linebreak);
  201. }
  202. }
  203. return new Document(root);
  204. }
  205. /*
  206. public boolean isIdentityKnown(FileIdentity identity) {
  207. //identity and mimetype must not be null or empty strings for an identity to be "known"
  208. if(identity == null
  209. || identity.getMime() == null
  210. || identity.getMime().length() == 0
  211. || identity.getFormat() == null
  212. || identity.getFormat().length() == 0) {
  213. return false;
  214. }
  215. String format = identity.getFormat();
  216. String mime = identity.getMime();
  217. if(format.equals("data") || format.equals("Unknown Binary") || mime.equals("application/octet-stream")) {
  218. return false;
  219. }
  220. else {
  221. return true;
  222. }
  223. }
  224. */
  225. public boolean isEnabled() {
  226. return enabled;
  227. }
  228. public void setEnabled(boolean value) {
  229. enabled = value;
  230. }
  231. }