Java tutorial
/** * This class caches imported/included XSD definitions. * * Copyright (C) 2007 Stephen Harding * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. * * Please send inquiries to; steve@inverse2.com * * $Revision: 1.3 $ * * $Log: XSDCache.java,v $ * Revision 1.3 2008/07/01 17:26:46 stevewdh * Changed logging so that user can specify the type they want (Java, Log4J or HTML). * * Revision 1.2 2008/03/05 10:47:27 stevewdh * *** empty log message *** * * Revision 1.1 2007/10/04 11:06:40 stevewdh * *** empty log message *** * * Revision 1.2 2007/09/30 13:09:05 stephen harding * Added contact details to license header. * * Revision 1.1 2007/09/15 16:09:05 stephen harding * Added header. * * */ package com.init.octo.schema; import java.io.FileInputStream; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import org.jdom2.Document; import org.jdom2.Namespace; import com.init.octo.util.Logger; public class XSDCache { private Logger log; private Map<String, Object> typeCache; // cache for XML element types private Map<String, Object> schemaCache; // cache for included/imported // schemas private Map<String, List<XMLType>> groupCache; // cache for groups private Map<String, Object> attributeCache; // cache for attributes private Map<String, Object> attributeGroupCache; // cache for attribute // groups private Map<String, Object> elementCache; // cache for elements private Map<String, String>[] namespaceCache; // cache for name spaces private int namespaceIdx; // index into namespace stack public XSDCache() { log = Logger.getLogger(XSDCache.class.getName()); typeCache = new HashMap<String, Object>(); schemaCache = new HashMap<String, Object>(); groupCache = new HashMap<String, List<XMLType>>(); attributeCache = new HashMap<String, Object>(); elementCache = new HashMap<String, Object>(); attributeGroupCache = new HashMap<String, Object>(); namespaceCache = new HashMap[100]; namespaceIdx = -1; } public void show() { showCache("Type cache", typeCache); showCache("Schema cache", schemaCache); // showCache("Group cache", groupCache); showCache("Attribute cache", attributeCache); showCache("Element cache", elementCache); showCache("AttributeGroup cache", attributeGroupCache); } private void showCache(String title, Map<String, Object> typeCache2) { Iterator<String> it; log.info(title); log.info("------------------------"); for (it = typeCache2.keySet().iterator(); it.hasNext();) { log.info(it.next()); } } public void pushNamespaces(String schemaFileName) throws Exception { Namespace namespace = null; String schemaElement; namespaceIdx++; namespaceCache[namespaceIdx] = new HashMap<String, String>(); FileInputStream fis = null; try { byte[] buf = new byte[1024]; StringBuffer str = new StringBuffer(); fis = new FileInputStream(schemaFileName); int schemaStart = -1; int schemaEnd = -1; int buffersRead = 0; while (fis.read(buf, 0, buf.length) > 0 && schemaEnd == -1) { String tmp = new String(buf); str.append(tmp); for (int i = 0; i < tmp.length(); i++) { if (schemaStart == -1) { if (tmp.charAt(i) == '<') { String[] elementSplit = tmp.substring(i + 1).split("[\\s:]", 3); if ("schema".compareToIgnoreCase(elementSplit[0]) == 0 || "schema".compareToIgnoreCase(elementSplit[1]) == 0) { schemaStart = i + (buffersRead * buf.length); } } } else { if (tmp.charAt(i) == '>') { schemaEnd = i + (buffersRead * buf.length); break; } } } buffersRead++; } schemaElement = str.substring(schemaStart, schemaEnd); } catch (Exception ex) { log.error("Exception caching schema namespaces [" + schemaFileName + "]", ex); throw ex; } finally { if (null != fis) { fis.close(); } } String[] splitSchemaElement = schemaElement.split("[\\s]"); boolean cache = false; for (int i = 0; i < splitSchemaElement.length; i++) { cache = false; if (splitSchemaElement[i].startsWith("targetNamespace")) { String[] tmp = splitSchemaElement[i].split("\""); namespace = Namespace.getNamespace(tmp[1]); cache = true; } else if (splitSchemaElement[i].startsWith("xmlns")) { String[] tmp = splitSchemaElement[i].split("[\"=]"); String[] pre = tmp[0].split(":"); namespace = Namespace.getNamespace((pre.length < 2) ? "" : pre[1], tmp[2]); cache = true; } if (cache) { cacheNamespace(namespace); } } } public void popNamespaces() { namespaceIdx--; } public String getNamespace(String abbriev) { String fullNamespace = (String) namespaceCache[namespaceIdx].get(abbriev); if (fullNamespace == null) { return (""); } return (fullNamespace); } private void cacheNamespace(Namespace namespace) { String prefix = namespace.getPrefix(); String uri = namespace.getURI(); namespaceCache[namespaceIdx].put(prefix, uri); } private String namespaceItem(String item) { String[] x = item.split(":"); String ns; String namespace; if (x.length == 1) { ns = ""; } else { ns = x[0]; } for (int idx = namespaceIdx; idx >= 0; idx--) { namespace = (String) namespaceCache[idx].get(ns); if (namespace != null) { return (namespace + x[x.length - 1]); } } return (x[x.length - 1]); } /** Element type cache... ************************************************************/ public void putType(String typeName, XSDElementTypeComplex type) { typeCache.put(namespaceItem(typeName), type); } public XSDElementTypeComplex getElementTypeComplex(String typeName) { return (XSDElementTypeComplex) (typeCache.get(namespaceItem(typeName))); } /** Schema cache... ******************************************************************/ public void putSchema(String schemaName, Document schema) { schemaCache.put(schemaName, schema); } public int schemaCacheSize() { return (schemaCache.size()); } public void clearSchemaCache() { schemaCache = new HashMap<String, Object>(); } public boolean schemaCached(String schemaName) { return (schemaCache.containsKey(schemaName)); } /** Group definition cache ************************************************************/ /* public void putGroup(String groupName, XSDGroup group) { groupCache.put(namespaceItem(groupName), group); } */ public void putGroup(String groupName, List<XMLType> group) { groupCache.put(namespaceItem(groupName), group); } public List<XMLType> getGroup(String groupName) { return groupCache.get(namespaceItem(groupName)); } /** Attribute definition cache ********************************************************/ public void putAttribute(String attributeName, XSDAttribute attribute) { attributeCache.put(namespaceItem(attributeName), attribute); } public Object getAttribute(String attributeName) { return (attributeCache.get(namespaceItem(attributeName))); } /** Element definition cache **********************************************************/ public void putElement(String elementName, XSDElement element) { elementCache.put(namespaceItem(elementName), element); } public Object getElement(String elementName) { return (elementCache.get(namespaceItem(elementName))); } /** Attribute group definition cache **************************************************/ public void putAttributeGroup(String attGroupName, XSDAttributeGroup attGroup) { attributeGroupCache.put(namespaceItem(attGroupName), attGroup); } public Object getAttributeGroup(String attGroupName) { return (attributeGroupCache.get(namespaceItem(attGroupName))); } } // end class XSDCache