~ubuntu-branches/ubuntu/natty/jabref/natty

« back to all changes in this revision

Viewing changes to src/java/net/sf/jabref/imports/HTMLConverter.java

  • Committer: Bazaar Package Importer
  • Author(s): gregor herrmann
  • Date: 2010-04-27 16:49:34 UTC
  • mfrom: (2.1.8 squeeze)
  • Revision ID: james.westby@ubuntu.com-20100427164934-ozu2dvinslmo3444
Tags: 2.6+ds-2
debian/control: add "Recommends: xdg-utils"; thanks to Vincent Fourmond
for the bug report (closes: #579346). Change xpdf to xpdf-reader in
Suggests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
package net.sf.jabref.imports;
2
2
 
 
3
import java.util.HashMap;
 
4
import java.util.Set;
 
5
import java.util.regex.Matcher;
 
6
import java.util.regex.Pattern;
 
7
 
3
8
import net.sf.jabref.export.layout.LayoutFormatter;
4
9
 
5
10
/**
10
15
 * To change this template use File | Settings | File Templates.
11
16
 */
12
17
public class HTMLConverter implements LayoutFormatter {
13
 
 
 
18
        private HashMap<String, String> escapedSymbols = new HashMap<String, String>();
 
19
        
 
20
        public HTMLConverter() {
 
21
                super();
 
22
                escapedSymbols.put("&ldquo;", "``");
 
23
                escapedSymbols.put("&rdquo;", "''");
 
24
                escapedSymbols.put("&lsquo;", "``");
 
25
                escapedSymbols.put("&rsquo;", "''");
 
26
                escapedSymbols.put("&nbsp;", " ");
 
27
                escapedSymbols.put("&quot;", "\"");
 
28
                escapedSymbols.put("&amp;", "&");
 
29
                escapedSymbols.put("&lt;", "<");
 
30
                escapedSymbols.put("&gt;", ">");
 
31
        }
14
32
    public String format(String text) {
15
 
 
16
33
        if (text == null)
17
34
            return null;
18
 
        text = text.replaceAll("&ldquo;", "``");
19
 
        text = text.replaceAll("&rdquo;", "''");
20
 
        text = text.replaceAll("&lsquo;", "`");
21
 
        text = text.replaceAll("&rsquo;", "'");
22
35
        StringBuffer sb = new StringBuffer();
23
36
        for (int i=0; i<text.length(); i++) {
24
37
 
25
38
            int c = text.charAt(i);
26
39
 
27
 
            if (c == '&') {
28
 
                i = readHtmlChar(text, sb, i);
29
 
                //sb.append((char)c);
30
 
            } else if (c == '<') {
 
40
            if (c == '<') {
31
41
                i = readTag(text, sb, i);
32
42
            } else
33
43
                sb.append((char)c);
34
44
 
35
45
        }
 
46
        text = sb.toString();
 
47
        Set<String> patterns = escapedSymbols.keySet();
 
48
        for (String pattern: patterns) {
 
49
                text = text.replaceAll(pattern, escapedSymbols.get(pattern));
 
50
        }
 
51
        
 
52
        Pattern escapedPattern = Pattern.compile("&#([x]*\\d+);");
 
53
        Matcher m = escapedPattern.matcher(text);
 
54
        while (m.find()) {
 
55
                int num = Integer.decode(m.group(1).replace("x", "#"));
 
56
                switch (num) {
 
57
                case 37:
 
58
                        text = text.replaceAll("&#" + m.group(1) + ";", "%");
 
59
                        break;
 
60
                case 38:
 
61
                        text = text.replaceAll("&#" + m.group(1) + ";", "&");
 
62
                        break;
 
63
                case 916:
 
64
                        text = text.replaceAll("&#" + m.group(1) + ";", "$\\delta$");
 
65
                        break;
 
66
                case 956:
 
67
                        text = text.replaceAll("&#" + m.group(1) + ";", "$\\mu$");
 
68
                        break;
 
69
                case 8208:
 
70
                        text = text.replaceAll("&#" + m.group(1) + ";", "-");
 
71
                        break;
 
72
                case 8211:
 
73
                        text = text.replaceAll("&#" + m.group(1) + ";", "--");
 
74
                        break;
 
75
                case 8212:
 
76
                        text = text.replaceAll("&#" + m.group(1) + ";", "---");
 
77
                        break;
 
78
                case 8217:
 
79
                        text = text.replaceAll("&#" + m.group(1) + ";", "'");
 
80
                        break;
 
81
                default:
 
82
                        System.err.println("HTML escaped char not converted " + m.group(1) + ": " + Integer.toString(num));
 
83
                }
 
84
        }
36
85
 
37
 
        return sb.toString();
 
86
        return text.trim();
38
87
    }
39
88
 
40
 
    private final int MAX_TAG_LENGTH = 20;
41
 
    private final int MAX_CHAR_LENGTH = 10;
 
89
    private final int MAX_TAG_LENGTH = 30;
 
90
    /*private final int MAX_CHAR_LENGTH = 10;
42
91
 
43
92
    private int readHtmlChar(String text, StringBuffer sb, int position) {
44
93
        // Have just read the < character that starts the tag.
48
97
            //System.out.println("Removed code: "+text.substring(position, index));
49
98
            return index; // Just skip the tag.
50
99
        } else return position; // Don't do anything.
51
 
    }
 
100
    }*/
52
101
 
53
102
    private int readTag(String text, StringBuffer sb, int position) {
54
103
        // Have just read the < character that starts the tag.