PageRenderTime 57ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 1ms

/carrot-jdk6-jnlp-macosx/src/plugin/share/converter/sun/plugin/converter/util/CommandLine.java

https://github.com/carrot-garden/carrot-jnlper
Java | 523 lines | 280 code | 70 blank | 173 comment | 66 complexity | 7fab46fac4187acd299efacdce76b4ea MD5 | raw file
  1. /*
  2. * @(#)CommandLine.java 1.23 10/03/24
  3. *
  4. * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
  5. * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package sun.plugin.converter.util;
  8. import sun.plugin.converter.*;
  9. import java.util.*;
  10. import java.io.*;
  11. public class CommandLine{
  12. /**
  13. * The command line to be processed
  14. */
  15. private String commandLine[];
  16. /**
  17. * boolean helper, do gui or not
  18. */
  19. private boolean gui = false;
  20. /**
  21. * boolean helper, are params present?
  22. */
  23. private boolean params = false;
  24. /**
  25. * boolean helper, was help a given parameter?
  26. */
  27. private boolean help = false;
  28. /**
  29. * boolean helper, was subdirs a given parameter?
  30. */
  31. private boolean processSubDirs = false;
  32. /**
  33. * boolean helper, was simulate a given parameter?
  34. */
  35. private boolean simulate = false;
  36. /**
  37. * boolean helper, was -log a given parameter?
  38. */
  39. private boolean logFile = false;
  40. /**
  41. * boolean helper, -f (overwrite option)
  42. */
  43. private boolean forceOverwrite = false;
  44. /**
  45. * holds the source directory
  46. */
  47. private String sourceStr;
  48. /**
  49. * holds the destination directory
  50. */
  51. private String destStr;
  52. /**
  53. * holds the backup directory
  54. */
  55. private String backupStr;
  56. /**
  57. * holds the template directory, plus the template file
  58. */
  59. private String templateStr;
  60. /**
  61. * holds the log path specified on the commandline
  62. */
  63. private String logPath;
  64. /**
  65. * holds the progress string
  66. */
  67. private String progressStr = "false";
  68. /**
  69. * holds the file directory and file name
  70. */
  71. private Vector fileSpecs = new Vector();
  72. /**
  73. * Static versioning boolean. True if the user wants only a
  74. * particular JRE to run the applet.
  75. */
  76. private boolean staticVersioning = true;
  77. /**
  78. * holds "/" or "\"
  79. */
  80. private String sep = System.getProperty("file.separator");
  81. private String destPathSuffix = "_CONV";
  82. private String backupPathSuffix = "_BAK";
  83. private int maxFileCharLength = 27;
  84. private boolean stdin = false;
  85. private boolean stdout = false;
  86. /**
  87. * This will take in the string given at the command line by the
  88. * user find if it is null and calls readline().
  89. * @param s[] holds the given command line string
  90. */
  91. public CommandLine(String s[]) throws CommandLineException, IndexOutOfBoundsException {
  92. if (s==null || s.length == 0){ // there are no params
  93. params = false;
  94. }
  95. else{
  96. params = true;
  97. commandLine = s;
  98. readLine();
  99. //
  100. // If no files are specified, default.
  101. //
  102. if ((isStdIn() == false) && fileSpecs.isEmpty()) {
  103. String specs = ResourceHandler.getMessage("converter_gui.lablel2");
  104. StringTokenizer st = new StringTokenizer(specs," ,");
  105. while ( st.hasMoreTokens() ) {
  106. fileSpecs.add(st.nextToken());
  107. }
  108. }
  109. checkForProblems();
  110. }
  111. }
  112. /**
  113. * This will parse the given command line and set variables
  114. */
  115. public void readLine() throws CommandLineException, IndexOutOfBoundsException {
  116. String arg;
  117. try{
  118. for(int i = 0;i<commandLine.length;i++){
  119. String rawArg = commandLine[i];
  120. arg = rawArg.trim().toUpperCase(); // Get a param
  121. if(arg.startsWith("-")){
  122. if(isCmdLineOption(arg)){ // File Spec
  123. if(arg.equals("-GUI")){ // -gui
  124. i++;
  125. gui = true;
  126. i--;
  127. }
  128. else if(arg.equals("-HELP")){ // -help
  129. i++;
  130. help = true;
  131. i--;
  132. }
  133. else if(arg.equals("-F")){ // -f
  134. forceOverwrite = true;
  135. }
  136. else if(arg.equals("-SUBDIRS")){ // -subdirs
  137. processSubDirs = true;
  138. }
  139. else if(arg.equals("-SOURCE")){ // -source directory
  140. sourceStr = commandLine[++i];
  141. }
  142. else if(arg.equals("-DEST")){ // -destination location
  143. destStr = commandLine[++i];
  144. }
  145. else if(arg.equals("-BACKUP")){ // -backup location
  146. backupStr = commandLine[++i];
  147. }
  148. else if(arg.equals("-TEMPLATE")){ // -template file
  149. templateStr = commandLine[++i];
  150. }
  151. else if(arg.equals("-LOG")){// -log file
  152. logFile = true;
  153. logPath = commandLine[++i];
  154. }
  155. else if(arg.equals("-PROGRESS")){ // -progress
  156. progressStr = commandLine[++i];
  157. }
  158. else if(arg.equals("-SIMULATE")){ //just a simulation?
  159. simulate = true;
  160. }
  161. else if(arg.equals("-LATEST")) { // Use any JRE that supports this mimetype.
  162. staticVersioning = false;
  163. }
  164. }
  165. else {
  166. throw new CommandLineException();
  167. }
  168. }
  169. else fileSpecs.add(rawArg);
  170. }
  171. } catch(IndexOutOfBoundsException e)
  172. {
  173. help = true;
  174. throw e;
  175. }
  176. }
  177. /**
  178. * This will make sure the destination path isn't the
  179. * same as the backup directory, the application will
  180. * terminate if they are the same
  181. */
  182. private void checkForProblems() throws CommandLineException {
  183. if((backupStr != null) && (destStr != null)){
  184. File fBackupTemp = new File(backupStr);
  185. File fDestTemp = new File(destStr);
  186. if ( (fDestTemp.getAbsolutePath()).equals(fBackupTemp.getAbsolutePath())){
  187. System.out.println(ResourceHandler.getMessage("illegal_source_and_backup.info"));
  188. throw new CommandLineException();
  189. }
  190. }
  191. }
  192. /**
  193. *This will return the the boolean logFile value
  194. * @return value of logfile
  195. */
  196. public boolean isLogFile(){
  197. return logFile;
  198. }
  199. /**
  200. *This will return the the boolean params value
  201. * @return value of params
  202. */
  203. public boolean areParams(){
  204. return params;
  205. }
  206. /**
  207. *This will return the the boolean help value
  208. * @return value of help
  209. */
  210. public boolean justHelp(){
  211. return help;
  212. }
  213. /**
  214. *This will return the the boolean processSubDirs value
  215. * @return value of processSubDirs
  216. */
  217. public boolean doSubDirs(){
  218. return processSubDirs;
  219. }
  220. /**
  221. *This will return the the boolean gui value
  222. * @return value of gui
  223. */
  224. public boolean doGUI(){
  225. return gui;
  226. }
  227. /**
  228. *This will return the the boolean progressStr value
  229. * @return value of progressStr
  230. */
  231. public boolean showProgress(){
  232. if(progressStr.equals("true"))
  233. return true;
  234. return false;
  235. }
  236. /**
  237. *This will return the the boolean simulate value
  238. * @return value of simulate
  239. */
  240. public boolean justSimulate(){
  241. return simulate;
  242. }
  243. /**
  244. * Returns the value of -f, the force overwrite option.
  245. *
  246. * @return True to overwrite, false otherwise.
  247. */
  248. public boolean isOverwrite(){
  249. return forceOverwrite;
  250. }
  251. /**
  252. * This method will determine if the source is reading from standard in.
  253. * @retrun true, if the source is standard input; false, otherwise.
  254. */
  255. public boolean isStdIn() {
  256. if ((sourceStr != null) && sourceStr.equals("-")) {
  257. stdin = true;
  258. sourceStr = null; // reset sourceStr; since "-" is not the path of source
  259. }
  260. return stdin;
  261. }
  262. /**
  263. * This method will determine if the destination is writing to standard output.
  264. * @retrun true, if the dest is standard output; false, otherwise.
  265. */
  266. public boolean isStdOut() {
  267. if ((destStr != null ) && destStr.equals("-")) {
  268. stdout = true;
  269. destStr = null; // reset destStr; since "-" is not the path of destination
  270. }
  271. return stdout;
  272. }
  273. /**
  274. * This will return the absolute path of the source directory
  275. * no matter what the case is. If the user didn't enter a source
  276. * directory, it will return null
  277. * @return the value of the sourceDir path
  278. */
  279. public String getSourceDir(){
  280. String tempSource;
  281. if(sourceStr != null){
  282. tempSource = doesExistAndAbsolute(sourceStr);
  283. return tempSource;
  284. }
  285. else
  286. return null;
  287. }
  288. /**
  289. * This will return the absolute path of the Destination directory
  290. * no matter what the case is. If the user didn't enter a dest
  291. * directory, it will return null
  292. * @return the value of the destination path
  293. */
  294. public String getDestDir(){
  295. if(destStr != null){
  296. String tempdestStr = doesExistAndAbsolute(destStr);
  297. return tempdestStr;
  298. }
  299. else
  300. return null;
  301. }
  302. /**
  303. * This will return the absolute path of the backup directory
  304. * no matter what the case is. If the user didn't enter a backup
  305. * directory, it will return null
  306. * @return the value of the backup directory
  307. */
  308. public String getBackupDir(){
  309. return backupStr;
  310. }
  311. /**
  312. * This will always have to be a definate path up to the filename.
  313. * The doesExistAndAbsolute method will just make sure it exists
  314. * and that it is an absolute pathname. If the user didn't enter
  315. * in a template path, it will return null
  316. * @return the path of the template file
  317. */
  318. public String getTemplateDir(){
  319. if(templateStr != null){
  320. File bill = new File(templateStr);
  321. String tempTemplate = doesExistAndAbsolute(templateStr);
  322. return tempTemplate;
  323. }
  324. else
  325. return null;
  326. }
  327. /**
  328. * This will return the absolute path to the log file
  329. * or null if no log file was specified.
  330. * @return the full path to the logfile
  331. */
  332. public String getLogFile() {
  333. String path = null;
  334. if ( logPath != null ) {
  335. File log = new File(logPath);
  336. path = doesExistAndAbsolute(log.getParent()) +
  337. File.separator + log.getName();
  338. }
  339. return path;
  340. }
  341. /**
  342. * This will return the vector of the files to be converted
  343. * @return the vector of paths for the converted files
  344. */
  345. public Vector getFileSpecs(){
  346. return fileSpecs;
  347. }
  348. /**
  349. * Get the staticVersioning boolean value.
  350. * @return True if any JRE supporting the mimetype can be used to run the applet.
  351. */
  352. public boolean getStaticVersioning() {
  353. return staticVersioning;
  354. }
  355. private boolean isCmdLineOption(String option){
  356. String test = option.trim().toUpperCase();
  357. return (
  358. test.equals("-SOURCE") ||
  359. test.equals("-DEST") ||
  360. test.equals("-BACKUP") ||
  361. test.equals("-F") ||
  362. test.equals("-SUBDIRS") ||
  363. test.equals("-TEMPLATE") ||
  364. test.equals("-LOG") ||
  365. test.equals("-PROGRESS") ||
  366. test.equals("-GUI") ||
  367. test.equals("-HELP") ||
  368. test.equals("-SIMULATE") ||
  369. test.equals("-LATEST"));
  370. }
  371. /**
  372. * Note: this is not used, but should be when we decide what length is
  373. *
  374. */
  375. private String truncateString(String dir,int length) {
  376. // If the name of the source dir is too long to take the suffix chars,
  377. // we crop the source name before adding the suffix
  378. if(dir.length()>length) dir = dir.substring(0,length-1);
  379. return dir;
  380. }
  381. /**
  382. * This method will take in a string which will be a directory.
  383. * It will make sure that the directory exists and return the absolute path
  384. * @return the value of the absolute directory
  385. */
  386. private String doesExistAndAbsolute(String dir){
  387. File tempDirFile = new File(dir);
  388. try{
  389. if(tempDirFile.exists()){
  390. if(tempDirFile.isAbsolute()){
  391. return dir;
  392. }
  393. else{
  394. tempDirFile = new File(System.getProperty("user.dir")+sep+dir);
  395. if(tempDirFile.exists()){
  396. return tempDirFile.getAbsolutePath();
  397. }
  398. else{
  399. throw new NotDirectoryException(ResourceHandler.getMessage("caption.reldirnotfound")+": "+tempDirFile.getAbsolutePath());
  400. }
  401. }
  402. }
  403. else{
  404. throw new NotDirectoryException(ResourceHandler.getMessage("caption.absdirnotfound")+": "+tempDirFile.getAbsolutePath());
  405. }
  406. }catch(NotDirectoryException e){
  407. System.out.println(ResourceHandler.getMessage("caption.absdirnotfound")+": "+tempDirFile.getAbsolutePath());
  408. return null;//this is just so it would compile
  409. }
  410. }
  411. /**
  412. * This method will read the standard input and save the content to a temporary.
  413. * source file since the source file will be parsed mutiple times. The HTML source
  414. * file is parsed and checked for Charset in HTML tags at first time, then source file
  415. * is parsed again for the converting
  416. * The temporary file is read from standard input and saved in current
  417. * directory and will be deleted after the conversion has completed.
  418. * @return the vector of the converted temporary source file name
  419. */
  420. public Vector getStandardInput() throws FileAccessException {
  421. byte[] buf = new byte[2048];
  422. int len = 0;
  423. FileInputStream fis = null;
  424. FileOutputStream fos = null;
  425. File tempSourceFile;
  426. String tmpSourceName = ".tmpSource_stdin";
  427. File outFile = new File(tmpSourceName);
  428. tempSourceFile = new File(System.getProperty("user.dir")+sep+tmpSourceName);
  429. if(!tempSourceFile.exists()){
  430. try {
  431. fis = new FileInputStream (FileDescriptor.in);
  432. fos = new FileOutputStream(outFile);
  433. while ((len = fis.read(buf, 0, 2048)) != -1)
  434. fos.write(buf, 0, len);
  435. }
  436. catch (IOException e) {
  437. System.out.println(ResourceHandler.getMessage("plugin_converter.write_permission"));
  438. return null;
  439. }
  440. }
  441. else {
  442. throw new FileAccessException(ResourceHandler.getMessage("plugin_converter.overwrite"));
  443. }
  444. tempSourceFile = new File(System.getProperty("user.dir")+sep+tmpSourceName);
  445. if(tempSourceFile.exists()){
  446. fileSpecs.add(tmpSourceName);
  447. return fileSpecs;
  448. }
  449. else {
  450. throw new FileAccessException(ResourceHandler.getMessage("plugin_converter.write_permission"));
  451. }
  452. }
  453. }