/**************************************************************
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*
*************************************************************/
package basicrunner.basichelper;
import com.sun.star.lang.XInitialization;
import com.sun.star.lang.XSingleServiceFactory;
import com.sun.star.lang.XServiceInfo;
import com.sun.star.lang.XTypeProvider;
import com.sun.star.uno.Type;
import com.sun.star.xml.sax.XDocumentHandler;
import com.sun.star.container.XNameAccess;
import com.sun.star.container.NoSuchElementException;
import java.util.Vector;
import util.XMLTools.Tag;
import util.XMLTools;
import java.io.StringWriter;
import java.io.PrintWriter;
/**
* This class provides a handler of the BASIC test document.
* @see com.sun.star.lang.XSingleServiceFactory
* @see com.sun.star.lang.XServiceInfo
*/
public class DocumentHandler implements XServiceInfo, XSingleServiceFactory {
/** The service name of this class **/
static final String __serviceName = "basichelper.DocumentHandler";
/** The actual handler of the document **/
static DocumentHandlerImpl oDocumentHandler = null;
/** A string writer **/
private StringWriter writer;
/** The log writer (just a wrapper around writer
) **/
private PrintWriter log;
/**
* Create an instance of the document handler.
* @param args A boolean value as args[0] determines,
* if checked XML data is printed to the log.
* Default is false.
* @return The document handler
*/
public Object createInstanceWithArguments(Object[] args) {
boolean printXML = false;
if (args != null && args.length!=0 && args[0] instanceof Boolean)
printXML = ((Boolean)args[0]).booleanValue();
writer = new StringWriter();
log = new PrintWriter(writer);
oDocumentHandler = new DocumentHandlerImpl(log, printXML, writer);
return oDocumentHandler;
}
/**
* Create an instance of the document handler.
* @return The document handler
*/
public Object createInstance() {
return createInstanceWithArguments(null);
}
/** Get the unique id for this implementation
* @return The id.
*/
public byte[] getImplementationId() {
return toString().getBytes();
}
/** Get all implemented types.
* @return The implemented UNO types.
*/
public Type[] getTypes() {
Class interfaces[] = getClass().getInterfaces();
Type types[] = new Type[interfaces.length];
for(int i = 0; i < interfaces.length; ++ i)
types[i] = new Type(interfaces[i]);
return types;
}
/** Is this servioce supported?
* @param name The service name.
* @return True, if the service is supported.
*/
public boolean supportsService(String name) {
return __serviceName.equals(name);
}
/**
* Get all supported service names.
* @return All supported servcices.
*/
public String[] getSupportedServiceNames() {
return new String[] {__serviceName};
}
/**
* Get the implementation name of this class.
* @return The implementation name.
*/
public String getImplementationName() {
return getClass().getName();
}
}
/**
* The actual implementation of the document handler
* @see util.XMLTools.XMLChecker
* @see com.sun.star.lang.XInitialization
* @see com.sun.star.xml.sax.XDocumentHandler
* @see com.sun.star.container.XNameAccess
* @see com.sun.star.lang.XTypeProvider
*/
class DocumentHandlerImpl extends XMLTools.XMLChecker
implements XInitialization, XDocumentHandler,
XNameAccess, XTypeProvider {
/** A string writer **/
private StringWriter writer;
/**
* Constructor
* @param log_ A log writer.
* @param printXML Should XML data be printed to the log?
* @param logWriter A wrapper around log_
for convenience.
*/
public DocumentHandlerImpl(PrintWriter log_,
boolean printXML, StringWriter logWriter) {
super(log_, printXML);
writer = logWriter;
}
/**
* Initialize this class with rules.
* @param parm1 An array of filter rules:
* processAction()
is called for every rule.
* @throws com.sun.star.uno.Exception for an incorrect rule.
*/
public void initialize(Object[] parm1) throws com.sun.star.uno.Exception {
if (!(parm1[0] instanceof Object[])) return;
for (int i=0; iname>/code> is the name of an element.
*/
public boolean hasByName(String name) {
return (name.equals("XMLCode") || name.equals("XMLIsCorrect"));
}
/**
* Get an element by its name.
* @param name The element name.
* @return The element with the specified name
.
* @throws NoSuchElementException Is thrown, if name does not exist.
*/
public Object getByName(String name) throws NoSuchElementException{
if (name.equals("XMLIsCorrect"))
return new Boolean(this.check());
else if (name.equals("XMLCode")) {
return writer.getBuffer().toString();
} else
throw new NoSuchElementException();
}
/**
* Are there any elements?
* @return Always true.
*/
public boolean hasElements() {
return true;
}
/**
* Get the element type.
* @return The type.
*/
public Type getElementType() {
return new Type(Object.class);
}
/**
* Get a unique id for this implementation.
* @return The id.
*/
public byte[] getImplementationId() {
return toString().getBytes();
}
/**
* Return all implemented types of this class.
* @return The implemented UNO types.
*/
public Type[] getTypes() {
Class interfaces[] = getClass().getInterfaces();
Type types[] = new Type[interfaces.length];
for(int i = 0; i < interfaces.length; ++ i)
types[i] = new Type(interfaces[i]);
return types;
}
}