Mercurial > hg > digilib
changeset 1498:c1b27845aea3
renamed XMLListLoader to more accurate XMLMapLoader. created new XMLMapListLoader.
author | robcast |
---|---|
date | Thu, 31 Mar 2016 14:08:01 +0200 |
parents | 77c5890bb699 |
children | 31566778c251 |
files | common/src/main/java/digilib/io/AliasingDocuDirCache.java common/src/main/java/digilib/util/XMLListLoader.java common/src/main/java/digilib/util/XMLMapListLoader.java common/src/main/java/digilib/util/XMLMapLoader.java servlet/src/main/java/digilib/auth/IpAuthnOps.java servlet/src/main/java/digilib/auth/MetaAccessAuthzOps.java servlet/src/main/java/digilib/auth/PathAuthzOps.java servlet/src/main/java/digilib/conf/DigilibServletConfiguration.java |
diffstat | 8 files changed, 402 insertions(+), 216 deletions(-) [+] |
line wrap: on
line diff
--- a/common/src/main/java/digilib/io/AliasingDocuDirCache.java Wed Mar 30 16:26:34 2016 +0200 +++ b/common/src/main/java/digilib/io/AliasingDocuDirCache.java Thu Mar 31 14:08:01 2016 +0200 @@ -34,7 +34,7 @@ import digilib.conf.DigilibConfiguration; import digilib.io.FileOps.FileClass; -import digilib.util.XMLListLoader; +import digilib.util.XMLMapLoader; /** * @author casties @@ -56,7 +56,7 @@ // read alias config file try { // load into pathMap - XMLListLoader mapLoader = new XMLListLoader("digilib-aliases", + XMLMapLoader mapLoader = new XMLMapLoader("digilib-aliases", "mapping", "link", "dir"); pathMap = mapLoader.loadUri(confFile.toURI()); } catch (Exception e) {
--- a/common/src/main/java/digilib/util/XMLListLoader.java Wed Mar 30 16:26:34 2016 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,206 +0,0 @@ -package digilib.util; - -/* - * #%L - * XMLListLoader -- Load an XML list into a Map - * - * Digital Image Library servlet components - * - * %% - * Copyright (C) 2001 - 2013 MPIWG Berlin - * %% - * This program 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 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 Lesser Public License for more details. - * - * You should have received a copy of the GNU General Lesser Public - * License along with this program. If not, see - * <http://www.gnu.org/licenses/lgpl-3.0.html>. - * #L% - * Author: Robert Casties (robcast@berlios.de) - */ - -// JAXP packages -import java.io.IOException; -import java.net.URI; -import java.net.URISyntaxException; -import java.util.HashMap; -import java.util.LinkedList; -import java.util.Map; - -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.parsers.SAXParser; -import javax.xml.parsers.SAXParserFactory; - -import org.apache.log4j.Logger; -import org.xml.sax.Attributes; -import org.xml.sax.SAXException; -import org.xml.sax.SAXParseException; -import org.xml.sax.helpers.DefaultHandler; - -/** Loads a simple XML list into a HashMap. - * - * The XML file has an outer <code>list_tag</code>. Every entry is an - * <code>entry_tag</code> with two attributes: the <code>key_att</code> - * key and the <code>value_att</code> value. - * - * The file is read by the <code>loadURL</code> method, that returns a - * HashMap with the key-value pairs. - * - * @author casties - */ -public class XMLListLoader { - - private Logger logger = Logger.getLogger(this.getClass()); - private String listTag = "list"; - private String entryTag = "entry"; - private String keyAtt = "key"; - private String valueAtt = "value"; - - public XMLListLoader() { - } - - /** - * Creates an XMLListLoader with an outer <code>list_tag</code>. Every entry is an - * <code>entry_tag</code> with two attributes: the <code>key_att</code> - * key and the <code>value_att</code> value. - * - * @param list_tag - * @param entry_tag - * @param key_att - * @param value_att - */ - public XMLListLoader(String list_tag, String entry_tag, String key_att, String value_att) { - logger.debug("xmlListLoader(" + list_tag + "," + entry_tag + "," + key_att + "," + value_att + ")"); - listTag = list_tag; - entryTag = entry_tag; - keyAtt = key_att; - valueAtt = value_att; - } - - /** - * inner class XMLListParser to be called by the parser - */ - private class XMLListParser extends DefaultHandler { - - private Map<String, String> listData; - private LinkedList<String> tagSpace; - - public Map<String, String> getData() { - return listData; - } - - // Parser calls this once at the beginning of a document - public void startDocument() throws SAXException { - listData = new HashMap<String, String>(); - tagSpace = new LinkedList<String>(); - } - - // Parser calls this for each element in a document - public void startElement( - String namespaceURI, - String localName, - String qName, - Attributes atts) - throws SAXException { - //System.out.println("<"+qName); - // open a new namespace - tagSpace.addLast(qName); - - // is it an entry tag? - if (qName.equals(entryTag)) { - // is it inside a list tag? - if ((listTag.length() > 0) && (!tagSpace.contains(listTag))) { - logger.error("BOO: Entry " - + entryTag - + " not inside list " - + listTag); - throw new SAXParseException( - "Entry " + entryTag + " not inside list " + listTag, - null); - } - // get the attributes - String key = atts.getValue(keyAtt); - String val = atts.getValue(valueAtt); - if ((key == null) || (val == null)) { - logger.error("BOO: Entry " - + entryTag - + " does not have Attributes " - + keyAtt - + ", " - + valueAtt); - throw new SAXParseException( - "Entry " - + entryTag - + " does not have Attributes " - + keyAtt - + ", " - + valueAtt, - null); - } - // add the values - //System.out.println("DATA: "+key+" = "+val); - listData.put(key, val); - } - } - - public void endElement( - String namespaceURI, - String localName, - String qName) - throws SAXException { - // exit the namespace - tagSpace.removeLast(); - } - - } - - /** - * load and parse a file (as URL) - * returns HashMap with list data - * @deprecated Use {@link #loadUri(URI)} instead - */ - public Map<String, String> loadURL(String uri) throws SAXException, IOException { - try { - return loadUri(new URI(uri)); - } catch (URISyntaxException e) { - logger.error("Unable to convert URI!"); - throw new IOException(e); - } - } - - /** - * load and parse a file (as URL) - * returns HashMap with list data - */ - public Map<String, String> loadUri(URI uri) throws SAXException, IOException { - //System.out.println("loadurl ("+path+")"); - // Create a JAXP SAXParserFactory and configure it - SAXParserFactory spf = SAXParserFactory.newInstance(); - spf.setNamespaceAware(true); - - SAXParser parser = null; - try { - // Create a JAXP SAXParser - parser = spf.newSAXParser(); - - } catch (ParserConfigurationException e) { - throw new SAXException(e); - } - - // create a list parser (keeps the data!) - XMLListParser listParser = new XMLListParser(); - - // Tell the SAXParser to parse the XML document - parser.parse(uri.toString(), listParser); - - return listParser.getData(); - } - -}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/common/src/main/java/digilib/util/XMLMapListLoader.java Thu Mar 31 14:08:01 2016 +0200 @@ -0,0 +1,187 @@ +package digilib.util; + +/* + * #%L + * XMLMapListLoader -- Load XML into a List of Maps + * + * Digital Image Library servlet components + * + * %% + * Copyright (C) 2016 MPIWG Berlin + * %% + * This program 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 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 Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + * Author: Robert Casties (robcast@users.sourceforge.net) + */ + +import java.io.IOException; +import java.net.URI; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; + +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.parsers.SAXParser; +import javax.xml.parsers.SAXParserFactory; + +import org.apache.log4j.Logger; +import org.xml.sax.Attributes; +import org.xml.sax.SAXException; +import org.xml.sax.SAXParseException; +import org.xml.sax.helpers.DefaultHandler; + +/** + * Loads a simple XML structure into a List of Maps. + * + * The XML file has an outer <code>list_tag</code>. Every entry is an + * <code>entry_tag</code>. All attributes are loaded into a Map. + * + * Nested tags are not supported. The text + * content of the tag is put in the Map under the key "_text". + * + * The file is read by the <code>loadURI()</code> method, that returns a List with + * Maps of key-value pairs. + * + * @author casties + */ +public class XMLMapListLoader { + + private Logger logger = Logger.getLogger(this.getClass()); + private String listTag = "list"; + private String entryTag = "entry"; + public static String CONTENT_KEY = "_text"; + + public XMLMapListLoader() { + } + + /** + * Creates an XMLMapListLoader with an outer <code>list_tag</code>. Every + * entry is an <code>entry_tag</code>. All attributes are stored in a Map. + * + * @param list_tag + * @param entry_tag + */ + public XMLMapListLoader(String list_tag, String entry_tag) { + logger.debug("xmlListLoader(" + list_tag + "," + entry_tag + ")"); + listTag = list_tag; + entryTag = entry_tag; + } + + /** + * Inner class MapListParser to be called by the SAX parser + */ + private class MapListParser extends DefaultHandler { + + private List<Map<String, String>> listData; + private Map<String, String> elementData; + private LinkedList<String> tagSpace; + + public List<Map<String, String>> getData() { + return listData; + } + + // Parser calls this once at the beginning of a document + @Override + public void startDocument() throws SAXException { + listData = new LinkedList<Map<String, String>>(); + tagSpace = new LinkedList<String>(); + } + + // Parser calls this for each element in a document + @Override + public void startElement(String namespaceURI, String localName, String qName, Attributes atts) + throws SAXException { + // open a new tag + tagSpace.addLast(qName); + + // is it an entry tag? + if (qName.equals(entryTag)) { + // is it inside a list tag? + if ((listTag.length() > 0) && (!tagSpace.contains(listTag))) { + logger.error("BOO: Entry " + entryTag + " not inside list " + listTag); + throw new SAXParseException("Entry " + entryTag + " not inside list " + listTag, null); + } + // get the attributes + elementData = new HashMap<String, String>(); + int numAtts = atts.getLength(); + for (int i = 0; i < numAtts; ++i) { + // use localname as key + String key = atts.getLocalName(i); + if (key.isEmpty()) { + key = atts.getQName(i); + } + String val = atts.getValue(i); + elementData.put(key, val); + } + // add the values + listData.add(elementData); + } + } + + // Parser calls this for each chunk of text inside an element + @Override + public void characters(char[] ch, int start, int length) throws SAXException { + if (elementData != null) { + // add to current elementData + String text = elementData.get(CONTENT_KEY); + if (text == null) { + text = ""; + } + text += new String(ch, start, length); + elementData.put(text, CONTENT_KEY); + } + } + + // Parser calls this for each element in a document + @Override + public void endElement(String namespaceURI, String localName, String qName) throws SAXException { + // exit the tag + tagSpace.removeLast(); + elementData = null; + } + + } + + /** + * Load and parse a file (as URL). + * + * returns List of Maps with data. + */ + public List<Map<String,String>> loadUri(URI uri) throws SAXException, IOException { + // System.out.println("loadurl ("+path+")"); + // Create a JAXP SAXParserFactory and configure it + SAXParserFactory spf = SAXParserFactory.newInstance(); + spf.setNamespaceAware(true); + + SAXParser parser = null; + try { + // Create a JAXP SAXParser + parser = spf.newSAXParser(); + + } catch (ParserConfigurationException e) { + throw new SAXException(e); + } + + // create a parser (keeps the data!) + MapListParser listParser = new MapListParser(); + + // Tell the SAXParser to parse the XML document + parser.parse(uri.toString(), listParser); + + return listParser.getData(); + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/common/src/main/java/digilib/util/XMLMapLoader.java Thu Mar 31 14:08:01 2016 +0200 @@ -0,0 +1,205 @@ +package digilib.util; + +/* + * #%L + * XMLMapLoader -- Load an XML list into a Map + * + * Digital Image Library servlet components + * + * %% + * Copyright (C) 2001 - 2013 MPIWG Berlin + * %% + * This program 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 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 Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + * Author: Robert Casties (robcast@berlios.de) + */ + +import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.Map; + +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.parsers.SAXParser; +import javax.xml.parsers.SAXParserFactory; + +import org.apache.log4j.Logger; +import org.xml.sax.Attributes; +import org.xml.sax.SAXException; +import org.xml.sax.SAXParseException; +import org.xml.sax.helpers.DefaultHandler; + +/** Loads a simple XML list into a HashMap. + * + * The XML file has an outer <code>list_tag</code>. Every entry is an + * <code>entry_tag</code> with two attributes: the <code>key_att</code> + * key and the <code>value_att</code> value. + * + * The file is read by the <code>loadURL</code> method, that returns a + * HashMap with the key-value pairs. + * + * @author casties + */ +public class XMLMapLoader { + + private Logger logger = Logger.getLogger(this.getClass()); + private String listTag = "list"; + private String entryTag = "entry"; + private String keyAtt = "key"; + private String valueAtt = "value"; + + public XMLMapLoader() { + } + + /** + * Creates an XMLMapLoader with an outer <code>list_tag</code>. Every entry is an + * <code>entry_tag</code> with two attributes: the <code>key_att</code> + * key and the <code>value_att</code> value. + * + * @param list_tag + * @param entry_tag + * @param key_att + * @param value_att + */ + public XMLMapLoader(String list_tag, String entry_tag, String key_att, String value_att) { + logger.debug("xmlListLoader(" + list_tag + "," + entry_tag + "," + key_att + "," + value_att + ")"); + listTag = list_tag; + entryTag = entry_tag; + keyAtt = key_att; + valueAtt = value_att; + } + + /** + * inner class XMLListParser to be called by the parser + */ + private class XMLListParser extends DefaultHandler { + + private Map<String, String> listData; + private LinkedList<String> tagSpace; + + public Map<String, String> getData() { + return listData; + } + + // Parser calls this once at the beginning of a document + public void startDocument() throws SAXException { + listData = new HashMap<String, String>(); + tagSpace = new LinkedList<String>(); + } + + // Parser calls this for each element in a document + public void startElement( + String namespaceURI, + String localName, + String qName, + Attributes atts) + throws SAXException { + //System.out.println("<"+qName); + // open a new namespace + tagSpace.addLast(qName); + + // is it an entry tag? + if (qName.equals(entryTag)) { + // is it inside a list tag? + if ((listTag.length() > 0) && (!tagSpace.contains(listTag))) { + logger.error("BOO: Entry " + + entryTag + + " not inside list " + + listTag); + throw new SAXParseException( + "Entry " + entryTag + " not inside list " + listTag, + null); + } + // get the attributes + String key = atts.getValue(keyAtt); + String val = atts.getValue(valueAtt); + if ((key == null) || (val == null)) { + logger.error("BOO: Entry " + + entryTag + + " does not have Attributes " + + keyAtt + + ", " + + valueAtt); + throw new SAXParseException( + "Entry " + + entryTag + + " does not have Attributes " + + keyAtt + + ", " + + valueAtt, + null); + } + // add the values + //System.out.println("DATA: "+key+" = "+val); + listData.put(key, val); + } + } + + public void endElement( + String namespaceURI, + String localName, + String qName) + throws SAXException { + // exit the namespace + tagSpace.removeLast(); + } + + } + + /** + * load and parse a file (as URL) + * returns HashMap with list data + * @deprecated Use {@link #loadUri(URI)} instead + */ + public Map<String, String> loadURL(String uri) throws SAXException, IOException { + try { + return loadUri(new URI(uri)); + } catch (URISyntaxException e) { + logger.error("Unable to convert URI!"); + throw new IOException(e); + } + } + + /** + * load and parse a file (as URL) + * returns HashMap with list data + */ + public Map<String, String> loadUri(URI uri) throws SAXException, IOException { + //System.out.println("loadurl ("+path+")"); + // Create a JAXP SAXParserFactory and configure it + SAXParserFactory spf = SAXParserFactory.newInstance(); + spf.setNamespaceAware(true); + + SAXParser parser = null; + try { + // Create a JAXP SAXParser + parser = spf.newSAXParser(); + + } catch (ParserConfigurationException e) { + throw new SAXException(e); + } + + // create a list parser (keeps the data!) + XMLListParser listParser = new XMLListParser(); + + // Tell the SAXParser to parse the XML document + parser.parse(uri.toString(), listParser); + + return listParser.getData(); + } + +}
--- a/servlet/src/main/java/digilib/auth/IpAuthnOps.java Wed Mar 30 16:26:34 2016 +0200 +++ b/servlet/src/main/java/digilib/auth/IpAuthnOps.java Thu Mar 31 14:08:01 2016 +0200 @@ -38,7 +38,7 @@ import digilib.conf.DigilibRequest; import digilib.conf.DigilibServletRequest; import digilib.util.HashTree; -import digilib.util.XMLListLoader; +import digilib.util.XMLMapLoader; /** * Implements AuthnOps using paths defined in an XML config file. @@ -84,7 +84,7 @@ Map<String, String> ipList = null; try { // load authIPs - XMLListLoader ipLoader = new XMLListLoader("digilib-addresses", "address", "ip", "role"); + XMLMapLoader ipLoader = new XMLMapLoader("digilib-addresses", "address", "ip", "role"); ipList = ipLoader.loadUri(configFile.toURI()); } catch (Exception e) { throw new AuthOpException("ERROR loading auth config file: " + e);
--- a/servlet/src/main/java/digilib/auth/MetaAccessAuthzOps.java Wed Mar 30 16:26:34 2016 +0200 +++ b/servlet/src/main/java/digilib/auth/MetaAccessAuthzOps.java Thu Mar 31 14:08:01 2016 +0200 @@ -43,7 +43,7 @@ import digilib.io.DocuDirent; import digilib.io.FileOpException; import digilib.meta.MetadataMap; -import digilib.util.XMLListLoader; +import digilib.util.XMLMapLoader; /** * Implementation of AuthzOps using "access" information from file metadata. @@ -85,7 +85,7 @@ Map<String, String> roleList = null; try { // load role mappings - XMLListLoader roleLoader = new XMLListLoader("digilib-access", "access", "type", "role"); + XMLMapLoader roleLoader = new XMLMapLoader("digilib-access", "access", "type", "role"); roleList = roleLoader.loadUri(configFile.toURI()); } catch (Exception e) { throw new AuthOpException("ERROR loading authorization config file: " + e);
--- a/servlet/src/main/java/digilib/auth/PathAuthzOps.java Wed Mar 30 16:26:34 2016 +0200 +++ b/servlet/src/main/java/digilib/auth/PathAuthzOps.java Thu Mar 31 14:08:01 2016 +0200 @@ -34,7 +34,7 @@ import digilib.conf.DigilibServletConfiguration; import digilib.conf.DigilibServletRequest; import digilib.util.HashTree; -import digilib.util.XMLListLoader; +import digilib.util.XMLMapLoader; /** * Implements AuthzOps using paths defined in an XML config file. @@ -75,7 +75,7 @@ Map<String, String> pathList = null; try { // load authPaths - XMLListLoader pathLoader = new XMLListLoader("digilib-paths", "path", "name", "role"); + XMLMapLoader pathLoader = new XMLMapLoader("digilib-paths", "path", "name", "role"); pathList = pathLoader.loadUri(configFile.toURI()); } catch (Exception e) { throw new AuthOpException("ERROR loading authorization config file: " + e);
--- a/servlet/src/main/java/digilib/conf/DigilibServletConfiguration.java Wed Mar 30 16:26:34 2016 +0200 +++ b/servlet/src/main/java/digilib/conf/DigilibServletConfiguration.java Thu Mar 31 14:08:01 2016 +0200 @@ -59,7 +59,7 @@ import digilib.servlet.ServletOps; import digilib.util.DigilibJobCenter; import digilib.util.Parameter; -import digilib.util.XMLListLoader; +import digilib.util.XMLMapLoader; /** * Class to hold the digilib servlet configuration parameters. The parameters @@ -198,7 +198,7 @@ File f = new File(fn); if (f.canRead()) { // setup config file list reader - XMLListLoader lilo = new XMLListLoader("digilib-config", "parameter", "name", "value"); + XMLMapLoader lilo = new XMLMapLoader("digilib-config", "parameter", "name", "value"); // read config file into HashMap Map<String, String> map = lilo.loadUri(f.toURI());