/* NamespaceMap.java * * Copyright (c) 2003 by Benja Fallenstein * * This file is part of Fenfire. * * Fenfire 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 of the License, or * (at your option) any later version. * * Fenfire 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 Fenfire; if not, write to the Free * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, * MA 02111-1307 USA * */ /* * Written by Benja Fallenstein */ package gzz.loom; import java.io.*; import java.util.*; import org.xml.sax.*; /** A class mapping XML namespace abbreviations like "rdf:" to * namespace URIs. */ public class NamespaceMap { Map m = new HashMap(); /** Add a shortname -> namespace mapping. * @param name The short name of the namespace, e.g. "rdfs". * @param uri The URI of the namespace. */ public void put(String name, String uri) { m.put(name, uri); } /** Get the abbreviation of an RDF resource URI. * If the URI starts with any of the namespace * URIs in this map, an abbreviation is returned * (e.g. "rdf:type"). Otherwise, a full URI * is returned. */ public String getAbbrev(String uri) { for(Iterator i=m.entrySet().iterator(); i.hasNext();) { Map.Entry e = (Map.Entry)i.next(); String name = (String)e.getKey(); String nameUri = (String)e.getValue(); if(uri.startsWith(nameUri)) return name + ":" + uri.substring(nameUri.length()); } return uri; } /** Load the name -> uri mappings from an XML file. */ public void loadMappings(Reader r) throws IOException, SAXException { XMLReader xr = new org.apache.xerces.parsers.SAXParser(); ContentHandler h = new org.xml.sax.helpers.DefaultHandler() { public void startPrefixMapping(java.lang.String prefix, java.lang.String uri) { put(prefix, uri); } }; xr.setContentHandler(h); xr.parse(new InputSource(r)); } }