001: /*
002: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: [See end of file]
004: $Id: DBPrefixMappingImpl.java,v 1.16 2008/01/02 12:08:24 andy_seaborne Exp $
005: */
006:
007: package com.hp.hpl.jena.db.impl;
008:
009: import java.util.Iterator;
010: import java.util.Map;
011:
012: import com.hp.hpl.jena.shared.PrefixMapping;
013: import com.hp.hpl.jena.shared.impl.PrefixMappingImpl;
014:
015: /**
016: * Implementation of prefix mapping specific to databases.
017: * This extends the base implementation, effectively turning it into
018: * a write-through cache - each new namespace is written to
019: * the database as it is added to the prefix map.
020: *
021: *
022: @author csayers
023: @version $Revision: 1.16 $
024: */
025: public class DBPrefixMappingImpl extends PrefixMappingImpl {
026:
027: protected DBPropGraph m_graphProperties = null;
028:
029: /**
030: * Constructor for a persistent prefix mapping.
031: *
032: * Each GraphRDB has a set of associated properties
033: * which are, themselves, represented as triples in
034: * a system graph.
035: *
036: * The prefix mapping is persisted by converting it
037: * to triples and storing it along with the other
038: * properties of the GraphRDB in that system graph.
039: *
040: * @param graphProperties the system properties of a persistent graph.
041: */
042: public DBPrefixMappingImpl(DBPropGraph graphProperties) {
043: super ();
044: m_graphProperties = graphProperties;
045:
046: // Populate the prefix map using data from the
047: // persistent graph properties
048: boolean commit = m_graphProperties.begin();
049: Iterator it = m_graphProperties.getAllPrefixes();
050: while (it.hasNext()) {
051: DBPropPrefix prefix = (DBPropPrefix) it.next();
052: super .setNsPrefix(prefix.getValue(), prefix.getURI());
053: }
054: m_graphProperties.conditionalCommit(commit);
055: }
056:
057: public PrefixMapping removeNsPrefix(String prefix) {
058: super .removeNsPrefix(prefix);
059: m_graphProperties.removePrefix(prefix);
060: return this ;
061: }
062:
063: /* (non-Javadoc)
064: * Override the default implementation so we can catch the write operation
065: * and update the persistent store.
066: * @see com.hp.hpl.jena.shared.PrefixMapping#setNsPrefix(java.lang.String, java.lang.String)
067: */
068: public PrefixMapping setNsPrefix(String prefix, String uri) {
069: // this avoids touching the database for existing maplets.
070: // if (uri.equals( super.getNsPrefixURI( prefix ) )) return this;
071: // Ordering is important here - we need to add it to the prefixMappingImpl
072: // first since it checks the validity of the prefix (it will throw
073: // an exception if there's any problem).
074: super .setNsPrefix(prefix, uri);
075:
076: // All went well, so persist the prefix by adding it to the graph properties
077: // (the addPrefix call will overwrite any existing mapping with the same prefix
078: // so it matches the behaviour of the prefixMappingImpl).
079: m_graphProperties.addPrefix(prefix, uri);
080: return this ;
081: }
082:
083: /* (non-Javadoc)
084: * Override the default implementation so we can catch all write operations
085: * @see com.hp.hpl.jena.shared.PrefixMapping#setNsPrefixes(com.hp.hpl.jena.shared.PrefixMapping)
086: */
087: public PrefixMapping setNsPrefixes(PrefixMapping other) {
088: return setNsPrefixes(other.getNsPrefixMap());
089: }
090:
091: /* (non-Javadoc)
092: * Override the default implementation so we can catch all write operations
093: * @see com.hp.hpl.jena.shared.PrefixMapping#setNsPrefixes(java.util.Map)
094: */
095: public PrefixMapping setNsPrefixes(Map other) {
096: checkUnlocked();
097: Iterator it = other.entrySet().iterator();
098: while (it.hasNext()) {
099: Map.Entry e = (Map.Entry) it.next();
100: setNsPrefix((String) e.getKey(), (String) e.getValue());
101: }
102: return this ;
103: }
104: }
105:
106: /*
107: (c) Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
108: All rights reserved.
109:
110: Redistribution and use in source and binary forms, with or without
111: modification, are permitted provided that the following conditions
112: are met:
113:
114: 1. Redistributions of source code must retain the above copyright
115: notice, this list of conditions and the following disclaimer.
116:
117: 2. Redistributions in binary form must reproduce the above copyright
118: notice, this list of conditions and the following disclaimer in the
119: documentation and/or other materials provided with the distribution.
120:
121: 3. The name of the author may not be used to endorse or promote products
122: derived from this software without specific prior written permission.
123:
124: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
125: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
126: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
127: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
128: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
129: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
130: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
131: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
132: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
133: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
134: */
|