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
1 1 package org.sophie2.extra.func.html; 2 2 3 import java.io.BufferedReader; 4 import java.io.ByteArrayInputStream; 3 5 import java.io.File; 6 import java.io.FileNotFoundException; 4 7 import java.io.FileReader; 5 8 import java.io.IOException; 6 import java.io.Reader; 9 import java.io.InputStream; 10 import java.util.regex.Matcher; 11 import java.util.regex.Pattern; 7 12 8 13 import javax.swing.JTextPane; 9 14 import javax.swing.filechooser.FileFilter; … … 28 33 /** 29 34 * The file filter of the html resources. 30 35 */ 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 34 39 /** 35 40 * The import role of the <code>HtmlTextImportManager</code>. 36 41 */ 37 42 public static final String IMPORT_ROLE = "import html text"; 38 43 39 44 @Override 40 45 public String getImportRole() { 41 46 return IMPORT_ROLE; 42 47 } 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 45 52 public ImmHotText getResourceData(File resFile, ResourceH parentResource, 46 53 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"))) { 49 59 return null; 50 60 } 51 61 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 52 88 HTMLEditorKit kit = new HTMLEditorKit(); 53 89 HTMLDocument doc = (HTMLDocument) kit.createDefaultDocument(); 54 55 90 JTextPane pane = new JTextPane(); 56 91 pane.setEditorKit(kit); 57 92 pane.setDocument(doc); 58 93 View view = pane.getUI().getRootView(pane); 59 60 Reader reader = null;61 String htmlPlain;62 94 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(); 66 98 67 htmlPlain = doc.getText(0, doc.getLength());68 99 } catch (BadLocationException e) { 69 100 throw new RuntimeException("Could not import HTML file", e); 70 101 } catch (IOException e) { 71 102 throw new RuntimeException("Could not import HTML file", e); 72 103 } 73 104 74 ImmHotText text = ImmHotText.createPlain(htmlPlain); 75 76 ApplyHtmlStylesUtility.applyStyles(text, view); 105 ImmHotText text = null; 77 106 107 try { 108 ApplyHtmlStylesUtility utility = new ApplyHtmlStylesUtility(); 109 text = utility.createStyled(view); 110 } catch (BadLocationException e) { 111 throw new RuntimeException(e); 112 } 113 78 114 return text; 115 79 116 } 80 117 81 118 }