2 * This file is part of NeverNote
3 * Copyright 2009 Randy Baumgarte
5 * This file may be licensed under the terms of of the
6 * GNU General Public License Version 2 (the ``GPL'').
8 * Software distributed under the License is distributed
9 * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
10 * express or implied. See the GPL for the specific language
11 * governing rights and limitations.
13 * You should have received a copy of the GPL along with this
14 * program. If not, go to http://www.gnu.org/licenses/gpl.html
15 * or write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 package cx.fbn.nevernote.xml;
21 import java.util.Vector;
23 import org.xml.sax.Attributes;
24 import org.xml.sax.SAXException;
25 import org.xml.sax.SAXParseException;
26 import org.xml.sax.helpers.DefaultHandler;
29 public class XMLNoteRepairHandler extends DefaultHandler {
31 private final Vector<String> tagQueue;
32 private final Vector<String> xml;
34 public XMLNoteRepairHandler() {
35 xml = new Vector<String>();
36 tagQueue = new Vector<String>();
39 public void setXml(String x) {
40 // First, change the <hr> tags. That is usually a problem
41 String val = x.toString().replace("<HR>", "<hr/>");
42 val = val.replace("<hr>", "<hr/>");
43 val = val.replace("</HR>", "");
44 x = val.replace("</hr>", "");
46 // Another problem is a bad closing bracket
47 x = x.replace("//>", "/>");
49 // Onother oddball is a bad br
50 x = x.replace("<br/></br>", "<br/>");
55 pos = x.indexOf('\n');
57 line = new String(x.substring(0,pos));
58 x = x.substring(pos+1);
65 public String getXml() {
66 StringBuffer b = new StringBuffer();
67 for (int i=0; i<xml.size(); i++) {
79 public void stripAttribute(String attribute, int line, int column) {
80 String repair = new String(xml.get(line-1));
83 String fragment = repair.substring(0,pos);
84 int startPos = fragment.lastIndexOf("<");
85 fragment = fragment.substring(startPos);
88 // Now we have the fragment, remove the attribute
89 int attributeStart = fragment.indexOf(attribute)-1;
90 int endPos = fragment.indexOf("\"", attributeStart+1);
91 endPos = fragment.indexOf("\"", endPos+1)+1;
93 endPos = fragment.length()-1;
95 String repairedFragment = fragment.substring(0,attributeStart) +fragment.substring(endPos);
96 repair = repair.substring(0,startPos)+ repairedFragment+ repair.substring(column-1);
98 xml.set(line-1, repair);
102 public void renameElement(String element, int line, int column) {
103 for (int i=0; i<xml.size(); i++) {
104 String value = xml.get(i);
105 value = value.replace("<"+element, "<span");
106 value = value.replace("</"+element, "</span");
111 public String repair(int line, int column) {
112 StringBuffer repair = new StringBuffer(xml.get(line-1));
115 for (int i=pos; i>=0; i--) {
116 if (repair.charAt(i) == '<') {
122 repair.insert(pos, "</" +tagQueue.get(tagQueue.size()-1) +">");
125 xml.add(line-1, repair.toString());
133 public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException {
138 public void endElement(String uri, String localName, String qName) throws SAXException {
139 String endVal = tagQueue.lastElement();
140 if (endVal.equals(qName)) {
141 tagQueue.remove(tagQueue.size()-1);
147 public void error(SAXParseException e)
150 Exception exception = new Exception();
151 EnmlException e1 = new EnmlException(e.getMessage(), e.getPublicId(), e.getSystemId(), e.getLineNumber(), e.getColumnNumber(), exception);
156 public void warning(SAXParseException e)
157 throws SAXParseException
159 System.out.println("---------- WARNING ------------- ");