Index: JspDataObject.java =================================================================== RCS file: /cvs/web/core/src/org/netbeans/modules/web/core/jsploader/JspDataObject.java,v retrieving revision 1.25 retrieving revision 1.30 diff -u -r1.25 -r1.30 --- JspDataObject.java 22 May 2003 11:16:15 -0000 1.25 +++ JspDataObject.java 30 Jun 2003 07:36:47 -0000 1.30 @@ -19,10 +19,14 @@ 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,7 +75,6 @@ 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 @@ -683,18 +686,6 @@ 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. **/ @@ -717,8 +708,17 @@ public static String getDefaultEncoding() { - String language = System.getProperty("user.language"); - if ("ja".equals(language)) { // NOI18N + 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 @@ -727,17 +727,47 @@ } 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"); + return "ISO-8859-1"; // NOI18N*/ } - -/* public void setEncoding(String encoding) throws IOException { - getPrimaryFile ().setAttribute (EA_ENCODING, encoding); - firePropertyChange(PROP_ENCODING, null, encoding); - } */ + 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")) { + return CORRECT_EUC_JP; + } + // chinese encodings that must be adjusted + if (encodingAlias.equalsIgnoreCase("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(); @@ -1018,6 +1048,58 @@ 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 --------- *****CVS exited normally with code 1*****