Index: JspDataObject.java =================================================================== RCS file: /cvs/web/core/src/org/netbeans/modules/web/core/jsploader/JspDataObject.java,v retrieving revision 1.22 diff -c -r1.22 JspDataObject.java *** JspDataObject.java 27 Feb 2003 23:44:18 -0000 1.22 --- JspDataObject.java 10 Oct 2003 12:02:27 -0000 *************** *** 19,28 **** --- 19,32 ---- import java.util.Enumeration; import java.util.HashMap; import java.util.Date; + import java.util.Locale; import java.beans.PropertyChangeListener; import java.beans.PropertyChangeEvent; import java.io.IOException; import java.io.File; + import java.nio.charset.Charset; + import javax.swing.text.BadLocationException; + import javax.swing.text.StyledDocument; import org.openide.*; import org.openide.cookies.EditorCookie; *************** *** 71,77 **** public static final String EA_CONTENT_LANGUAGE = "AttrJSPContentLanguage"; // NOI18N public static final String EA_SCRIPTING_LANGUAGE = "AttrJSPScriptingLanguage"; // NOI18N - // public static final String EA_ENCODING = "AttrEncoding"; // NOI18N public static final String EA_JSP_ERRORPAGE = "jsp_errorpage"; // NOI18N // property for the servlet dataobject corresponding to this page public static final String PROP_SERVLET_DATAOBJECT = "servlet_do"; // NOI18N --- 75,80 ---- *************** *** 673,690 **** firePropertyChange(PROP_SCRIPTING_LANGUAGE, null, scriptingLanguage); } - /* public String getEncoding() { - try { - String encoding = (String)getPrimaryFile ().getAttribute (EA_ENCODING); - if (encoding != null) { - return encoding; - } - } catch (Exception ex) { - // null pointer or IOException - } - return getDefaultEncoding(); - }*/ - /** Gets the raw encoding from the fileobject, * ensures beckward compatibility. **/ --- 676,681 ---- *************** *** 707,714 **** public static String getDefaultEncoding() { ! String language = System.getProperty("user.language"); ! if ("ja".equals(language)) { // NOI18N // we are Japanese if (org.openide.util.Utilities.isUnix()) return "EUC-JP"; // NOI18N --- 698,714 ---- public static String getDefaultEncoding() { ! String language = Locale.getDefault().getLanguage(); ! if (language.startsWith("en")) { ! // we are English ! return "ISO-8859-1"; // NOI18N ! // per JSP 1.2 specification, the default encoding is always ISO-8859-1, ! // regardless of the setting of the file.encoding property ! //return System.getProperty("file.encoding", "ISO-8859-1"); ! } ! return canonizeEncoding(System.getProperty("file.encoding", "ISO-8859-1")); ! ! /* if ("ja".equals(language)) { // NOI18N // we are Japanese if (org.openide.util.Utilities.isUnix()) return "EUC-JP"; // NOI18N *************** *** 717,733 **** } else // we are English ! return "ISO-8859-1"; // NOI18N ! // per JSP 1.2 specification, the default encoding is always ISO-8859-1, ! // regardless of the setting of the file.encoding property ! //return System.getProperty("file.encoding", "ISO-8859-1"); } - - /* public void setEncoding(String encoding) throws IOException { - getPrimaryFile ().setAttribute (EA_ENCODING, encoding); - firePropertyChange(PROP_ENCODING, null, encoding); - } */ private void printJob(CompilerJob job) { /* System.out.println("-- compilers --"); // NOI18N java.util.Iterator compilers = job.compilers().iterator(); --- 717,766 ---- } else // we are English ! return "ISO-8859-1"; // NOI18N*/ } + private static final String CORRECT_WINDOWS_31J = "windows-31j"; + private static final String CORRECT_EUC_JP = "EUC-JP"; + private static final String CORRECT_GB2312 = "GB2312"; + private static final String CORRECT_BIG5 = "BIG5"; + + private static String canonizeEncoding(String encodingAlias) { + + // canonic name first + if (Charset.isSupported(encodingAlias)) { + Charset cs = Charset.forName(encodingAlias); + encodingAlias = cs.name(); + } + + // this is not supported on JDK 1.4.1 + if (encodingAlias.equalsIgnoreCase("MS932")) { + return CORRECT_WINDOWS_31J; + } + // this is not a correct charset by http://www.iana.org/assignments/character-sets + if (encodingAlias.equalsIgnoreCase("euc-jp-linux") || + encodingAlias.equalsIgnoreCase("x-euc-jp-linux") || + encodingAlias.equalsIgnoreCase("eucJP-open")) { + return CORRECT_EUC_JP; + } + // chinese encodings that must be adjusted + if (encodingAlias.equalsIgnoreCase("EUC-CN") || + encodingAlias.equalsIgnoreCase("x-EUC-CN")) { + return CORRECT_GB2312; + } + if (encodingAlias.equalsIgnoreCase("GBK")) { + return CORRECT_GB2312; + } + if (encodingAlias.equalsIgnoreCase("GB18030")) { + return CORRECT_GB2312; + } + if (encodingAlias.equalsIgnoreCase("EUC-TW")) { + return CORRECT_BIG5; + } + + return encodingAlias; + } + private void printJob(CompilerJob job) { /* System.out.println("-- compilers --"); // NOI18N java.util.Iterator compilers = job.compilers().iterator(); *************** *** 1008,1013 **** --- 1041,1098 ---- public void removeSaveCookie(){ Node.Cookie cookie = getCookie(SaveCookie.class); if (cookie!=null) getCookieSet().remove(cookie); + } + + /* Creates new object from template. Inserts the correct ";charset=..." clause to the object + * @exception IOException + */ + protected DataObject handleCreateFromTemplate ( + DataFolder df, String name + ) throws IOException { + + DataObject dobj = super.handleCreateFromTemplate(df, name); + if (dobj instanceof JspDataObject) { + JspDataObject jspDO = (JspDataObject)dobj; + FileObject prim = jspDO.getPrimaryFile(); + String encoding = jspDO.getFileEncoding(prim); + if (!"ISO-8859-1".equals(encoding)) { + // write the encoding to file + sun.io.CharToByteConverter.getConverter(encoding); + Util.setFileEncoding(prim, encoding); + + // change in the file + // warning: the following approach will only work if the page does not + // contain any strange characters. That's the case right after creation + // from template, so we are safe here + EditorCookie edit = (EditorCookie)jspDO.getCookie(EditorCookie.class); + if (edit != null) { + try { + StyledDocument doc = edit.openDocument(); + int offset = findEncodingOffset(doc); + if (offset != -1) { + doc.insertString(offset, ";charset=" + encoding, null); + SaveCookie sc = (SaveCookie)jspDO.getCookie(SaveCookie.class); + if (sc != null) { + sc.save(); + } + } + } + catch (BadLocationException e) { + ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); + } + } + } + } + return dobj; + } + + private int findEncodingOffset(StyledDocument doc) throws BadLocationException { + String text = doc.getText(0, doc.getLength()); + String toFind = "contentType=\""; + int index1 = text.indexOf(toFind); + if (index1 == -1) return -1; + int index2 = text.indexOf("\"", index1 + toFind.length()); + return index2; } ////// -------- INNER CLASSES ---------