PageRenderTime 135ms CodeModel.GetById 118ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
Java | 184 lines | 124 code | 28 blank | 32 comment | 6 complexity | 72ac2489107c46d862ab5486a0e5dfbf MD5 | raw file
  1/*
  2* BuffersOutputDirectory.java - Trang output to jEdit buffers
  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*/
 15package xml.translate;
 16
 17
 18// {{{ Imports
 19import java.io.IOException;
 20import java.io.UnsupportedEncodingException;
 21import java.io.StringWriter;
 22import java.util.HashMap;
 23import java.util.Map;
 24
 25import org.gjt.sp.jedit.jEdit;
 26import org.gjt.sp.jedit.View;
 27import org.gjt.sp.jedit.Buffer;
 28import org.gjt.sp.jedit.Mode;
 29import org.gjt.sp.jedit.MiscUtilities;
 30import org.gjt.sp.jedit.syntax.ModeProvider;
 31import org.gjt.sp.util.Log;
 32import org.gjt.sp.jedit.io.VFSManager;
 33
 34import com.thaiopensource.relaxng.output.OutputDirectory;
 35import com.thaiopensource.xml.out.CharRepertoire;
 36// }}}
 37
 38/**
 39 * channels Trang output to jEdit buffers.
 40 * For each file Trang wants to write to, a jEdit buffer is opened.
 41 * FIXME: If the file is read-only, writing to it will fail.
 42 */
 43public class BuffersOutputDirectory implements OutputDirectory
 44{
 45	private View view;
 46	
 47	// maps URIs to filenames
 48	private final Map<String, String> uriMap;
 49	private final String mainInputExtension;
 50	private final String outputExtension;
 51	private final String outputDirectory;
 52	private int outputCount;
 53
 54	private int indent;
 55	private int lineLength;
 56	
 57	public BuffersOutputDirectory(View view, String mainInput, String mainOutput){
 58		this.view = view;
 59		this.uriMap = new HashMap<String, String>();
 60		
 61		this.mainInputExtension = MiscUtilities.getFileExtension(mainInput);
 62		this.outputDirectory = MiscUtilities.getParentOfPath(mainOutput);
 63		this.outputExtension = MiscUtilities.getFileExtension(mainOutput);
 64		
 65		uriMap.put(mainInput,MiscUtilities.getFileName(mainOutput));
 66		
 67		// init output properties
 68		Mode outputMode = ModeProvider.instance.getModeForFile(MiscUtilities.getFileName(mainOutput),"");
 69		if(outputMode == null)
 70		{
 71			outputMode = jEdit.getMode("text");
 72		}
 73		indent = ((Integer)outputMode.getProperty("indentSize")).intValue();
 74		lineLength = ((Integer)outputMode.getProperty("maxLineLen")).intValue();
 75	}
 76	
 77	public int getIndent()
 78	{
 79		Log.log(Log.DEBUG,BuffersOutputDirectory.class,"getIndent()");
 80		return indent;
 81	}
 82	
 83	public int getLineLength(){
 84		Log.log(Log.DEBUG,BuffersOutputDirectory.class,"getLineLength()");
 85		return lineLength;
 86	}
 87	
 88	public String getLineSeparator(){
 89		Log.log(Log.DEBUG,BuffersOutputDirectory.class,"getLineSeparator()");
 90		return "\n";
 91	}
 92	
 93	public OutputDirectory.Stream open(String sourceUri, String ignoredEncoding)
 94	throws UnsupportedEncodingException
 95	{
 96		Log.log(Log.DEBUG,BuffersOutputDirectory.class,"open("+sourceUri+","+ignoredEncoding+")");
 97
 98		String newSourceURI = MiscUtilities.constructPath(
 99									null
100									,outputDirectory
101									,mapFilename(sourceUri)
102			);
103		Log.log(Log.DEBUG,BuffersOutputDirectory.class,"===>"+newSourceURI);
104
105		Buffer b = jEdit.openFile(view, newSourceURI);
106		BufferWriter writer = new BufferWriter(b);
107		String encoding = (String)b.getProperty(Buffer.ENCODING);
108		CharRepertoire repertoire = CharRepertoire.getInstance(encoding);
109		
110		outputCount++;
111		
112		return new OutputDirectory.Stream(writer,encoding, repertoire);
113	}
114	
115	public void setEncoding(String encoding){
116		Log.log(Log.DEBUG,BuffersOutputDirectory.class,"setEncoding("+encoding+")");
117		// no-op
118	}
119	
120	public void setIndent(int indent){
121		Log.log(Log.DEBUG,BuffersOutputDirectory.class,"setIndent("+indent+")");
122		// no-op
123	}
124	
125	public String reference(String fromSourceUri, String toSourceUri) {
126		Log.log(Log.DEBUG,BuffersOutputDirectory.class,"reference("+fromSourceUri+","+toSourceUri+")");
127		Log.log(Log.DEBUG,BuffersOutputDirectory.class,"===>"+mapFilename(toSourceUri)+")");
128		return mapFilename(toSourceUri);
129	}
130	
131	public int getOutputCount(){
132		return outputCount;
133	}
134	
135	// {{{ private members
136	private String mapFilename(String sourceUri) {
137		String filename = uriMap.get(sourceUri);
138		if (filename == null) {
139			filename = chooseFilename(sourceUri);
140			uriMap.put(sourceUri, filename);
141		}
142		return filename;
143	}
144	
145	private String chooseFilename(String sourceUri) {
146		String filename = MiscUtilities.getFileName(sourceUri);
147		String base;
148		if (filename.endsWith(mainInputExtension))
149			base = filename.substring(0, filename.length() - mainInputExtension.length());
150		else
151			base = filename;
152		filename = base + outputExtension;
153		for (int i = 1; uriMap.containsValue(filename); i++)
154			filename = base + Integer.toString(i) + outputExtension;
155		return filename;
156	}
157	// }}}
158	
159	// {{{ BufferWriter class
160	/**
161	* writes to a Buffer upon close()
162	*/
163	public static class BufferWriter extends StringWriter
164	{
165		private Buffer targetBuffer;
166		
167		public BufferWriter(Buffer targetBuffer)
168		{
169			super();
170			this.targetBuffer = targetBuffer;
171		}
172		
173		public void close() throws IOException
174		{
175			super.close();
176			VFSManager.waitForRequests();
177			targetBuffer.beginCompoundEdit();
178			targetBuffer.remove(0,targetBuffer.getLength());
179			targetBuffer.insert(0,this.toString());
180			targetBuffer.endCompoundEdit();
181		}
182	}
183	// }}}
184}