/*
Copyright (c) 2011 Mizar Tools Contributors (mizartools.org)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/* Contributors :
* 2011-03-20 Marco Riccardi - initial implementation
*
*/
package org.mizartools.example.test;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.LinkedList;
import org.mizartools.system.ElementFactory;
import org.mizartools.system.ElementParseException;
import org.mizartools.system.IElement;
import org.mizartools.utility.SystemHost;
import org.mizartools.utility.UtilityFile;
import org.mizartools.utility.xml.XMLElement;
import org.mizartools.utility.xml.XMLFile;
public class FilePrel {
private static XMLFile xmlFile = new XMLFile();
private static ElementFactory elementFactory = ElementFactory.newInstance();
public static String check(File file) throws ElementParseException {
synchronized (FilePrel.class) {
StringBuffer sb = new StringBuffer("");
LinkedList<XMLElement> xmlElement1List;
xmlFile.setFile(file);
xmlElement1List = xmlFile.getXMLElementList();
//sb.append(" [" + xmlElement1List.size() + "]");
try {
String text = UtilityFile.readFile(file);
String textParsed = createText(xmlElement1List);
if (!text.equals(textParsed)) {
sb.append("\n");
sb.append("Error during test of parsing of " + file.toString());
sb.append("\n");
sb.append("ORIGINAL START\n");
sb.append(text + "\n");
sb.append("ORIGINAL END\n");
sb.append("\n");
sb.append("PARSED START\n");
sb.append(textParsed + "\n");
sb.append("PARSED END\n");
}
} catch (FileNotFoundException e) {
throw new ElementParseException(ElementParseException.FILE_NOT_FOUND, "Error opening the file " + file.toString());
} catch (IOException e) {
throw new ElementParseException(ElementParseException.IO, "Error reading the file " + file.toString());
}
if (!sb.toString().equals("")) return sb.toString();
IElement element = elementFactory.createElement(file);
LinkedList<XMLElement> xmlElement2List = element.getXMLElementList();
clearCharacters(xmlElement1List);
clearCharacters(xmlElement2List);
if (!xmlElement1List.equals(xmlElement2List)){
while (!xmlElement1List.isEmpty()){
XMLElement xmlElement1 = xmlElement1List.poll();
XMLElement xmlElement2 = xmlElement2List.poll();
if (!xmlElement1.equals(xmlElement2)){
sb.append("\n");
sb.append("Error during test of parsing of " + file.toString());
sb.append("\n");
sb.append("\n");
sb.append("ORIGINAL START\n");
sb.append(xmlElement1.toString() + "\n");
sb.append("ORIGINAL END\n");
sb.append("\n");
sb.append("PARSED START\n");
sb.append(xmlElement2.toString() + "\n");
sb.append("PARSED END\n");
sb.append("\n");
sb.append("ORIGINAL TAIL START\n");
for (XMLElement xmlElement3: xmlElement1List){
sb.append(xmlElement3.toString() + "\n");
}
sb.append("ORIGINAL TAIL END\n");
}
}
sb.append("\n");
sb.append("Error during test of parsing of " + file.toString());
sb.append("\n");
}
return sb.toString();
}
}
private static void clearCharacters(LinkedList<XMLElement> xmlElementList){
for (XMLElement xmlElement : xmlElementList){
xmlElement.setCharacters("\n");
}
}
private static String createText(LinkedList<XMLElement> xmlElementList){
String ret = SystemHost.lineTerminator;
StringBuffer sb = new StringBuffer("");
sb.append("<?xml version=\"1.0\"?>");
sb.append(ret);
for (XMLElement e: xmlElementList){
sb.append(e.toString().replaceAll("\n", ret));
}
sb.append(ret);
return sb.toString();
}
}