harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From telli...@apache.org
Subject svn commit: r572907 [5/5] - in /harmony/enhanced/classlib/branches/java6: depends/files/ modules/awt/src/main/java/common/java/awt/ modules/awt/src/main/java/common/java/awt/geom/ modules/awt/src/main/java/common/org/apache/harmony/awt/gl/ modules/awt/...
Date Wed, 05 Sep 2007 09:14:06 GMT
Modified: harmony/enhanced/classlib/branches/java6/modules/swing/src/main/java/common/javax/swing/JEditorPane.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/branches/java6/modules/swing/src/main/java/common/javax/swing/JEditorPane.java?rev=572907&r1=572906&r2=572907&view=diff
==============================================================================
--- harmony/enhanced/classlib/branches/java6/modules/swing/src/main/java/common/javax/swing/JEditorPane.java
(original)
+++ harmony/enhanced/classlib/branches/java6/modules/swing/src/main/java/common/javax/swing/JEditorPane.java
Wed Sep  5 02:14:01 2007
@@ -25,6 +25,7 @@
 import java.io.StringReader;
 import java.io.StringWriter;
 import java.net.URL;
+import java.net.URLConnection;
 import java.nio.charset.Charset;
 import java.util.Map;
 import java.util.Hashtable;
@@ -201,6 +202,8 @@
 
     private static final String RTF_CONTENT_TYPE = "text/rtf";
 
+    private static final String RTF2_CONTENT_TYPE = "application/rtf";
+
     private static final String REFERENCE_TAIL_PATTERN = "#.*";
 
     private static final String RTF_HEADER = "{\\rtf";
@@ -213,8 +216,6 @@
     private static Map<String, EditorKit> localContentTypes =
             new Hashtable<String, EditorKit>();
 
-    private String contentType = PLAIN_CONTENT_TYPE;
-
     private EditorKit editorKit;
 
     private URL currentPage;
@@ -230,6 +231,8 @@
                 new ContentTypeRegistration("javax.swing.text.html.HTMLEditorKit", null));
         contentTypes.put(RTF_CONTENT_TYPE,
                 new ContentTypeRegistration("javax.swing.text.rtf.RTFEditorKit", null));
