OSDN Git Service

9a4eeeb737efc7816a992620ad9f5e20bf4e35d6
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / xml / HtmlTagModifier.java
1 /*\r
2  * This file is part of NixNote \r
3  * Copyright 2009 Randy Baumgarte\r
4  * \r
5  * This file may be licensed under the terms of of the\r
6  * GNU General Public License Version 2 (the ``GPL'').\r
7  *\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
12  *\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
17  *\r
18 */\r
19 \r
20 \r
21 package cx.fbn.nevernote.xml;\r
22 \r
23 import com.evernote.edam.type.Resource;\r
24 \r
25 import cx.fbn.nevernote.Global;\r
26 \r
27 public class HtmlTagModifier {\r
28         StringBuffer html;\r
29 \r
30         public HtmlTagModifier() {\r
31                 html = null;\r
32         }\r
33         \r
34         public HtmlTagModifier(String data) {\r
35                 html = new StringBuffer(data);\r
36         }\r
37         \r
38         public void setHtml(String data) {\r
39                 html = new StringBuffer(data);\r
40         }\r
41         \r
42         public String getHtml() {\r
43                 return html.toString();\r
44         }\r
45         \r
46         public void modifyLatexTagHash(Resource res) {\r
47                 \r
48                 int position = 0;\r
49                 for (; position<html.length();) {\r
50                         position = html.indexOf("<img", position);\r
51                         if (position > 0) {\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
56                                         return;\r
57                                 } \r
58                         }\r
59                         position = position+1;\r
60                 }\r
61                 return;\r
62         }\r
63         \r
64         \r
65         private boolean matchesGuid(int position, String guid) {\r
66                 int endPosition = html.indexOf(">", position);\r
67                 if (endPosition < 0)\r
68                         return false;\r
69                 \r
70                 int guidPos = html.indexOf(guid, position);\r
71                 if (guidPos > endPosition) \r
72                         return false;\r
73                 else\r
74                         return true;\r
75         }\r
76         \r
77         public void replaceValue(int position, String attribute, String newValue) {\r
78                 int endPosition = html.indexOf(">", position);\r
79                 if (endPosition < 0)\r
80                         return;\r
81                 \r
82                 int attributeStart = html.indexOf(attribute, position);\r
83                 if (attributeStart < 0)\r
84                         return;\r
85                 \r
86                 int attributeEnd = html.indexOf(" ", attributeStart);\r
87                 if (attributeEnd < 0 || endPosition < attributeEnd) \r
88                         attributeEnd = endPosition-1;\r
89                                 \r
90                 attributeStart = attributeStart+2+attribute.length();\r
91                 html = html.delete(attributeStart, endPosition-1);\r
92                 html = html.insert(attributeStart, newValue);\r
93                 \r
94         }\r
95 }\r