Clover Coverage Report
Coverage timestamp: Sa Aug 2 2008 13:56:27 CEST
../../../../../img/srcFileCovDistChart7.png 62% of files have more coverage
44   158   17   6,29
4   110   0,39   7
7     2,43  
1    
 
  XmlQedeqFileDao       Line # 57 44 17 63,6% 0.6363636
 
  (38)
 
1    /* $Id: XmlQedeqFileDao.java,v 1.1 2008/07/26 08:00:51 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.dao;
19   
20    import java.io.File;
21    import java.io.FileOutputStream;
22    import java.io.IOException;
23    import java.io.OutputStream;
24    import java.io.Reader;
25   
26    import javax.xml.parsers.ParserConfigurationException;
27   
28    import org.qedeq.base.io.TextOutput;
29    import org.qedeq.base.trace.Trace;
30    import org.qedeq.kernel.base.module.Qedeq;
31    import org.qedeq.kernel.bo.QedeqBo;
32    import org.qedeq.kernel.bo.module.InternalKernelServices;
33    import org.qedeq.kernel.bo.module.KernelQedeqBo;
34    import org.qedeq.kernel.bo.module.QedeqFileDao;
35    import org.qedeq.kernel.common.DefaultSourceFileExceptionList;
36    import org.qedeq.kernel.common.ModuleContext;
37    import org.qedeq.kernel.common.ModuleDataException;
38    import org.qedeq.kernel.common.SourceArea;
39    import org.qedeq.kernel.common.SourceFileExceptionList;
40    import org.qedeq.kernel.xml.handler.module.QedeqHandler;
41    import org.qedeq.kernel.xml.mapper.Context2SimpleXPath;
42    import org.qedeq.kernel.xml.parser.SaxDefaultHandler;
43    import org.qedeq.kernel.xml.parser.SaxParser;
44    import org.qedeq.kernel.xml.tracker.SimpleXPath;
45    import org.qedeq.kernel.xml.tracker.XPathLocationParser;
46    import org.xml.sax.SAXException;
47   
48    import com.sun.syndication.io.XmlReader;
49   
50   
51    /**
52    * This class provides access methods for loading QEDEQ modules from XML files.
53    *
54    * @version $Revision: 1.1 $
55    * @author Michael Meyling
56    */
 
57    public class XmlQedeqFileDao implements QedeqFileDao {
58   
59    /** This class. */
60    private static final Class CLASS = XmlQedeqFileDao.class;
61   
62    /** Internal kernel services. */
63    private InternalKernelServices services;
64   
65    /**
66    * Constructor.
67    */
 
68  53 toggle public XmlQedeqFileDao() {
69    }
70   
 
71  53 toggle public void setServices(final InternalKernelServices services) {
72  53 this.services = services;
73    }
74   
 
75  0 toggle public InternalKernelServices getServices() {
76  0 return this.services;
77    }
78   
 
79  110 toggle public Qedeq loadQedeq(final QedeqBo prop, final File file)
80    throws SourceFileExceptionList {
81  110 final String method = "loadLocalModule";
82  110 SaxDefaultHandler handler = new SaxDefaultHandler();
83  110 QedeqHandler simple = new QedeqHandler(handler);
84  110 handler.setBasisDocumentHandler(simple);
85  110 SaxParser parser = null;
86  110 try {
87  110 parser = new SaxParser(handler);
88    } catch (SAXException e) {
89  0 Trace.trace(CLASS, this, method, e);
90  0 final DefaultSourceFileExceptionList sfl = new DefaultSourceFileExceptionList(
91    // TODO mime 20080404: search for better solution
92    new RuntimeException(e));
93  0 throw sfl;
94    } catch (ParserConfigurationException e) {
95  0 Trace.trace(CLASS, this, method, e);
96  0 final DefaultSourceFileExceptionList sfl = new DefaultSourceFileExceptionList(
97    // TODO mime 20080404: search for better solution
98    new RuntimeException("XML parser configuration error", e));
99  0 throw sfl;
100    }
101  110 try {
102  110 parser.parse(file, prop.getUrl());
103    } catch (SourceFileExceptionList e) {
104  4 Trace.trace(CLASS, this, method, e);
105  4 throw e;
106    }
107  106 return simple.getQedeq();
108    }
109   
 
110  0 toggle public void saveQedeq(final KernelQedeqBo prop, final File localFile)
111    throws SourceFileExceptionList, IOException {
112  0 final OutputStream outputStream = new FileOutputStream(localFile);
113  0 final TextOutput printer = new TextOutput(localFile.getName(), outputStream);
114  0 Qedeq2Xml.print(prop, printer);
115    }
116   
 
117  262 toggle public SourceArea createSourceArea(final Qedeq qedeq, final ModuleContext context) {
118    // copy constructor
119  262 final String method = "createSourceArea(Qedeq, ModuleContext)";
120  262 if (qedeq == null || context == null) {
121  126 return null;
122    }
123  136 ModuleContext ctext = new ModuleContext(context);
124  136 final String xpath;
125  136 try {
126  136 xpath = Context2SimpleXPath.getXPath(ctext, qedeq).toString();
127    } catch (ModuleDataException e) {
128  0 Trace.trace(CLASS, method, e);
129  0 return null;
130  136 };
131   
132  136 SimpleXPath find = null;
133  136 try {
134  136 find = XPathLocationParser.getXPathLocation(
135    services.getLocalFilePath(ctext.getModuleLocation()),
136    xpath,
137    ctext.getModuleLocation().getURL());
138  136 if (find.getStartLocation() == null) {
139  0 return null;
140    }
141  136 return new SourceArea(ctext.getModuleLocation().getURL(), find.getStartLocation(),
142    find.getEndLocation());
143    } catch (ParserConfigurationException e) {
144  0 Trace.trace(CLASS, method, e);
145    } catch (SAXException e) {
146  0 Trace.trace(CLASS, method, e);
147    } catch (IOException e) {
148  0 Trace.trace(CLASS, method, e);
149    }
150  0 return null;
151    }
152   
 
153  2 toggle public Reader getModuleReader(final KernelQedeqBo bo)
154    throws IOException {
155  2 return new XmlReader(services.getLocalFilePath(bo.getModuleAddress()));
156    }
157   
158    }