/************************************************************************* * * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * Copyright 2000, 2010 Oracle and/or its affiliates. * * OpenOffice.org - a multi-platform office productivity suite * * This file is part of OpenOffice.org. * * OpenOffice.org is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License version 3 * only, as published by the Free Software Foundation. * * OpenOffice.org is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License version 3 for more details * (a copy is included in the LICENSE file that accompanied this code). * * You should have received a copy of the GNU Lesser General Public License * version 3 along with OpenOffice.org. If not, see * * for a copy of the LGPLv3 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; } }