PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/bundles/plugins-trunk/XML/xml/translate/TrangTranslator.java

#
Java | 251 lines | 188 code | 41 blank | 22 comment | 31 complexity | 1ecad943cb39d72407a5cda2dc9dbb09 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  1. /*
  2. * TrangTranslator.java
  3. * :folding=explicit:collapseFolds=1:
  4. *
  5. * Copyright (C) 2010 Eric Le Lay
  6. *
  7. * The XML plugin is licensed under the GNU General Public License, with
  8. * the following exception:
  9. *
  10. * "Permission is granted to link this code with software released under
  11. * the Apache license version 1.1, for example used by the Xerces XML
  12. * parser package."
  13. *
  14. * This class has been inspired by
  15. * jing-trang-20090818/mod/trang/src/main/com/thaiopensource/relaxng/translate/Driver.java
  16. */
  17. package xml.translate;
  18. // {{{ Imports
  19. import com.thaiopensource.relaxng.edit.SchemaCollection;
  20. import com.thaiopensource.relaxng.input.InputFailedException;
  21. import com.thaiopensource.relaxng.input.InputFormat;
  22. import com.thaiopensource.relaxng.input.MultiInputFormat;
  23. import com.thaiopensource.relaxng.output.OutputDirectory;
  24. import com.thaiopensource.relaxng.output.OutputFailedException;
  25. import com.thaiopensource.relaxng.output.OutputFormat;
  26. import com.thaiopensource.relaxng.translate.util.InvalidParamsException;
  27. import com.thaiopensource.resolver.Resolver;
  28. import com.thaiopensource.resolver.xml.sax.SAX;
  29. import com.thaiopensource.relaxng.translate.Formats;
  30. import org.xml.sax.SAXException;
  31. import org.xml.sax.ErrorHandler;
  32. import java.io.IOException;
  33. import java.util.ArrayList;
  34. import java.util.List;
  35. import java.util.Arrays;
  36. import java.util.Collections;
  37. import org.gjt.sp.util.Log;
  38. import org.gjt.sp.jedit.jEdit;
  39. import org.gjt.sp.jedit.Buffer;
  40. import org.gjt.sp.jedit.MiscUtilities;
  41. import org.gjt.sp.jedit.View;
  42. import org.gjt.sp.jedit.GUIUtilities;
  43. import errorlist.ErrorSource;
  44. import errorlist.DefaultErrorSource;
  45. // }}}
  46. public class TrangTranslator
  47. {
  48. private static DefaultErrorSource errorSource;
  49. static final String[] inputTypes = new String[]{"rng","rnc","dtd","xml"};
  50. static final String[] outputTypes = new String[]{"rng","rnc","dtd","xsd"};
  51. public static void stop()
  52. {
  53. if(errorSource != null)
  54. {
  55. ErrorSource.unregisterErrorSource(errorSource);
  56. errorSource = null;
  57. }
  58. }
  59. private static void initErrorSource()
  60. {
  61. errorSource = new DefaultErrorSource("xml.TrangTranslator");
  62. ErrorSource.registerErrorSource(errorSource);
  63. }
  64. public static String guessInputType(Buffer buffer){
  65. String input = buffer.getPath();
  66. String extension = null;
  67. if(input.length()>0){
  68. extension = input.substring(input.length() - 3);
  69. }
  70. String inputType;
  71. if(Arrays.asList(inputTypes).contains(extension))
  72. {
  73. inputType = extension;
  74. }
  75. else
  76. {
  77. inputType = buffer.getMode().getName();
  78. if(!Arrays.asList(inputTypes).contains(inputType))
  79. {
  80. inputType = null;
  81. }
  82. }
  83. return inputType;
  84. }
  85. public static void translateCurrentBuffer(View view, Buffer buffer, String outputType)
  86. {
  87. String inputType;
  88. String input = buffer.getPath();
  89. // infer the input type
  90. inputType = guessInputType(buffer);
  91. // not guessed => ask the user
  92. if(inputType == null)
  93. {
  94. int res = GUIUtilities.listConfirm(
  95. view
  96. , "xml.translate.choose-input-type"
  97. , new String[]{}
  98. , inputTypes);
  99. if(res >=0 && res < inputTypes.length)
  100. {
  101. inputType = inputTypes[res];
  102. }
  103. else
  104. {
  105. return;
  106. }
  107. }
  108. List<String> inputs = Collections.singletonList(input);
  109. // infer the output
  110. String output;
  111. if(input.endsWith(inputType)){
  112. output = input.replaceAll(inputType+"$",outputType);
  113. }else{
  114. output = input + "." + outputType;
  115. }
  116. // translate
  117. translate(view, inputType, inputs, Collections.<String>emptyList(), outputType, output, Collections.<String>emptyList());
  118. }
  119. public static void translate(View view, String inputType, List<String> inputs, List<String> inputParams, String outputType, String outputFilename, List<String> outputParams)
  120. {
  121. if(inputs.isEmpty())throw new IllegalArgumentException("must provide at least one input");
  122. String mainInput = inputs.get(0);
  123. if(errorSource == null)initErrorSource();
  124. ErrorHandler eh = new xml.parser.ErrorListErrorHandler(errorSource,mainInput);
  125. if (inputType == null) {
  126. inputType = MiscUtilities.getFileExtension(mainInput);
  127. if (inputType.length() > 0)
  128. inputType = inputType.substring(1);
  129. }
  130. final InputFormat inputFormat;
  131. if (inputType.equalsIgnoreCase("dtd")){
  132. inputFormat = new BufferDtdInputFormat();
  133. }else if(inputType.equalsIgnoreCase("rng")){
  134. inputFormat = new BufferSAXParseInputFormat();
  135. }else if(inputType.equalsIgnoreCase("rnc")){
  136. inputFormat = new BufferCompactParseInputFormat();
  137. }else{
  138. inputFormat = Formats.createInputFormat(inputType);
  139. }
  140. if (inputFormat == null) {
  141. throw new IllegalArgumentException("unsupported input format : "+inputType);
  142. }
  143. String ext = MiscUtilities.getFileExtension(outputFilename);
  144. if (outputType == null) {
  145. outputType = ext;
  146. if (outputType.length() > 0)
  147. outputType = outputType.substring(1);
  148. }
  149. final OutputFormat outputFormat = Formats.createOutputFormat(outputType);
  150. if (outputFormat == null) {
  151. throw new IllegalArgumentException("unsupported output format : "+outputType);
  152. }
  153. Resolver resolver = new EntityResolverWrapper(xml.Resolver.instance(),true);
  154. String[] inputParamArray = inputParams.toArray(new String[inputParams.size()]);
  155. outputType = outputType.toLowerCase();
  156. SchemaCollection sc;
  157. try
  158. {
  159. if (inputs.size() > 1) {
  160. if (!(inputFormat instanceof MultiInputFormat)) {
  161. throw new IllegalArgumentException("Only XML input type can handle multiple inputs !");
  162. }
  163. String[] uris = new String[inputs.size()];
  164. for (int i = 0; i < uris.length; i++)
  165. {
  166. uris[i] = xml.PathUtilities.pathToURL(inputs.get(i));
  167. }
  168. sc = ((MultiInputFormat)inputFormat).load(uris, inputParamArray, outputType, eh, resolver);
  169. }
  170. else
  171. {
  172. sc = inputFormat.load(xml.PathUtilities.pathToURL(mainInput), inputParamArray, outputType, eh, resolver);
  173. }
  174. if (ext.length() == 0) ext = outputType;
  175. BuffersOutputDirectory od = new BuffersOutputDirectory(view, sc.getMainUri(), outputFilename);
  176. outputFormat.output(sc, od, outputParams.toArray(new String[outputParams.size()]), inputType.toLowerCase(), eh);
  177. String done = jEdit.getProperty("xml.translate.done.message",new String[]{String.valueOf(od.getOutputCount())});
  178. Log.log(Log.MESSAGE,TrangTranslator.class,done);
  179. GUIUtilities.message(
  180. view
  181. , "xml.translate.done"
  182. , new String[]{String.valueOf(od.getOutputCount())});
  183. }
  184. catch (OutputFailedException e)
  185. {
  186. GUIUtilities.error(view,"xml.translate.failed",new String[]{e.toString()});
  187. e.printStackTrace();
  188. }
  189. catch (InputFailedException e)
  190. {
  191. GUIUtilities.error(view,"xml.translate.failed",new String[]{e.toString()});
  192. e.printStackTrace();
  193. }
  194. catch (InvalidParamsException e)
  195. {
  196. GUIUtilities.error(view,"xml.translate.failed",new String[]{e.toString()});
  197. e.printStackTrace();
  198. }
  199. catch (IOException e)
  200. {
  201. GUIUtilities.error(view,"xml.translate.failed",new String[]{e.toString()});
  202. e.printStackTrace();
  203. }
  204. catch (SAXException e)
  205. {
  206. GUIUtilities.error(view,"xml.translate.failed",new String[]{e.toString()});
  207. e.printStackTrace();
  208. }
  209. }
  210. }