+        contentTypes.put(RTF2_CONTENT_TYPE,
+                new ContentTypeRegistration("javax.swing.text.rtf.RTFEditorKit", null));
     }
 
     public static EditorKit createEditorKitForContentType(final String contentType) {
@@ -327,7 +330,7 @@
 
     @Override
     public AccessibleContext getAccessibleContext() {
-        if (HTML_CONTENT_TYPE.equals(contentType)) {
+        if (HTML_CONTENT_TYPE.equals(getContentType())) {
             if (accessibleHTML == null) {
                 accessibleHTML = new AccessibleJEditorPaneHTML();
             }
@@ -340,7 +343,7 @@
     }
 
     public final String getContentType() {
-        return contentType;
+        return ((editorKit != null) ? editorKit.getContentType() : null);
     }
 
     public EditorKit getEditorKit() {
@@ -417,57 +420,19 @@
         return false;
     }
 
-    private String getContentTypeByInputStream(final InputStream stream) {
-        int bufferSize = RTF_HEADER.length();
-        byte[] bytes = new byte[bufferSize];
-        String buffer = null;
-        try {
-            int status = stream.read(bytes, 0, 1);
-            int index = 0;
-            boolean notRtf = false;
-            while (status > 0) {
-                if (bytes[0] == '<') {
-                    notRtf = true;
-                    status = stream.read(bytes, 0, bufferSize);
-                    if (status < 0 || status < bufferSize) {
-                        break;
-                    }
-                    buffer = new String(bytes);
-                    if (("<" + buffer.toLowerCase()).startsWith(HTML_HEADER)) {
-                        return HTML_CONTENT_TYPE;
-                    }
-                } else {
-                    if (index < RTF_HEADER.length() && !notRtf) {
-                        if ((char) bytes[0] != RTF_HEADER.charAt(index)) {
-                            notRtf = true;
-                        } else {
-                            if (index++ == RTF_HEADER.length() - 1) {
-                                return RTF_CONTENT_TYPE;
-                            }
-                        }
-                    }
-                    status = stream.read(bytes, 0, 1);
-                }
-            }
-        } catch (IOException e) {
-        }
-        return PLAIN_CONTENT_TYPE;
-    }
-
     private String getBaseURL(final String url) {
         return (url == null) ? null : url.replaceAll(REFERENCE_TAIL_PATTERN, "");
     }
 
     protected InputStream getStream(final URL url) throws IOException {
-        InputStream inputStream = url.openStream();
         if (url.getProtocol() == "http") {
             getDocument().putProperty(Document.StreamDescriptionProperty,
                     getBaseURL(url.toString()));
         }
-        setContentType(getContentTypeByInputStream(inputStream));
-        //Perhaps, it is not best solution. I'm going to think about this one
-        inputStream.close();
-        return url.openStream();
+        URLConnection connection = url.openConnection();
+        String contentType = connection.getContentType();
+        setContentType((contentType != null) ? contentType : PLAIN_CONTENT_TYPE);
+        return connection.getInputStream();
     }
 
     @Override
@@ -487,9 +452,9 @@
 
     @Override
     protected String paramString() {
-        return super.paramString() + "," + "contentType=" + contentType + "," + "editorKit="
-                + editorKit + "," + "document=" + getDocument() + "," + "currentPage="
-                + currentPage;
+        return (super.paramString() + "," + "contentType=" + getContentType() + ","
+                + "editorKit=" + editorKit + "," + "document=" + getDocument() + ","
+                + "currentPage=" + currentPage);
     }
 
     public void read(final InputStream stream, final Object type) throws IOException {
@@ -566,37 +531,26 @@
                  || (PLAIN_CONTENT_TYPE.equals(contentType) && editorKit instanceof
PlainEditorKit));
     }
 
-    public final void setContentType(final String type) {
+    public final void setContentType(String type) {
         if (type == null) {
             throw new NullPointerException(Messages.getString("swing.03","Content type"));
//$NON-NLS-1$ //$NON-NLS-2$
         }
-        contentType = (contentTypes.containsKey(type) ? type : PLAIN_CONTENT_TYPE);
+        int comma = type.indexOf(';');
 
-        if (changeEditorKit(contentType)) {
-            EditorKit kit = getEditorKitForContentType(contentType);
-            updateEditorKit((kit != null) ? kit : new PlainEditorKit());
-            updateDocument(editorKit);
-        } 
-    }
-
-    private String getContentTypeByEditorKit(final EditorKit kit) {
-        if (kit == null) {
-            return PLAIN_CONTENT_TYPE;
+        if (comma >= 0) {
+            type = type.substring(0, comma);
         }
+        type = type.trim().toLowerCase();
 
-        for (Map.Entry<String, EditorKit> entry : localContentTypes.entrySet()) {
-             if (kit.equals(entry.getValue())) {
-                 return entry.getKey();
-             }
+        if (!contentTypes.containsKey(type)) {
+            type = PLAIN_CONTENT_TYPE;
         }
 
-        for (Map.Entry<String, ContentTypeRegistration> entry : contentTypes.entrySet())
{
-             if (kit.getClass().getName().equals(entry.getValue().className)) {
-                 return entry.getKey();
-             }
+        if (changeEditorKit(type)) {
+            EditorKit kit = getEditorKitForContentType(type);
+            updateEditorKit((kit != null) ? kit : new PlainEditorKit());
+            updateDocument(editorKit);
         }
-
-        return PLAIN_CONTENT_TYPE;
     }
 
     private void updateEditorKit(final EditorKit kit) {
@@ -618,10 +572,8 @@
     }
 
     public void setEditorKit(final EditorKit kit) {
-        String newContentType = getContentTypeByEditorKit(kit);
         updateEditorKit(kit);
         updateDocument(kit);
-        contentType = newContentType;
     }
 
     public void setEditorKitForContentType(final String type, final EditorKit kit) {
@@ -769,7 +721,7 @@
         StringReader reader = new StringReader(content == null ? "" : content);
 
         try {
-            read(reader, contentType);
+            read(reader, getContentType());
         } catch (IOException e) {
         }
     }

Modified: harmony/enhanced/classlib/branches/java6/modules/swing/src/main/java/common/javax/swing/plaf/basic/BasicSliderUI.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/branches/java6/modules/swing/src/main/java/common/javax/swing/plaf/basic/BasicSliderUI.java?rev=572907&r1=572906&r2=572907&view=diff
==============================================================================
--- harmony/enhanced/classlib/branches/java6/modules/swing/src/main/java/common/javax/swing/plaf/basic/BasicSliderUI.java
(original)
+++ harmony/enhanced/classlib/branches/java6/modules/swing/src/main/java/common/javax/swing/plaf/basic/BasicSliderUI.java
Wed Sep  5 02:14:01 2007
@@ -51,6 +51,7 @@
 import javax.swing.plaf.ComponentUI;
 import javax.swing.plaf.SliderUI;
 
+import org.apache.harmony.luni.util.NotImplementedException;
 import org.apache.harmony.x.swing.StringConstants;
 import org.apache.harmony.x.swing.Utilities;
 
@@ -63,18 +64,28 @@
  */
 public class BasicSliderUI extends SliderUI {
 
+    /**
+     * This class isn't used since 1.3 and not implemented here (see
+     * HARMONY-4523 for details - the methods doesn't throw new
+     * NotImplementedException according to backward compatibility)
+     */
+    @SuppressWarnings("unused")
     public class ActionScroller extends AbstractAction {
         private static final long serialVersionUID = -3454576988589353120L;
 
-        public ActionScroller(final JSlider slider, final int dir, final boolean block) {
+        public ActionScroller(JSlider slider, int dir, boolean block)
+                throws NotImplementedException {
+            // Not implemented.
         }
 
-        public void actionPerformed(final ActionEvent e) {
+        public void actionPerformed(ActionEvent e)
+                throws NotImplementedException {
+            throw new NotImplementedException();
         }
 
         @Override
         public boolean isEnabled() {
-            return true;
+            return enabled;
         }
     }
 

Modified: harmony/enhanced/classlib/branches/java6/modules/swing/src/test/api/java.injected/javax/swing/JEditorPaneTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/branches/java6/modules/swing/src/test/api/java.injected/javax/swing/JEditorPaneTest.java?rev=572907&r1=572906&r2=572907&view=diff
==============================================================================
--- harmony/enhanced/classlib/branches/java6/modules/swing/src/test/api/java.injected/javax/swing/JEditorPaneTest.java
(original)
+++ harmony/enhanced/classlib/branches/java6/modules/swing/src/test/api/java.injected/javax/swing/JEditorPaneTest.java
Wed Sep  5 02:14:01 2007
@@ -724,6 +724,35 @@
         }
     }
 
+    static void checkContentType(String fileName, String expected) throws IOException {
+        if (!fileName.startsWith("http")) {
+            fileName = "content-type/" + fileName;
+            URL url = JEditorPaneTest.class.getResource(fileName);
+            assertNotNull("Resource not found: " + fileName, url);
+            fileName = url.toString();
+        }
+        JEditorPane pane = new JEditorPane(fileName);
+        assertEquals(pane.getContentType(), expected);
+        assertEquals(pane.getEditorKit().getContentType(), expected);
+    }
+
+    public void testGetContentType() throws IOException {
+        // Regression for HARMONY-4696
+        checkContentType("txt", "text/plain");
+        checkContentType("html", "text/html");
+        checkContentType("rtf", "text/plain");
+        checkContentType("txt.txt", "text/plain");
+        checkContentType("txt.html", "text/html");
+        checkContentType("txt.rtf", "text/plain");  // Change to "text/rtf" when RTFEditorKit
becomes available.
+        checkContentType("html.txt", "text/plain");
+        checkContentType("html.html", "text/html");
+        checkContentType("html.rtf", "text/plain"); // Change to "text/rtf" when RTFEditorKit
becomes available.
+        checkContentType("rtf.txt", "text/plain");
+        checkContentType("rtf.html", "text/html");
+        checkContentType("rtf.rtf", "text/plain");  // Change to "text/rtf" when RTFEditorKit
becomes available.
+        checkContentType("http://www.apache.org", "text/html");
+    }
+
     private String getClassName(final Object obj) {
         assertNotNull(obj);
         return obj.getClass().getName();



Mime
View raw message