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.module.InternalKernelServices;
031 import org.qedeq.kernel.bo.module.KernelQedeqBo;
032 import org.qedeq.kernel.se.common.ModuleAddress;
033 import org.qedeq.kernel.se.common.Plugin;
034 import org.qedeq.kernel.se.common.SourceFileExceptionList;
035 
036 
037 /**
038  * Test application.
039  *
040  @author  Michael Meyling
041  */
042 public final class Xml2Xml implements Plugin {
043 
044     /** This class. */
045     private static final Class CLASS = Xml2Xml.class;
046 
047     /**
048      * Constructor.
049      */
050     private Xml2Xml() {
051         // nothing to do
052     }
053 
054     /**
055      * Generate XML file out of XML file.
056      *
057      @param   services        Use this kernel services.
058      @param   from            Read this XML file.
059      @param   to              Write to this file. Could be <code>null</code>.
060      @throws  SourceFileExceptionList    Module could not be successfully loaded.
061      @return  File name of generated LaTeX file.
062      */
063     public static String generate(final InternalKernelServices services,
064             final File from, final File to)
065             throws SourceFileExceptionList {
066         final String method = "generate(File, File)";
067         File destination = null;
068         try {
069             if (to != null) {
070                 destination = to.getCanonicalFile();
071             else {
072                 String xml = from.getName();
073                 if (xml.toLowerCase(Locale.US).endsWith(".xml")) {
074                     xml = xml.substring(0, xml.length() 4);
075                 }
076                 destination = new File(from.getParentFile(), xml + "_.xml").getCanonicalFile();
077             }
078             return generate(services, UrlUtility.toUrl(from), destination);
079         catch (IOException e) {
080             Trace.fatal(CLASS, "Writing failed destionation", method, e);
081             throw services.createSourceFileExceptionList(
082                 DaoErrors.WRITING_MODULE_FILE_FAILED_CODE,
083                 DaoErrors.WRITING_MODULE_FILE_FAILED_TEXT + destination,
084                 to + "", e);
085         }
086     }
087 
088     /**
089      * Generate XML file out of XML file.
090      *
091      @param   services        Here we get our kernel services.
092      @param   from            Read this XML file.
093      @param   to              Write to this file. Could not be <code>null</code>.
094      @throws  SourceFileExceptionList     Module could not be successfully loaded.
095      @throws  IOException                 Writing (or reading) failed.
096      @return  File name of generated LaTeX file.
097      */
098     private static String generate(final KernelServices services, final URL from, final File to)
099             throws SourceFileExceptionList, IOException {
100         final String method = "generate(URL, File)";
101         Trace.begin(CLASS, method);
102         Trace.param(CLASS, method, "from", from);
103         Trace.param(CLASS, method, "to", to);
104         TextOutput printer = null;
105         try {
106             final ModuleAddress address = services.getModuleAddress(from);
107             // TODO mime 20080303: find a solution without casting!
108             final KernelQedeqBo prop = (KernelQedeqBoservices.loadModule(address);
109             if (prop.getLoadingState().isFailure()) {
110                 throw prop.getErrors();
111             }
112             IoUtility.createNecessaryDirectories(to);
113             final OutputStream outputStream = new FileOutputStream(to);
114             printer = new TextOutput(to.getName(), outputStream, "UTF-8");
115             Qedeq2Xml.print(new Xml2Xml(), prop, printer);
116             return to.getCanonicalPath();
117         finally {
118             if (printer != null) {
119                 printer.close();
120             }
121             Trace.end(CLASS, method);
122         }
123     }
124 
125     public String getPluginId() {
126         return CLASS.getName();
127     }
128     public String getPluginActionName() {
129         return "Xml2Xml";
130     }
131     public String getPluginDescription() {
132         return "transform XML QEDEQ module in a new XML file";
133     }
134 
135 }