PageRenderTime 78ms CodeModel.GetById 63ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 1ms

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

#
Java | 251 lines | 188 code | 41 blank | 22 comment | 31 complexity | 1ecad943cb39d72407a5cda2dc9dbb09 MD5 | raw file
  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 */
 17package xml.translate;
 18
 19// {{{ Imports
 20import com.thaiopensource.relaxng.edit.SchemaCollection;
 21import com.thaiopensource.relaxng.input.InputFailedException;
 22import com.thaiopensource.relaxng.input.InputFormat;
 23import com.thaiopensource.relaxng.input.MultiInputFormat;
 24import com.thaiopensource.relaxng.output.OutputDirectory;
 25import com.thaiopensource.relaxng.output.OutputFailedException;
 26import com.thaiopensource.relaxng.output.OutputFormat;
 27import com.thaiopensource.relaxng.translate.util.InvalidParamsException;
 28import com.thaiopensource.resolver.Resolver;
 29import com.thaiopensource.resolver.xml.sax.SAX;
 30
 31
 32import com.thaiopensource.relaxng.translate.Formats;
 33
 34import org.xml.sax.SAXException;
 35import org.xml.sax.ErrorHandler;
 36
 37
 38import java.io.IOException;
 39import java.util.ArrayList;
 40import java.util.List;
 41import java.util.Arrays;
 42import java.util.Collections;
 43
 44import org.gjt.sp.util.Log;
 45import org.gjt.sp.jedit.jEdit;
 46import org.gjt.sp.jedit.Buffer;
 47import org.gjt.sp.jedit.MiscUtilities;
 48import org.gjt.sp.jedit.View;
 49import org.gjt.sp.jedit.GUIUtilities;
 50
 51import errorlist.ErrorSource;
 52import errorlist.DefaultErrorSource;
 53
 54// }}}
 55
 56public class TrangTranslator
 57{
 58	private static DefaultErrorSource errorSource;
 59	
 60	static final String[] inputTypes = new String[]{"rng","rnc","dtd","xml"};
 61	static final String[] outputTypes = new String[]{"rng","rnc","dtd","xsd"};
 62	
 63	
 64	public static void stop()
 65	{
 66		if(errorSource != null)
 67		{
 68			ErrorSource.unregisterErrorSource(errorSource);
 69			errorSource = null;
 70		}
 71	}
 72	
 73	private static void initErrorSource()
 74	{
 75		errorSource = new DefaultErrorSource("xml.TrangTranslator");
 76		ErrorSource.registerErrorSource(errorSource);
 77	}
 78	
 79	
 80	public static String guessInputType(Buffer buffer){
 81
 82		String input = buffer.getPath();
 83		String extension = null;
 84		if(input.length()>0){
 85			extension = input.substring(input.length() - 3);
 86		}
 87
 88		String inputType;
 89
 90		if(Arrays.asList(inputTypes).contains(extension))
 91		{
 92			inputType = extension;
 93		}
 94		else
 95		{
 96			inputType = buffer.getMode().getName();
 97			if(!Arrays.asList(inputTypes).contains(inputType))
 98			{
 99				inputType = null;
100			}
101		}
102		return inputType;
103	}
104	
105	public static void translateCurrentBuffer(View view, Buffer buffer, String outputType)
106	{
107		String inputType;
108		
109		String input = buffer.getPath();
110		
111		// infer the input type
112		inputType = guessInputType(buffer);
113		
114		// not guessed => ask the user
115		if(inputType == null)
116		{
117			int res = GUIUtilities.listConfirm(
118				  view
119				, "xml.translate.choose-input-type"
120				, new String[]{}
121				, inputTypes);
122			
123			if(res >=0 && res < inputTypes.length)
124			{
125				inputType = inputTypes[res];
126			}
127			else
128			{
129				return;
130			}
131		}
132		
133		List<String> inputs = Collections.singletonList(input);
134		
135		// infer the output
136		String output;
137		if(input.endsWith(inputType)){
138			output = input.replaceAll(inputType+"$",outputType);
139		}else{
140			output = input + "." + outputType;
141		}
142		
143		// translate
144		translate(view, inputType, inputs, Collections.<String>emptyList(), outputType, output, Collections.<String>emptyList());
145	}
146	
147	public static void translate(View view, String inputType, List<String> inputs, List<String> inputParams, String outputType, String outputFilename, List<String> outputParams)
148	{
149		if(inputs.isEmpty())throw new IllegalArgumentException("must provide at least one input");
150
151		String mainInput = inputs.get(0);
152		
153		if(errorSource == null)initErrorSource();
154		ErrorHandler eh = new xml.parser.ErrorListErrorHandler(errorSource,mainInput);
155		
156		if (inputType == null) {
157			inputType = MiscUtilities.getFileExtension(mainInput);
158			if (inputType.length() > 0)
159				inputType = inputType.substring(1);
160		}
161		
162		final InputFormat inputFormat;
163		if (inputType.equalsIgnoreCase("dtd")){
164			inputFormat = new BufferDtdInputFormat();
165		}else if(inputType.equalsIgnoreCase("rng")){
166			inputFormat = new BufferSAXParseInputFormat();
167		}else if(inputType.equalsIgnoreCase("rnc")){
168			inputFormat = new BufferCompactParseInputFormat();
169		}else{
170			inputFormat = Formats.createInputFormat(inputType);
171		}
172		
173		if (inputFormat == null) {
174			throw new IllegalArgumentException("unsupported input format : "+inputType);
175		}
176		
177		String ext = MiscUtilities.getFileExtension(outputFilename);
178		if (outputType == null) {
179			outputType = ext;
180			if (outputType.length() > 0)
181				outputType = outputType.substring(1);
182		}
183		final OutputFormat outputFormat = Formats.createOutputFormat(outputType);
184		if (outputFormat == null) {
185			throw new IllegalArgumentException("unsupported output format : "+outputType);
186		}
187		
188		Resolver resolver = new EntityResolverWrapper(xml.Resolver.instance(),true);
189		
190		String[] inputParamArray = inputParams.toArray(new String[inputParams.size()]);
191		outputType = outputType.toLowerCase();
192		SchemaCollection sc;
193		try
194		{
195			if (inputs.size() > 1) {
196				if (!(inputFormat instanceof MultiInputFormat)) {
197					throw new IllegalArgumentException("Only XML input type can handle multiple inputs !");
198				}
199				
200				String[] uris = new String[inputs.size()];
201				for (int i = 0; i < uris.length; i++)
202				{
203					uris[i] = xml.PathUtilities.pathToURL(inputs.get(i));
204				}
205				sc = ((MultiInputFormat)inputFormat).load(uris, inputParamArray, outputType, eh, resolver);
206			}
207			else
208			{
209				sc = inputFormat.load(xml.PathUtilities.pathToURL(mainInput), inputParamArray, outputType, eh, resolver);
210			}
211			
212			if (ext.length() == 0) ext = outputType;
213			
214			BuffersOutputDirectory od = new BuffersOutputDirectory(view, sc.getMainUri(), outputFilename);
215			outputFormat.output(sc, od, outputParams.toArray(new String[outputParams.size()]), inputType.toLowerCase(), eh);
216			
217			String done = jEdit.getProperty("xml.translate.done.message",new String[]{String.valueOf(od.getOutputCount())});
218			Log.log(Log.MESSAGE,TrangTranslator.class,done);
219			GUIUtilities.message(
220				  view
221				, "xml.translate.done"
222				, new String[]{String.valueOf(od.getOutputCount())});
223		}
224		catch (OutputFailedException e)
225		{
226			GUIUtilities.error(view,"xml.translate.failed",new String[]{e.toString()});
227			e.printStackTrace();
228		}
229		catch (InputFailedException e)
230		{
231			GUIUtilities.error(view,"xml.translate.failed",new String[]{e.toString()});
232			e.printStackTrace();
233		}
234		catch (InvalidParamsException e)
235		{
236			GUIUtilities.error(view,"xml.translate.failed",new String[]{e.toString()});
237			e.printStackTrace();
238		}
239		catch (IOException e)
240		{
241			GUIUtilities.error(view,"xml.translate.failed",new String[]{e.toString()});
242			e.printStackTrace();
243		}
244		catch (SAXException e)
245		{
246			GUIUtilities.error(view,"xml.translate.failed",new String[]{e.toString()});
247			e.printStackTrace();
248		}
249	}
250	
251}