Xml2Xml.java
001 /* This file is part of the project "Hilbert II" - http://www.qedeq.org
002  *
003  * Copyright 2000-2013,  Michael Meyling <mime@qedeq.org>.
004  *
005  * "Hilbert II" is free software; you can redistribute
006  * it and/or modify it under the terms of the GNU General Public
007  * License as published by the Free Software Foundation; either
008  * version 2 of the License, or (at your option) any later version.
009  *
010  * This program is distributed in the hope that it will be useful,
011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013  * GNU General Public License for more details.
014  */
015 
016 package org.qedeq.kernel.xml.dao;
017 
018 import java.io.File;
019 import java.io.FileOutputStream;
020 import java.io.IOException;
021 import java.io.OutputStream;
022 import java.net.URL;
023 import java.util.Locale;
024 
025 import org.qedeq.base.io.IoUtility;
026 import org.qedeq.base.io.TextOutput;
027 import org.qedeq.base.io.UrlUtility;
028 import org.qedeq.base.trace.Trace;
029 import org.qedeq.kernel.bo.common.KernelServices;
030 import org.qedeq.kernel.bo.common.ServiceProcess;
031 import org.qedeq.kernel.bo.module.InternalKernelServices;
032 import org.qedeq.kernel.bo.module.KernelQedeqBo;
033 import org.qedeq.kernel.se.common.ModuleAddress;
034 import org.qedeq.kernel.se.common.Plugin;
035 import org.qedeq.kernel.se.common.SourceFileExceptionList;
036 
037 
038 /**
039  * Test application.
040  *
041  @author  Michael Meyling
042  */
043 public final class Xml2Xml implements Plugin {
044 
045     /** This class. */
046     private static final Class CLASS = Xml2Xml.class;
047 
048     /**
049      * Constructor.
050      */
051     private Xml2Xml() {
052         // nothing to do
053     }
054 
055     /**
056      * Generate XML file out of XML file.
057      *
058      @param   process         Process we run within.
059      @param   services        Use this kernel services.
060      @param   internal        Use this internal kernel services.
061      @param   from            Read this XML file.
062      @param   to              Write to this file. Could be <code>null</code>.
063      @throws  SourceFileExceptionList    Module could not be successfully loaded.
064      @return  File name of generated LaTeX file.
065      */
066     public static String generate(final ServiceProcess process, final KernelServices services,
067             final InternalKernelServices internal, final File from, final File to)
068             throws SourceFileExceptionList {
069         final String method = "generate(File, File)";
070         File destination = null;
071         try {
072             if (to != null) {
073                 destination = to.getCanonicalFile();
074             else {
075                 String xml = from.getName();
076                 if (xml.toLowerCase(Locale.US).endsWith(".xml")) {
077                     xml = xml.substring(0, xml.length() 4);
078                 }
079                 destination = new File(from.getParentFile(), xml + "_.xml").getCanonicalFile();
080             }
081             return generate(process, services, UrlUtility.toUrl(from), destination);
082         catch (IOException e) {
083             Trace.fatal(CLASS, "Writing failed destionation", method, e);
084             throw internal.createSourceFileExceptionList(
085                 DaoErrors.WRITING_MODULE_FILE_FAILED_CODE,
086                 DaoErrors.WRITING_MODULE_FILE_FAILED_TEXT + destination,
087                 to + "", e);
088         }
089     }
090 
091     /**
092      * Generate XML file out of XML file.
093      *
094      @param   process         We work for this process.
095      @param   services        Here we get our kernel services.
096      @param   from            Read this XML file.
097      @param   to              Write to this file. Could not be <code>null</code>.
098      @throws  SourceFileExceptionList     Module could not be successfully loaded.
099      @throws  IOException                 Writing (or reading) failed.
100      @return  File name of generated LaTeX file.
101      */
102     private static String generate(final ServiceProcess process, final KernelServices services,
103             final URL from, final File tothrows SourceFileExceptionList, IOException {
104         final String method = "generate(URL, File)";
105         Trace.begin(CLASS, method);
106         Trace.param(CLASS, method, "from", from);
107         Trace.param(CLASS, method, "to", to);
108         TextOutput printer = null;
109         try {
110             final ModuleAddress address = services.getModuleAddress(from);
111             // TODO mime 20080303: find a solution without casting!
112             final KernelQedeqBo prop = (KernelQedeqBoservices.loadModule(address);
113             if (prop.getLoadingState().isFailure()) {
114                 throw prop.getErrors();
115             }
116             IoUtility.createNecessaryDirectories(to);
117             final OutputStream outputStream = new FileOutputStream(to);
118             printer = new TextOutput(to.getName(), outputStream, "UTF-8");
119             Qedeq2Xml.print(process, new Xml2Xml(), prop, printer);
120             return to.getCanonicalPath();
121         finally {
122             if (printer != null) {
123                 printer.close();
124             }
125             Trace.end(CLASS, method);
126         }
127     }
128 
129     public String getPluginId() {
130         return CLASS.getName();
131     }
132     public String getPluginActionName() {
133         return "Xml2Xml";
134     }
135     public String getPluginDescription() {
136         return "transform XML QEDEQ module in a new XML file";
137     }
138 
139 }