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