com.hfg.xml.msofficexml.OfficeOpenXmlXsd Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of com_hfg Show documentation
Show all versions of com_hfg Show documentation
com.hfg xml, html, svg, and bioinformatics utility library
package com.hfg.xml.msofficexml;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.GZIPInputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import com.hfg.util.StringBuilderPlus;
import com.hfg.util.StringUtil;
import com.hfg.xml.XMLException;
import com.hfg.xml.msofficexml.docx.wordprocessingml.WmlXML;
import com.hfg.xml.msofficexml.xlsx.spreadsheetml.SsmlXML;
import com.hfg.xml.xsd.Xsd;
//------------------------------------------------------------------------------
/**
XML xsd specification for OfficeOpenXml files. This information is need when
producing OfficeOpenXml documents because, for some reason, MS Office applications
are extremely picky about XML tag order.
xsd files downloaded from:
http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-376,%20Fourth%20Edition,%20Part%201%20-%20Fundamentals%20And%20Markup%20Language%20Reference.zip
@author J. Alex Taylor, hairyfatguy.com
*/
//------------------------------------------------------------------------------
// com.hfg XML/HTML Coding Library
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
// [email protected]
//------------------------------------------------------------------------------
public class OfficeOpenXmlXsd extends Xsd
{
private static OfficeOpenXmlXsd sInstance;
private static final List sXsdResources;
static
{
sXsdResources = new ArrayList<>(5);
sXsdResources.add("rsrc/OfficeOpenXML-XMLSchema-Strict.zip");
// From https://docs.microsoft.com/en-us/openspecs/office_standards/ms-xlsx/aa12452a-467d-4192-8ebf-0a90d86dd64b
sXsdResources.add("rsrc/xlbasictypes.xsd.gz"); // http://schemas.microsoft.com/office/excel/2006/main
sXsdResources.add("rsrc/xlslicercache.xsd.gz"); // http://schemas.microsoft.com/office/spreadsheetml/2009/9/main
sXsdResources.add("rsrc/xl15.xsd.gz"); // http://schemas.microsoft.com/office/spreadsheetml/2010/11/main
sXsdResources.add("rsrc/xlpivot16.xsd.gz"); // http://schemas.microsoft.com/office/spreadsheetml/2014/11/main
}
private final static Logger LOGGER = Logger.getLogger(OfficeOpenXmlXsd.class.getPackage().getName());
//###########################################################################
// CONSTRUCTORS
//###########################################################################
//---------------------------------------------------------------------------
private OfficeOpenXmlXsd()
throws Exception
{
super();
// If we don't start by initializing namespaces then some parsed xsd data will lack a prefix
// and values can collide.
SsmlXML.initializeNamespaces();
WmlXML.initializeNamespaces();
for (String rsrc : sXsdResources)
{
InputStream is = OfficeXML.class.getResourceAsStream(rsrc);
if (null == is)
{
throw new RuntimeException("The resource " + StringUtil.singleQuote(rsrc) + " couldn't be found!");
}
if (rsrc.endsWith(".zip"))
{
ZipInputStream zis = new ZipInputStream(is);
ZipEntry zipEntry;
while ((zipEntry = zis.getNextEntry()) != null)
{
LOGGER.log(Level.FINE, "Extracting: " + zipEntry + " ...");
Reader reader = new BufferedReader(new InputStreamReader(zis));
parse(reader);
zis.closeEntry();
}
}
else
{
if (rsrc.endsWith(".gz"))
{
is = new GZIPInputStream(is);
}
LOGGER.log(Level.FINE, "Extracting: " + rsrc + " ...");
Reader reader = new BufferedReader(new InputStreamReader(is));
parse(reader);
}
}
integrateTypesWithElements();
}
//###########################################################################
// PUBLIC METHODS
//###########################################################################
//---------------------------------------------------------------------------
public synchronized static OfficeOpenXmlXsd getInstance()
{
if (null == sInstance)
{
try
{
sInstance = new OfficeOpenXmlXsd();
}
catch (Exception e)
{
throw new XMLException("Problem initializing the OfficeOpenXML XSD info!", e);
}
}
return sInstance;
}
//---------------------------------------------------------------------------
public static Logger getLogger()
{
return LOGGER;
}
//---------------------------------------------------------------------------
public CharSequence getConfigSummary()
{
StringBuilderPlus buffer = new StringBuilderPlus();
buffer.appendln("Loaded XSD resources:");
for (String rsrc : sXsdResources)
{
buffer.appendln(" " + rsrc);
}
buffer.appendln();
buffer.appendln(super.getConfigSummary());
return buffer;
}
}