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.StringTokenizer;
25 import java.util.regex.Pattern;
28 * utility class for dealing with HTML link extraction
33 public class ParseHtmlBodyAndLinks
35 private static final Pattern LEFT_ANGLE_BRACKET_PATTERN = Pattern
40 public String getOrig()
45 boolean htmlContent = true;
48 * @return true if the content looked like HTML
50 public boolean isHtmlContent()
55 ArrayList<String> links = new ArrayList<String>();
57 StringBuffer sb = new StringBuffer();
60 * result of parsing description - with or without HTML tags
64 public String getContent()
71 * list of Label|Link encoded URL links extracted from HTML
75 public ArrayList<String> getLinks()
83 * - html or text content to be parsed
85 * flag to indicate if HTML tags should be removed if they are
89 public ParseHtmlBodyAndLinks(String description, boolean removeHTML,
92 if (description == null || description.length() == 0)
97 if (description.toUpperCase().indexOf("<HTML>") == -1)
102 StringTokenizer st = new StringTokenizer(description, "<");
106 while (st.hasMoreElements())
108 token = st.nextToken("&>");
109 if (token.equalsIgnoreCase("html") || token.startsWith("/"))
115 startTag = token.indexOf("<");
119 tag = token.substring(startTag + 1);
120 token = token.substring(0, startTag);
123 if (tag != null && tag.toUpperCase().startsWith("A HREF="))
125 if (token.length() > 0)
129 link = tag.substring(tag.indexOf("\"") + 1, tag.length() - 1);
130 String label = st.nextToken("<>");
131 links.add(label + "|" + link);
132 sb.append(label + "%LINK%");
134 else if (tag != null && tag.equalsIgnoreCase("br"))
138 else if (token.startsWith("lt;"))
140 sb.append("<" + token.substring(3));
142 else if (token.startsWith("gt;"))
144 sb.append(">" + token.substring(3));
146 else if (token.startsWith("amp;"))
148 sb.append("&" + token.substring(4));
155 if (removeHTML && !htmlContent)
157 // instead of parsing the html into plaintext
158 // clean the description ready for embedding in html
159 sb = new StringBuffer(LEFT_ANGLE_BRACKET_PATTERN.matcher(description)
160 .replaceAll("<"));
167 * get either the parsed content or the original, depending on whether the
168 * original looked like html content or not.
172 public String getNonHtmlContent()
174 return isHtmlContent() ? sb.toString() : orig;