2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.StringTokenizer;
26 import java.util.regex.Pattern;
29 * utility class for dealing with HTML link extraction
34 public class ParseHtmlBodyAndLinks
36 private static final Pattern LEFT_ANGLE_BRACKET_PATTERN = Pattern
41 public String getOrig()
46 boolean htmlContent = true;
49 * @return true if the content looked like HTML
51 public boolean isHtmlContent()
56 List<String> links = new ArrayList<String>();
61 * result of parsing description - with or without HTML tags
65 public String getContent()
72 * list of Label|Link encoded URL links extracted from HTML
76 public List<String> getLinks()
82 * Parses the given html and
84 * <li>extracts any 'href' links to a list of "displayName|url" strings,
85 * retrievable by #getLinks</li>
86 * <li>extracts the remaining text (with %LINK% placeholders replacing hrefs),
87 * retrievable by #getContent</li>
91 * - html or text content to be parsed
93 * flag to indicate if HTML tags should be removed if they are
97 public ParseHtmlBodyAndLinks(String description, boolean removeHTML,
100 if (description == null || description.length() == 0)
105 StringBuilder sb = new StringBuilder(description.length());
106 if (description.toUpperCase().indexOf("<HTML>") == -1)
111 StringTokenizer st = new StringTokenizer(description, "<");
115 while (st.hasMoreElements())
117 token = st.nextToken(">");
118 if (token.equalsIgnoreCase("html") || token.startsWith("/"))
124 startTag = token.indexOf("<");
128 tag = token.substring(startTag + 1);
129 token = token.substring(0, startTag);
132 if (tag != null && tag.toUpperCase().startsWith("A HREF="))
134 if (token.length() > 0)
138 link = tag.substring(tag.indexOf("\"") + 1, tag.length() - 1);
139 String label = st.nextToken("<>");
140 links.add(label + "|" + link);
141 sb.append(label + "%LINK%");
143 else if (tag != null && tag.equalsIgnoreCase("br"))
152 if (removeHTML && !htmlContent)
154 // instead of parsing the html into plaintext
155 // clean the description ready for embedding in html
156 sb = new StringBuilder(LEFT_ANGLE_BRACKET_PATTERN
157 .matcher(description).replaceAll("<"));
159 content = translateEntities(sb.toString());
162 private String translateEntities(String s)
164 s = s.replaceAll("&", "&");
165 s = s.replaceAll("<", "<");
166 s = s.replaceAll(">", ">");
171 * get either the parsed content or the original, depending on whether the
172 * original looked like html content or not.
176 public String getNonHtmlContent()
178 return isHtmlContent() ? content : orig;