Clover icon

Coverage Report

  1. Project Clover database Mon Nov 11 2024 15:14:12 GMT
  2. Package jalview.util

File ParseHtmlBodyAndLinks.java

 

Coverage histogram

../../img/srcFileCovDistChart9.png
13% of files have more coverage

Code metrics

20
42
7
1
183
101
22
0.52
6
7
3.14

Classes

Class Line # Actions
36 42 22
0.869565287%
 

Contributing tests

This file is covered by 96 tests. .

Source view

1    /*
2    * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3    * Copyright (C) $$Year-Rel$$ The Jalview Authors
4    *
5    * This file is part of Jalview.
6    *
7    * Jalview is free software: you can redistribute it and/or
8    * modify it under the terms of the GNU General Public License
9    * as published by the Free Software Foundation, either version 3
10    * of the License, or (at your option) any later version.
11    *
12    * Jalview is distributed in the hope that it will be useful, but
13    * WITHOUT ANY WARRANTY; without even the implied warranty
14    * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15    * PURPOSE. See the GNU General Public License for more details.
16    *
17    * You should have received a copy of the GNU General Public License
18    * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19    * The Jalview Authors are detailed in the 'AUTHORS' file.
20    */
21    package jalview.util;
22   
23    import java.util.Locale;
24   
25    import java.util.ArrayList;
26    import java.util.List;
27    import java.util.StringTokenizer;
28    import java.util.regex.Pattern;
29   
30    /**
31    * utility class for dealing with HTML link extraction
32    *
33    * @author jprocter
34    *
35    */
 
36    public class ParseHtmlBodyAndLinks
37    {
38    private static final Pattern LEFT_ANGLE_BRACKET_PATTERN = Pattern
39    .compile("<");
40   
41    String orig = null;
42   
 
43  0 toggle public String getOrig()
44    {
45  0 return orig;
46    }
47   
48    boolean htmlContent = true;
49   
50    /**
51    * @return true if the content looked like HTML
52    */
 
53  28142 toggle public boolean isHtmlContent()
54    {
55  28142 return htmlContent;
56    }
57   
58    List<String> links = new ArrayList<String>();
59   
60    String content;
61   
62    /**
63    * result of parsing description - with or without HTML tags
64    *
65    * @return
66    */
 
67  5 toggle public String getContent()
68    {
69   
70  5 return content;
71    }
72   
73    /**
74    * list of Label|Link encoded URL links extracted from HTML
75    *
76    * @return
77    */
 
78  547 toggle public List<String> getLinks()
79    {
80  547 return links;
81    }
82   
83    /**
84    * Parses the given html and
85    * <ul>
86    * <li>extracts any 'href' links to a list of "displayName|url" strings,
87    * retrievable by #getLinks</li>
88    * <li>extracts the remaining text (with %LINK% placeholders replacing hrefs),
89    * retrievable by #getContent</li>
90    * </ul>
91    *
92    * @param description
93    * - html or text content to be parsed
94    * @param removeHTML
95    * flag to indicate if HTML tags should be removed if they are
96    * present.
97    * @param newline
98    */
 
99  28356 toggle public ParseHtmlBodyAndLinks(String description, boolean removeHTML,
100    String newline)
101    {
102  28356 if (description == null || description.length() == 0)
103    {
104  0 htmlContent = false;
105  0 return;
106    }
107  28356 StringBuilder sb = new StringBuilder(description.length());
108  28356 if (description.toUpperCase(Locale.ROOT).indexOf("<HTML>") == -1)
109    {
110  28206 htmlContent = false;
111    }
112  28356 orig = description;
113  28356 StringTokenizer st = new StringTokenizer(description, "<");
114  28356 String token, link;
115  28356 int startTag;
116  28356 String tag = null;
117  57180 while (st.hasMoreElements())
118    {
119  28824 token = st.nextToken(">");
120  28824 if (token.equalsIgnoreCase("html") || token.startsWith("/"))
121    {
122  0 continue;
123    }
124   
125  28824 tag = null;
126  28824 startTag = token.indexOf("<");
127   
128  28824 if (startTag > -1)
129    {
130  604 tag = token.substring(startTag + 1);
131  604 token = token.substring(0, startTag);
132    }
133   
134  28824 if (tag != null && tag.toUpperCase(Locale.ROOT).startsWith("A HREF="))
135    {
136  140 if (token.length() > 0)
137    {
138  139 sb.append(token);
139    }
140  140 link = tag.substring(tag.indexOf("\"") + 1, tag.length() - 1);
141  140 String label = st.nextToken("<>");
142  140 links.add(label + "|" + link);
143  140 sb.append(label + "%LINK%");
144    }
145  28684 else if (tag != null && tag.equalsIgnoreCase("br"))
146    {
147  0 sb.append(newline);
148    }
149    else
150    {
151  28684 sb.append(token);
152    }
153    }
154  28356 if (removeHTML && !htmlContent)
155    {
156    // instead of parsing the html into plaintext
157    // clean the description ready for embedding in html
158  28023 sb = new StringBuilder(LEFT_ANGLE_BRACKET_PATTERN.matcher(description)
159    .replaceAll("&lt;"));
160    }
161  28356 content = translateEntities(sb.toString());
162    }
163   
 
164  28356 toggle private String translateEntities(String s)
165    {
166  28356 s = s.replaceAll("&amp;", "&");
167  28356 s = s.replaceAll("&lt;", "<");
168  28356 s = s.replaceAll("&gt;", ">");
169  28356 return s;
170    }
171   
172    /**
173    * get either the parsed content or the original, depending on whether the
174    * original looked like html content or not.
175    *
176    * @return
177    */
 
178  28142 toggle public String getNonHtmlContent()
179    {
180  28142 return isHtmlContent() ? content : orig;
181    }
182   
183    }