package org.apache.axis2.om;
import org.apache.axis2.om.impl.OMOutputImpl;
import org.apache.axis2.om.impl.llom.builder.StAXOMBuilder;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import javax.xml.namespace.QName;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
/*
* Copyright 2001-2004 The Apache Software Foundation.
*
* 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.
*
* @author : Eran Chinthaka (chinthaka@apache.org)
*/
public class OMPerfChecker {
private OMDocument omDocument;
private OMFactory factory;
private OMElement documentElement;
File parse1File;
File parse2File;
protected Log log = LogFactory.getLog(getClass());
public OMPerfChecker() {
this.factory = OMAbstractFactory.getOMFactory();
this.omDocument = factory.createOMDocument();
documentElement = factory.createOMElement(new QName("DocumentElement"), omDocument);
omDocument.setDocumentElement(documentElement);
parse1File = new File("parse1.xml");
parse2File = new File("parse2.xml");
}
public void testMaximumFileOMCanHandle() {
try {
while (true) {
// create file
checkFile(parse1File);
checkFile(parse2File);
FileOutputStream outStream = new FileOutputStream(parse1File);
OMOutputImpl output = new OMOutputImpl(outStream, false);
omDocument.serialize(output);
output.flush();
outStream.close();
BufferedWriter out = new BufferedWriter(new FileWriter("filename", true));
// read the file again and serialise
FileInputStream inStream = new FileInputStream(parse1File);
StAXOMBuilder builder = new StAXOMBuilder(inStream);
builder.getDocumentElement().build();
output = new OMOutputImpl(new FileOutputStream(parse2File), false);
omDocument.serialize(output);
output.flush();
inStream.close();
System.out.println("Completed File Size is " + (parse1File.length() / 1024) + "kB");
// no problem ? increase the size and repeat
increaseXMLSize();
System.gc();
}
} catch (Exception e) {
log.error(e.getMessage(), e);
}
}
public void increaseXMLSize() {
OMElement element = factory.createOMElement("Element", null);
documentElement.addChild(element);
OMElement elementTwo;
String elementText;
for (int i = 0; i < 10000; i++) {
elementTwo = factory.createOMElement("ElementTwo", null);
elementText = new String();
for (int j = 0; j < 10000; j++) {
elementText += "This is some text that will be added recursivelly.";
}
elementTwo.setText(elementText);
element.addChild(elementTwo);
}
}
public void testOM() throws Exception {
System.out.println("Reading File " + parse1File.getName() + ". Size = " +
(parse1File.length() / 1024) + "kB");
FileInputStream inStream = new FileInputStream(parse1File);
StAXOMBuilder builder = new StAXOMBuilder(inStream);
builder.getDocumentElement().build();
OMOutputImpl output = new OMOutputImpl(new FileOutputStream(parse2File), false);
omDocument.serialize(output);
output.flush();
inStream.close();
System.out.println("Completed File Size is " + (parse1File.length() / 1024) + "kB");
}
public static void main(String[] args) {
try {
OMPerfChecker perfChecker = new OMPerfChecker();
perfChecker.testOM();
} catch (Exception e) {
e.printStackTrace();
}
}
private void checkFile(File file) throws IOException {
if (file.exists()) {
file.delete();
}
file.createNewFile();
}
}