Clover coverage report - QedeqKernelSe Coverage Report
Coverage timestamp: Do Mrz 27 2008 21:46:26 CET
file stats: LOC: 195   Methods: 8
NCLOC: 127   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
XmlModuleLoader.java 83,3% 67,3% 62,5% 68,1%
coverage coverage
 1    /* $Id: XmlModuleLoader.java,v 1.1 2008/03/27 05:16:28 m31 Exp $
 2    *
 3    * This file is part of the project "Hilbert II" - http://www.qedeq.org
 4    *
 5    * Copyright 2000-2008, Michael Meyling <mime@qedeq.org>.
 6    *
 7    * "Hilbert II" is free software; you can redistribute
 8    * it and/or modify it under the terms of the GNU General Public
 9    * License as published by the Free Software Foundation; either
 10    * version 2 of the License, or (at your option) any later version.
 11    *
 12    * This program is distributed in the hope that it will be useful,
 13    * but WITHOUT ANY WARRANTY; without even the implied warranty of
 14    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 15    * GNU General Public License for more details.
 16    */
 17   
 18    package org.qedeq.kernel.xml.loader;
 19   
 20    import java.io.File;
 21    import java.io.IOException;
 22    import java.io.Reader;
 23   
 24    import javax.xml.parsers.ParserConfigurationException;
 25   
 26    import org.qedeq.kernel.base.module.Qedeq;
 27    import org.qedeq.kernel.bo.control.InternalKernelServices;
 28    import org.qedeq.kernel.bo.control.KernelQedeqBo;
 29    import org.qedeq.kernel.bo.control.ModuleFileNotFoundException;
 30    import org.qedeq.kernel.bo.control.ModuleLoader;
 31    import org.qedeq.kernel.common.DefaultSourceFileExceptionList;
 32    import org.qedeq.kernel.common.LoadingState;
 33    import org.qedeq.kernel.common.ModuleContext;
 34    import org.qedeq.kernel.common.ModuleDataException;
 35    import org.qedeq.kernel.common.SourceArea;
 36    import org.qedeq.kernel.common.SourceFileExceptionList;
 37    import org.qedeq.kernel.trace.Trace;
 38    import org.qedeq.kernel.xml.handler.module.QedeqHandler;
 39    import org.qedeq.kernel.xml.mapper.Context2SimpleXPath;
 40    import org.qedeq.kernel.xml.parser.SaxDefaultHandler;
 41    import org.qedeq.kernel.xml.parser.SaxParser;
 42    import org.qedeq.kernel.xml.tracker.SimpleXPath;
 43    import org.qedeq.kernel.xml.tracker.XPathLocationParser;
 44    import org.xml.sax.SAXException;
 45   
 46    import com.sun.syndication.io.XmlReader;
 47   
 48   
 49    /**
 50    * This class provides access methods for loading QEDEQ modules.
 51    *
 52    * @version $Revision: 1.1 $
 53    * @author Michael Meyling
 54    */
 55    public class XmlModuleLoader implements ModuleLoader {
 56   
 57    /** This class. */
 58    private static final Class CLASS = XmlModuleLoader.class;
 59   
 60    /** Internal kernel services. */
 61    private InternalKernelServices services;
 62   
 63    /**
 64    * Constructor.
 65    */
 66  45 public XmlModuleLoader() {
 67    }
 68   
 69  45 public void setServices(final InternalKernelServices services) {
 70  45 this.services = services;
 71    }
 72   
 73  0 public InternalKernelServices getServices() {
 74  0 return this.services;
 75    }
 76   
 77    /**
 78    * Load a local QEDEQ module.
 79    *
 80    * @param prop Module properties.
 81    * @param localFile Load XML file from this location.
 82    * @throws ModuleFileNotFoundException Local file was not found.
 83    * @throws SourceFileExceptionList Module could not be successfully loaded.
 84    */
 85  187 public Qedeq loadLocalModule(final KernelQedeqBo prop,
 86    final File localFile)
 87    throws ModuleFileNotFoundException, SourceFileExceptionList {
 88  187 final String method = "loadLocalModule";
 89  187 prop.setLoader(this);
 90  187 final File file;
 91  187 try {
 92  187 file = localFile.getCanonicalFile();
 93    } catch (IOException e) {
 94  0 Trace.trace(CLASS, this, method, e);
 95  0 throw new ModuleFileNotFoundException("file path not correct: " + localFile);
 96    }
 97  187 if (!file.canRead()) {
 98  32 Trace.trace(CLASS, this, method, "file not readable=" + file);
 99  32 throw new ModuleFileNotFoundException("file not found: " + file);
 100    }
 101  155 prop.setLoadingProgressState(LoadingState.STATE_LOADING_FROM_BUFFER);
 102  155 SaxDefaultHandler handler = new SaxDefaultHandler();
 103  155 QedeqHandler simple = new QedeqHandler(handler);
 104  155 handler.setBasisDocumentHandler(simple);
 105  155 SaxParser parser = null;
 106  155 try {
 107  155 parser = new SaxParser(handler);
 108    } catch (SAXException e) {
 109  0 Trace.trace(CLASS, this, method, e);
 110  0 prop.setLoadingFailureState(LoadingState.STATE_LOADING_FROM_BUFFER_FAILED,
 111    new DefaultSourceFileExceptionList(e));
 112  0 throw createXmlFileExceptionList(e);
 113    } catch (ParserConfigurationException e) {
 114  0 Trace.trace(CLASS, this, method, e);
 115  0 prop.setLoadingFailureState(LoadingState.STATE_LOADING_FROM_BUFFER_FAILED,
 116    new DefaultSourceFileExceptionList(new RuntimeException(
 117    "XML parser configuration error", e)));
 118  0 throw createXmlFileExceptionList(e);
 119    }
 120  155 try {
 121  155 parser.parse(file, prop.getUrl());
 122    } catch (SourceFileExceptionList e) {
 123  20 Trace.trace(CLASS, this, method, e);
 124  20 prop.setEncoding(parser.getEncoding());
 125  20 prop.setLoadingFailureState(LoadingState.STATE_LOADING_FROM_BUFFER_FAILED, e);
 126  20 throw e;
 127    }
 128  135 prop.setEncoding(parser.getEncoding());
 129  135 return simple.getQedeq();
 130    }
 131   
 132    /**
 133    * Get area in XML source file for QEDEQ module context.
 134    *
 135    * @param qedeq Look at this QEDEQ module.
 136    * @param context Search for this context.
 137    * @return Created file area. Maybe <code>null</code>.
 138    */
 139  158 public SourceArea createSourceArea(final Qedeq qedeq, final ModuleContext context) {
 140    // copy constructor
 141  158 final String method = "createSourceArea(Qedeq, ModuleContext)";
 142  158 if (qedeq == null || context == null) {
 143  72 return null;
 144    }
 145  86 ModuleContext ctext = new ModuleContext(context);
 146  86 final String xpath;
 147  86 try {
 148  86 xpath = Context2SimpleXPath.getXPath(ctext, qedeq).toString();
 149    } catch (ModuleDataException e) {
 150  0 Trace.trace(CLASS, method, e);
 151  0 return null;
 152    };
 153   
 154  86 SimpleXPath find = null;
 155  86 try {
 156  86 find = XPathLocationParser.getXPathLocation(
 157    services.getLocalFilePath(ctext.getModuleLocation()),
 158    xpath,
 159    ctext.getModuleLocation().getURL());
 160  86 if (find.getStartLocation() == null) {
 161  0 return null;
 162    }
 163  86 return new SourceArea(ctext.getModuleLocation().getURL(), find.getStartLocation(),
 164    find.getEndLocation());
 165    } catch (ParserConfigurationException e) {
 166  0 Trace.trace(CLASS, method, e);
 167    } catch (SAXException e) {
 168  0 Trace.trace(CLASS, method, e);
 169    } catch (IOException e) {
 170  0 Trace.trace(CLASS, method, e);
 171    }
 172  0 return null;
 173    }
 174   
 175    /**
 176    * Return reader for QEDEQ module source.
 177    *
 178    * @param bo Get source for this one.
 179    * @return Source.
 180    * @throws IOException Reading failed.
 181    */
 182  389 public Reader getModuleReader(final KernelQedeqBo bo)
 183    throws IOException {
 184  389 return new XmlReader(services.getLocalFilePath(bo.getModuleAddress()));
 185    }
 186   
 187  0 private SourceFileExceptionList createXmlFileExceptionList(
 188    final ParserConfigurationException e) {
 189  0 return new DefaultSourceFileExceptionList(e);
 190    }
 191   
 192  0 private DefaultSourceFileExceptionList createXmlFileExceptionList(final SAXException e) {
 193  0 return new DefaultSourceFileExceptionList(e);
 194    }
 195    }