2 * This file is part of NixNote/NeighborNote
\r
3 * Copyright 2009 Randy Baumgarte
\r
5 * This file may be licensed under the terms of of the
\r
6 * GNU General Public License Version 2 (the ``GPL'').
\r
8 * Software distributed under the License is distributed
\r
9 * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
\r
10 * express or implied. See the GPL for the specific language
\r
11 * governing rights and limitations.
\r
13 * You should have received a copy of the GPL along with this
\r
14 * program. If not, go to http://www.gnu.org/licenses/gpl.html
\r
15 * or write to the Free Software Foundation, Inc.,
\r
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
\r
21 package cx.fbn.nevernote.xml;
\r
23 import com.evernote.edam.type.Resource;
\r
25 import cx.fbn.nevernote.Global;
\r
27 public class HtmlTagModifier {
\r
30 public HtmlTagModifier() {
\r
34 public HtmlTagModifier(String data) {
\r
35 html = new StringBuffer(data);
\r
38 public void setHtml(String data) {
\r
39 html = new StringBuffer(data);
\r
42 public String getHtml() {
\r
43 return html.toString();
\r
46 public void modifyLatexTagHash(Resource res) {
\r
49 for (; position<html.length();) {
\r
50 position = html.indexOf("<img", position);
\r
52 if (matchesGuid(position, res.getGuid())) {
\r
53 replaceValue(position, "height", new Integer(res.getHeight()).toString());
\r
54 replaceValue(position, "width", new Integer(res.getWidth()).toString());
\r
55 replaceValue(position, "hash", Global.byteArrayToHexString(res.getData().getBodyHash()));
\r
59 position = position+1;
\r
65 private boolean matchesGuid(int position, String guid) {
\r
66 int endPosition = html.indexOf(">", position);
\r
67 if (endPosition < 0)
\r
70 int guidPos = html.indexOf(guid, position);
\r
71 if (guidPos > endPosition)
\r
77 public void replaceValue(int position, String attribute, String newValue) {
\r
79 int endPosition = html.indexOf(">", position);
\r
80 if (endPosition < 0)
\r
83 int attributeStart = html.indexOf(attribute, position);
\r
84 if (attributeStart < 0 || attributeStart > endPosition)
\r
87 int attributeEnd = html.indexOf(" ", attributeStart);
\r
88 if (attributeEnd < 0 || endPosition < attributeEnd)
\r
89 attributeEnd = endPosition-1;
\r
91 attributeStart = attributeStart+2+attribute.length();
\r
92 html = html.delete(attributeStart, endPosition-1);
\r
93 html = html.insert(attributeStart, newValue);
\r