Ticket #1958: html.2.patch

File html.2.patch, 4.0 KB (added by diana, 15 years ago)
  • src/main/java/org/sophie2/extra/func/html/HtmlTextImportManager.java

    ### Eclipse Workspace Patch 1.0
    #P org.sophie2.extra.func.html
     
    11package org.sophie2.extra.func.html; 
    22 
     3import java.io.BufferedReader; 
     4import java.io.ByteArrayInputStream; 
    35import java.io.File; 
     6import java.io.FileNotFoundException; 
    47import java.io.FileReader; 
    58import java.io.IOException; 
    6 import java.io.Reader; 
     9import java.io.InputStream; 
     10import java.util.regex.Matcher; 
     11import java.util.regex.Pattern; 
    712 
    813import javax.swing.JTextPane; 
    914import javax.swing.filechooser.FileFilter; 
     
    2833        /** 
    2934         * The file filter of the html resources. 
    3035         */ 
    31         public static final FileFilter FILE_FILTER =  
    32                 new FileExtensionFilter("HTML (*.html, *.htm)", "html", "htm"); 
    33          
     36        public static final FileFilter FILE_FILTER = new FileExtensionFilter( 
     37                        "HTML (*.html, *.htm)", "html", "htm"); 
     38 
    3439        /** 
    3540         * The import role of the <code>HtmlTextImportManager</code>. 
    3641         */ 
    3742        public static final String IMPORT_ROLE = "import html text"; 
    38          
     43 
    3944        @Override 
    4045        public String getImportRole() { 
    4146                return IMPORT_ROLE; 
    4247        } 
    43  
    44         @Override 
     48   private static final String splitBadTagStr = "<meta\\s*http-equiv" + 
     49                "=\\s*\"Content-Type\"\\s*content=\"text/html;"; 
     50        private static final String splitEndTagStr = ">"; 
     51   @Override 
    4552        public ImmHotText getResourceData(File resFile, ResourceH parentResource, 
    4653                        Object additionalData) { 
    47                 if (resFile == null || !(resFile.getName().endsWith("html") 
    48                                 || resFile.getName().endsWith("htm"))) { 
     54            
     55           BufferedReader input = null; 
     56                if (resFile == null 
     57                                || !(resFile.getName().endsWith("html") || resFile.getName() 
     58                                                .endsWith("htm"))) { 
    4959                        return null; 
    5060                } 
    5161                 
     62                StringBuilder fileContent = new StringBuilder(); 
     63                try { 
     64                         input =  new BufferedReader(new FileReader(resFile)); 
     65                        String readLine; 
     66                        while ((readLine = input.readLine()) != null) { 
     67                                        fileContent.append(readLine); 
     68                        } 
     69 
     70                } catch (FileNotFoundException e) { 
     71                        throw new RuntimeException("Could not import HTML file", e); 
     72                } catch (IOException e) { 
     73                        throw new RuntimeException("Could not import HTML file", e); 
     74                } 
     75                Pattern badTagPattern = Pattern.compile(splitBadTagStr,Pattern.CASE_INSENSITIVE); 
     76                Pattern endTagPattern = Pattern.compile(splitEndTagStr); 
     77                Matcher badTagMatcher = badTagPattern.matcher(fileContent); 
     78                Matcher endTagMatcher = endTagPattern.matcher(fileContent); 
     79                if(badTagMatcher.find()) { 
     80                        int start = badTagMatcher.start(); 
     81                        int end = badTagMatcher.end(); 
     82                        if(endTagMatcher.find(end)) { 
     83                        end = endTagMatcher.end(); 
     84                        } 
     85                        fileContent.replace(start, end, ""); 
     86                } 
     87                 
    5288                HTMLEditorKit kit = new HTMLEditorKit(); 
    5389                HTMLDocument doc = (HTMLDocument) kit.createDefaultDocument(); 
    54  
    5590                JTextPane pane = new JTextPane(); 
    5691                pane.setEditorKit(kit); 
    5792                pane.setDocument(doc); 
    5893                View view = pane.getUI().getRootView(pane); 
    59  
    60                 Reader reader = null; 
    61                 String htmlPlain; 
    6294                try { 
    63                         reader = new FileReader(resFile); 
    64                         kit.read(reader, doc, 0); 
    65                         reader.close(); 
     95                        InputStream is = new ByteArrayInputStream(fileContent.toString().getBytes()); 
     96                        kit.read(is, doc, 0); 
     97                        is.close(); 
    6698 
    67                         htmlPlain = doc.getText(0, doc.getLength()); 
    6899                } catch (BadLocationException e) { 
    69100                        throw new RuntimeException("Could not import HTML file", e); 
    70101                } catch (IOException e) { 
    71102                        throw new RuntimeException("Could not import HTML file", e); 
    72103                } 
    73104 
    74                 ImmHotText text = ImmHotText.createPlain(htmlPlain); 
    75  
    76                 ApplyHtmlStylesUtility.applyStyles(text, view); 
     105      ImmHotText text = null; 
    77106                 
     107                try { 
     108                        ApplyHtmlStylesUtility utility = new ApplyHtmlStylesUtility(); 
     109                        text = utility.createStyled(view); 
     110                } catch (BadLocationException e) { 
     111                        throw new RuntimeException(e); 
     112                } 
     113                 
    78114                return text; 
     115                 
    79116        } 
    80117 
    81118}