| 1 |
package com.thaiopensource.relaxng.translate.test; |
| 2 |
|
| 3 |
import com.thaiopensource.resolver.xml.sax.SAXResolver; |
| 4 |
import com.thaiopensource.util.UriOrFile; |
| 5 |
import com.thaiopensource.xml.sax.AbstractLexicalHandler; |
| 6 |
import org.xml.sax.Attributes; |
| 7 |
import org.xml.sax.InputSource; |
| 8 |
import org.xml.sax.SAXException; |
| 9 |
import org.xml.sax.SAXNotRecognizedException; |
| 10 |
import org.xml.sax.SAXNotSupportedException; |
| 11 |
import org.xml.sax.XMLReader; |
| 12 |
import org.xml.sax.helpers.DefaultHandler; |
| 13 |
|
| 14 |
import java.io.File; |
| 15 |
import java.io.IOException; |
| 16 |
import java.util.Collections; |
| 17 |
import java.util.Comparator; |
| 18 |
import java.util.List; |
| 19 |
import java.util.Vector; |
| 20 |
|
| 21 |
public class Compare { |
| 22 |
static public boolean compare(File file1, File file2, SAXResolver resolver) throws SAXException, IOException { |
| 23 |
return load(resolver, file1).equals(load(resolver, file2)); |
| 24 |
} |
| 25 |
|
| 26 |
static private List<Event> load(SAXResolver resolver, File file) throws SAXException, IOException { |
| 27 |
InputSource in = new InputSource(UriOrFile.fileToUri(file)); |
| 28 |
Saver saver = new Saver(); |
| 29 |
XMLReader xr = resolver.createXMLReader(); |
| 30 |
|
| 31 |
try { |
| 32 |
xr.setFeature("http://xml.org/sax/features/namespace-prefixes", true); |
| 33 |
} |
| 34 |
catch (SAXNotRecognizedException e) { |
| 35 |
throw new SAXException("support for namespaces-prefixes feature required"); |
| 36 |
} |
| 37 |
catch (SAXNotSupportedException e) { |
| 38 |
throw new SAXException("support for namespaces-prefixes feature required"); |
| 39 |
} |
| 40 |
xr.setContentHandler(saver); |
| 41 |
try { |
| 42 |
xr.setProperty("http://xml.org/sax/properties/lexical-handler", new CommentSaver(saver)); |
| 43 |
} |
| 44 |
catch (SAXNotRecognizedException e) { |
| 45 |
} |
| 46 |
catch (SAXNotSupportedException e) { |
| 47 |
} |
| 48 |
xr.parse(in); |
| 49 |
return saver.getEventList(); |
| 50 |
} |
| 51 |
|
| 52 |
static abstract class Event { |
| 53 |
boolean merge(char[] chars, int start, int count) { |
| 54 |
return false; |
| 55 |
} |
| 56 |
boolean isWhitespace() { |
| 57 |
return false; |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
static class StartElement extends Event { |
| 62 |
private final String qName; |
| 63 |
|
| 64 |
StartElement(String qName) { |
| 65 |
this.qName = qName; |
| 66 |
} |
| 67 |
|
| 68 |
public boolean equals(Object obj) { |
| 69 |
if (!(obj instanceof StartElement)) |
| 70 |
return false; |
| 71 |
return qName.equals(((StartElement)obj).qName); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
static class Attribute extends Event { |
| 76 |
private final String qName; |
| 77 |
private final String value; |
| 78 |
|
| 79 |
Attribute(String qName, String value) { |
| 80 |
this.qName = qName; |
| 81 |
this.value = value; |
| 82 |
} |
| 83 |
|
| 84 |
String getQName() { |
| 85 |
return qName; |
| 86 |
} |
| 87 |
|
| 88 |
public boolean equals(Object obj) { |
| 89 |
if (!(obj instanceof Attribute)) |
| 90 |
return false; |
| 91 |
Attribute other = (Attribute)obj; |
| 92 |
return qName.equals(other.qName) && value.equals(other.value); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
private static class EndElement extends Event { |
| 97 |
public boolean equals(Object obj) { |
| 98 |
return obj instanceof EndElement; |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
static class Comment extends Event { |
| 103 |
private final String value; |
| 104 |
|
| 105 |
Comment(String value) { |
| 106 |
this.value = value; |
| 107 |
} |
| 108 |
|
| 109 |
public boolean equals(Object obj) { |
| 110 |
if (!(obj instanceof Comment)) |
| 111 |
return false; |
| 112 |
return value.equals(((Comment)obj).value); |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
static class Text extends Event { |
| 117 |
private String value; |
| 118 |
|
| 119 |
Text(String value) { |
| 120 |
this.value = value; |
| 121 |
} |
| 122 |
|
| 123 |
public boolean equals(Object obj) { |
| 124 |
if (!(obj instanceof Text)) |
| 125 |
return false; |
| 126 |
return value.equals(((Text)obj).value); |
| 127 |
} |
| 128 |
|
| 129 |
boolean isWhitespace() { |
| 130 |
for (int i = 0, len = value.length(); i < len; i++) { |
| 131 |
switch (value.charAt(i)) { |
| 132 |
case '\r': |
| 133 |
case '\n': |
| 134 |
case '\t': |
| 135 |
case ' ': |
| 136 |
break; |
| 137 |
default: |
| 138 |
return false; |
| 139 |
} |
| 140 |
} |
| 141 |
return true; |
| 142 |
} |
| 143 |
|
| 144 |
boolean merge(char[] chars, int start, int count) { |
| 145 |
StringBuffer buf = new StringBuffer(value); |
| 146 |
buf.append(chars, start, count); |
| 147 |
value = buf.toString(); |
| 148 |
return true; |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
static class Saver extends DefaultHandler { |
| 153 |
private final List<Event> eventList = new Vector<Event>(); |
| 154 |
private final List<Attribute> attributeList = new Vector<Attribute>(); |
| 155 |
|
| 156 |
List<Event> getEventList() { |
| 157 |
return eventList; |
| 158 |
} |
| 159 |
|
| 160 |
void flushWhitespace(boolean endElement) { |
| 161 |
int len = eventList.size(); |
| 162 |
if (len == 0) |
| 163 |
return; |
| 164 |
if (!(eventList.get(len - 1)).isWhitespace()) |
| 165 |
return; |
| 166 |
if (endElement && len > 1 && eventList.get(len - 2) instanceof StartElement) |
| 167 |
return; |
| 168 |
eventList.remove(len - 1); |
| 169 |
} |
| 170 |
|
| 171 |
public void startElement(String ns, String localName, String qName, Attributes attributes) { |
| 172 |
flushWhitespace(false); |
| 173 |
eventList.add(new StartElement(qName)); |
| 174 |
for (int i = 0, len = attributes.getLength(); i < len; i++) |
| 175 |
attributeList.add(new Attribute(attributes.getQName(i), attributes.getValue(i))); |
| 176 |
Collections.sort(attributeList, new Comparator<Attribute>() { |
| 177 |
public int compare(Attribute a1, Attribute a2) { |
| 178 |
return a1.getQName().compareTo(a2.getQName()); |
| 179 |
} |
| 180 |
}); |
| 181 |
eventList.addAll(attributeList); |
| 182 |
attributeList.clear(); |
| 183 |
} |
| 184 |
|
| 185 |
public void endElement(String ns, String localName, String qName) { |
| 186 |
flushWhitespace(true); |
| 187 |
eventList.add(new EndElement()); |
| 188 |
} |
| 189 |
|
| 190 |
public void characters(char[] chars, int start, int length) { |
| 191 |
int len = eventList.size(); |
| 192 |
if (len == 0 || !(eventList.get(len - 1)).merge(chars, start, length)) |
| 193 |
eventList.add(new Text(new String(chars, start, length))); |
| 194 |
} |
| 195 |
|
| 196 |
public void ignorableWhitespace(char[] chars, int start, int length) { |
| 197 |
characters(chars, start, length); |
| 198 |
} |
| 199 |
|
| 200 |
public void endDocument() { |
| 201 |
flushWhitespace(false); |
| 202 |
} |
| 203 |
|
| 204 |
void comment(String value) { |
| 205 |
flushWhitespace(false); |
| 206 |
eventList.add(new Comment(value)); |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
static class CommentSaver extends AbstractLexicalHandler { |
| 211 |
private final Saver saver; |
| 212 |
CommentSaver(Saver saver) { |
| 213 |
this.saver = saver; |
| 214 |
} |
| 215 |
|
| 216 |
public void comment(char[] chars, int start, int length) throws SAXException { |
| 217 |
saver.comment(new String(chars, start, length)); |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
static public void main(String[] args) throws SAXException, IOException { |
| 222 |
System.err.println(compare(new File(args[0]), new File(args[1]), new SAXResolver())); |
| 223 |
} |
| 224 |
} |