View Javadoc

1   /*
2    * Copyright 2007 - 2007 JEuclid, http://jeuclid.sf.net
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  /* $Id: Mml2xxx.java 319 2007-05-21 14:55:54Z maxberger $ */
18  
19  package net.sourceforge.jeuclid.app;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.util.Map;
24  
25  import net.sourceforge.jeuclid.Converter;
26  import net.sourceforge.jeuclid.MathBase;
27  import net.sourceforge.jeuclid.ParameterKey;
28  import net.sourceforge.jeuclid.app.support.CommandLineParser;
29  
30  /**
31   * Utility class to be used from the command line to call the converters.
32   * 
33   * @author Max Berger
34   * @version $Revision: 319 $
35   */
36  public final class Mml2xxx {
37  
38      private static final String COLON = ": ";
39  
40      private static final String SPACE = " ";
41  
42      private Mml2xxx() {
43          // Empty on purpose
44      }
45  
46      /**
47       * Main function for use from scripts.
48       * 
49       * @param args
50       *            command line arguments.
51       */
52      public static void main(final String[] args) {
53  
54          try {
55              final CommandLineParser.ParseResults parseResults = CommandLineParser
56                      .parseCommandLine(args);
57              final File source = parseResults.getSource();
58              final File target = parseResults.getTarget();
59  
60              final Map<ParameterKey, String> params = MathBase
61                      .getDefaultParameters();
62              final boolean mimeTypeIsSet = parseResults.getParams()
63                      .containsKey(ParameterKey.OutFileType);
64              params.putAll(parseResults.getParams());
65  
66              if (source == null) {
67                  throw new IllegalArgumentException("No source given");
68              } else if (target == null) {
69                  throw new IllegalArgumentException("No target given");
70              } else if (!source.isFile()) {
71                  throw new IllegalArgumentException("Source is not a file");
72              }
73  
74              if (!mimeTypeIsSet) {
75                  final String fileName = target.getName();
76                  final String extension = fileName.substring(fileName
77                          .lastIndexOf('.') + 1);
78                  final String mimetype = Converter
79                          .getMimeTypeForSuffix(extension);
80                  params.put(ParameterKey.OutFileType, mimetype);
81              }
82  
83              Converter.convert(source, target, params);
84  
85          } catch (ArrayIndexOutOfBoundsException aiobe) {
86              Mml2xxx.showUsage();
87              System.out.println(aiobe.getClass().toString() + Mml2xxx.COLON
88                      + aiobe.getMessage());
89          } catch (IllegalArgumentException ia) {
90              Mml2xxx.showUsage();
91              System.out.println(ia.getClass().toString() + Mml2xxx.COLON
92                      + ia.getMessage());
93          } catch (final IOException e) {
94              Mml2xxx.showUsage();
95              System.out.println(e.getClass().toString() + Mml2xxx.COLON
96                      + e.getMessage());
97          }
98      }
99  
100     private static void showUsage() {
101         System.out.println("JEuclid BasicConverter");
102         System.out.println("");
103         System.out.println("Usage:");
104         System.out.println("");
105         System.out.println("mml2xxx source target (option value)*");
106         System.out.println("");
107         System.out.println("where:");
108         System.out
109                 .println(" source is the path to the source file (MathML or ODF format)");
110         System.out.println(" target is the path to the target file");
111         System.out.println("Possible options (with default value):");
112         final ParameterKey[] options = ParameterKey.values();
113         for (int i = 0; i < options.length; i++) {
114             final ParameterKey param = options[i];
115             final String name = param.name();
116             System.out.print(" -" + name);
117             System.out.print(Mml2xxx.SPACE);
118             System.out.println(MathBase.getDefaultParameters().get(param));
119         }
120         System.out.println("The following output types are supported:");
121         System.out.print("   ");
122         for (final String type : Converter.getAvailableOutfileTypes()) {
123             System.out.print(Mml2xxx.SPACE + type);
124         }
125         System.out.println();
126         System.out.println("Example: ");
127         System.out.println("  mml2xxx a.mml a.png -BackgroundColor white");
128         System.out.println();
129         System.out.println();
130 
131     }
132 
133 }