2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
20 import java.util.ArrayList;
21 import java.util.StringTokenizer;
22 import java.util.regex.Pattern;
25 * utility class for dealing with HTML link extraction
30 public class ParseHtmlBodyAndLinks
34 public String getOrig()
39 boolean htmlContent = true;
42 * @return true if the content looked like HTML
44 public boolean isHtmlContent()
49 ArrayList<String> links = new ArrayList<String>();
51 StringBuffer sb = new StringBuffer();
54 * result of parsing description - with or without HTML tags
58 public String getContent()
65 * list of Label|Link encoded URL links extracted from HTML
69 public ArrayList<String> getLinks()
77 * - html or text content to be parsed
79 * flag to indicate if HTML tags should be removed if they are
83 public ParseHtmlBodyAndLinks(String description, boolean removeHTML,
86 if (description == null || description.length() == 0)
91 if (description.toUpperCase().indexOf("<HTML>") == -1)
96 StringTokenizer st = new StringTokenizer(description, "<");
100 while (st.hasMoreElements())
102 token = st.nextToken("&>");
103 if (token.equalsIgnoreCase("html") || token.startsWith("/"))
109 startTag = token.indexOf("<");
113 tag = token.substring(startTag + 1);
114 token = token.substring(0, startTag);
117 if (tag != null && tag.toUpperCase().startsWith("A HREF="))
119 if (token.length() > 0)
123 link = tag.substring(tag.indexOf("\"") + 1, tag.length() - 1);
124 String label = st.nextToken("<>");
125 links.add(label + "|" + link);
126 sb.append(label + "%LINK%");
128 else if (tag != null && tag.equalsIgnoreCase("br"))
132 else if (token.startsWith("lt;"))
134 sb.append("<" + token.substring(3));
136 else if (token.startsWith("gt;"))
138 sb.append(">" + token.substring(3));
140 else if (token.startsWith("amp;"))
142 sb.append("&" + token.substring(4));
149 if (removeHTML && !htmlContent)
151 // instead of parsing the html into plaintext
152 // clean the description ready for embedding in html
153 sb = new StringBuffer(Pattern.compile("<").matcher(description)
154 .replaceAll("<"));
161 * get either the parsed content or the original, depending on whether the
162 * original looked like html content or not.
166 public String getNonHtmlContent()
168 return isHtmlContent() ? sb.toString() : orig;