OSDN Git Service

NeverNote 0.88.
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / xml / XMLNoteRepairHandler.java
1 /*
2  * This file is part of NeverNote 
3  * Copyright 2009 Randy Baumgarte
4  * 
5  * This file may be licensed under the terms of of the
6  * GNU General Public License Version 2 (the ``GPL'').
7  *
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.
12  *
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.
17  *
18 */
19 package cx.fbn.nevernote.xml;
20
21 import java.util.Vector;
22
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;
27
28
29 public class XMLNoteRepairHandler extends DefaultHandler {
30         
31         private final Vector<String> tagQueue;
32         private final Vector<String> xml; 
33         
34         public XMLNoteRepairHandler() {
35                 xml = new Vector<String>();
36                 tagQueue = new Vector<String>();
37         }
38         
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>", "");
45                 
46                 // Another problem is a bad closing bracket
47                 x = x.replace("//>", "/>");
48                 
49                 // Onother oddball is a bad br
50                 x = x.replace("<br/></br>", "<br/>");
51                 
52                 int pos = 0;
53                 for (; pos > -1;) {
54                         String line;
55                         pos = x.indexOf('\n');
56                         if (pos > -1) {
57                                 line = new String(x.substring(0,pos));
58                                 x = x.substring(pos+1);
59                         } else
60                                 line = x;
61                         xml.add(line);
62                 }
63         }
64         
65         public String getXml() {
66                 StringBuffer b = new StringBuffer();
67                 for (int i=0; i<xml.size(); i++) {
68                         b.append(xml.get(i));
69                         if (i<xml.size()-1)
70                                 b.append('\n');
71                 }
72                 return b.toString();
73         }
74         
75         public void reset() {
76                 tagQueue.clear();
77         }
78         
79         public void stripAttribute(String attribute, int line, int column) {
80             String repair = new String(xml.get(line-1));
81             
82             int pos = column-1;
83             String fragment = repair.substring(0,pos);
84             int startPos = fragment.lastIndexOf("<");
85             fragment = fragment.substring(startPos);   
86
87         
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;
92         if (endPos == -1) 
93                 endPos = fragment.length()-1;
94         
95         String repairedFragment = fragment.substring(0,attributeStart) +fragment.substring(endPos);
96         repair = repair.substring(0,startPos)+ repairedFragment+ repair.substring(column-1);
97         
98         xml.set(line-1, repair);
99         }
100         
101         
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");
107                         xml.set(i, value);
108                 }
109         }
110         
111         public String repair(int line, int column) {
112             StringBuffer repair = new StringBuffer(xml.get(line-1));
113             int pos = column-1;
114             if (pos > 0) {
115                 for (int i=pos; i>=0; i--) {
116                         if (repair.charAt(i) == '<') {
117                                 pos = i;
118                                 i = -1;
119                         }
120                 }
121                 if (pos > 0) {
122                         repair.insert(pos, "</" +tagQueue.get(tagQueue.size()-1) +">");
123                 }
124                 xml.remove(line-1);
125                 xml.add(line-1, repair.toString());
126             }
127                 
128                 
129                 return getXml();
130         }
131                 
132         @Override
133         public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException {
134                 tagQueue.add(qName);
135         }
136         
137         @Override
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);
142                 } 
143         }
144
145
146         @Override
147         public void error(SAXParseException e)
148           throws EnmlException
149         {
150                 Exception exception = new Exception();
151                 EnmlException e1 = new EnmlException(e.getMessage(), e.getPublicId(), e.getSystemId(), e.getLineNumber(), e.getColumnNumber(), exception);
152                 throw e1;
153         }
154
155         @Override
156         public void warning(SAXParseException e)
157           throws SAXParseException
158         {
159                 System.out.println("---------- WARNING ------------- ");
160           throw e;
161         }
162
163
164 }