Author: vgritsenko Date: Thu Aug 9 21:35:01 2007 New Revision: 564461 URL: http://svn.apache.org/viewvc?view=rev&rev=564461 Log: Fix bug in SymbolTable: see lookup.indexOf() line - was preventing namespaced indexes from be selected by index manager.
Set indexer status to READY in index manager. Remove unused test file. Add SymbolTable tests. Added: xml/xindice/trunk/java/tests/src/org/apache/xindice/xml/SymbolTableTest.java (with props) Removed: xml/xindice/trunk/java/tests/src/org/apache/xindice/UnitTests.java Modified: xml/xindice/trunk/java/src/org/apache/xindice/core/indexer/DocumentHandler.java xml/xindice/trunk/java/src/org/apache/xindice/core/indexer/IndexManager.java xml/xindice/trunk/java/src/org/apache/xindice/xml/SymbolTable.java Modified: xml/xindice/trunk/java/src/org/apache/xindice/core/indexer/DocumentHandler.java URL: http://svn.apache.org/viewvc/xml/xindice/trunk/java/src/org/apache/xindice/core/indexer/DocumentHandler.java?view=diff&rev=564461&r1=564460&r2=564461 ============================================================================== --- xml/xindice/trunk/java/src/org/apache/xindice/core/indexer/DocumentHandler.java (original) +++ xml/xindice/trunk/java/src/org/apache/xindice/core/indexer/DocumentHandler.java Thu Aug 9 21:35:01 2007 @@ -185,7 +185,7 @@ String nsURI = atts.getURI(i); short id; if (nsURI != null && nsURI.length() > 0) { - String attrNSID = "ns" + Integer.toString(nsURI.hashCode()) + ":" + atts.getLocalName(i); + String attrNSID = SymbolTable.getNormalizedQName(atts.getLocalName(i), nsURI); id = symbols.getSymbol(attrNSID, nsURI, true); } else { id = symbols.getSymbol(atts.getQName(i)); @@ -234,4 +234,3 @@ } } } - Modified: xml/xindice/trunk/java/src/org/apache/xindice/core/indexer/IndexManager.java URL: http://svn.apache.org/viewvc/xml/xindice/trunk/java/src/org/apache/xindice/core/indexer/IndexManager.java?view=diff&rev=564461&r1=564460&r2=564461 ============================================================================== --- xml/xindice/trunk/java/src/org/apache/xindice/core/indexer/IndexManager.java (original) +++ xml/xindice/trunk/java/src/org/apache/xindice/core/indexer/IndexManager.java Thu Aug 9 21:35:01 2007 @@ -266,7 +266,7 @@ } } } else { - status.put(name, STATUS_BUSY); + status.put(name, STATUS_READY); idx.open(); } indexes.put(name, idx); @@ -383,13 +383,19 @@ Iterator i = indexes.values().iterator(); while (i.hasNext()) { Indexer index = (Indexer) i.next(); - if (!status.get(index.getName()).equals(STATUS_READY) || !index.getIndexStyle().equals(style)) { + // Indexer is not ready: can not use it + if (!status.get(index.getName()).equals(STATUS_READY)) { + continue; + } + + // Indexer is of different style: can not use it + if (!index.getIndexStyle().equals(style)) { continue; } // TODO: should it check patterns? // there can be only one full text index for a collection - if (style.equals(Indexer.STYLE_FULLTEXT) && index.getIndexStyle().equals(style)) { + if (style.equals(Indexer.STYLE_FULLTEXT)) { return index; } Modified: xml/xindice/trunk/java/src/org/apache/xindice/xml/SymbolTable.java URL: http://svn.apache.org/viewvc/xml/xindice/trunk/java/src/org/apache/xindice/xml/SymbolTable.java?view=diff&rev=564461&r1=564460&r2=564461 ============================================================================== --- xml/xindice/trunk/java/src/org/apache/xindice/xml/SymbolTable.java (original) +++ xml/xindice/trunk/java/src/org/apache/xindice/xml/SymbolTable.java Thu Aug 9 21:35:01 2007 @@ -88,16 +88,12 @@ return "ns" + namespaceURI.hashCode() + ':' + localName; } - private static String getLookupName(String qname, String namespaceURI) { - return '[' + namespaceURI + ']' + qname; - } - public static short getNormalizedSymbol(SymbolTable symbols, String lookup, NamespaceMap nsMap, boolean create) { // Parse [<namespaceURI>]<nsPrefix>:<localName> with optional nsPrefix if (lookup.startsWith("[")) { int idx = lookup.indexOf(']'); String nsURI = lookup.substring(1, idx); - int cidx = lookup.indexOf(idx + 1, ':'); + int cidx = lookup.indexOf(':', idx + 1); String name = cidx != -1 ? lookup.substring(cidx + 1) : lookup.substring(idx + 1); return symbols.getSymbol(getNormalizedQName(name, nsURI), nsURI, create); @@ -112,14 +108,15 @@ String name = lookup.substring(idx + 1); return symbols.getSymbol(getNormalizedQName(name, nsURI), nsURI, create); } - - // Attempt to lookup <nsPrefix>:<localName> when nsMap has no namespace URI for the prefix - return symbols.getSymbol(lookup, create); } return symbols.getSymbol(lookup, create); } + private static String getLookupName(String qname, String namespaceURI) { + return '[' + namespaceURI + ']' + qname; + } + // // Instance Methods // @@ -140,15 +137,32 @@ this.lastModified = System.currentTimeMillis(); } + public final long getLastModified() { + return lastModified; + } + + // + // Lookup by symbol id + // + public final SymbolInfo getSymbolInfo(short symbol) { return (SymbolInfo) names.get(new Short(symbol)); } - public final SymbolInfo getSymbolInfo(String qname, String namespaceURI) { - String lookupName = getLookupName(qname, namespaceURI); - return (SymbolInfo) symbols.get(lookupName); + public final String getNamespaceURI(short symbol) { + SymbolInfo info = getSymbolInfo(symbol); + return info != null ? info.namespaceURI : null; } - + + public final String getName(short symbol) { + SymbolInfo info = getSymbolInfo(symbol); + return info != null ? info.qname : null; + } + + // + // Lookup by qname and namespace + // + public final short getSymbol(String qname) { return getSymbol(qname, false); } @@ -199,36 +213,28 @@ return -1; } - public final String getNamespaceURI(short symbol) { - SymbolInfo info = getSymbolInfo(symbol); - return info != null ? info.namespaceURI : null; - } - - public final String getName(short symbol) { - SymbolInfo info = getSymbolInfo(symbol); - return info != null ? info.qname : null; - } - - public final long getLastModified() { - return lastModified; - } + // + // XMLSerializable + // public final Element streamToXML(Document doc) throws DOMException { Element root = doc.createElement(SYMBOLS); synchronized (symbols) { - Iterator list = symbols.values().iterator(); - while (list.hasNext()) { - SymbolInfo info = (SymbolInfo) list.next(); + Iterator i = symbols.values().iterator(); + while (i.hasNext()) { + SymbolInfo info = (SymbolInfo) i.next(); + Element e = doc.createElement(SYMBOL); + e.setAttribute(ID, Short.toString(info.symbol)); e.setAttribute(NAME, info.qname); if (info.namespaceURI != null && info.namespaceURI.length() > 0) { e.setAttribute(NSURI, info.namespaceURI); } - e.setAttribute(ID, Short.toString(info.symbol)); root.appendChild(e); } } + return root; } @@ -248,12 +254,12 @@ namespaceURI = null; } - Short id = new Short(elem.getAttribute(ID)); - if (id.shortValue() > maxSymbol) { - maxSymbol = id.shortValue(); + short id = Short.parseShort(elem.getAttribute(ID)); + if (id > maxSymbol) { + maxSymbol = id; } - SymbolInfo info = new SymbolInfo(qname, namespaceURI, id.shortValue()); + SymbolInfo info = new SymbolInfo(qname, namespaceURI, id); if (namespaceURI != null) { String lookupName = getLookupName(qname, namespaceURI); symbols.put(lookupName, info); @@ -261,7 +267,7 @@ symbols.put(qname, info); } - names.put(id, info); + names.put(new Short(id), info); } } } Added: xml/xindice/trunk/java/tests/src/org/apache/xindice/xml/SymbolTableTest.java URL: http://svn.apache.org/viewvc/xml/xindice/trunk/java/tests/src/org/apache/xindice/xml/SymbolTableTest.java?view=auto&rev=564461 ============================================================================== --- xml/xindice/trunk/java/tests/src/org/apache/xindice/xml/SymbolTableTest.java (added) +++ xml/xindice/trunk/java/tests/src/org/apache/xindice/xml/SymbolTableTest.java Thu Aug 9 21:35:01 2007 @@ -0,0 +1,144 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * $Id$ + */ + +package org.apache.xindice.xml; + +import org.apache.xindice.xml.dom.DOMParser; + +import junit.framework.TestCase; + +/** + * @version $Revision$, $Date$ + */ +public class SymbolTableTest extends TestCase { + + private static final String SYMBOLS = + "<symbols>" + + " <symbol id='1' name='simple'/>" + + " <symbol id='2' nsuri='http://www.w3.org/1999/xhtml' name='spaced'/>" + + " <symbol id='3' nsuri='http://www.w3.org/1999/xhtml' name='a:spaced'/>" + + " <symbol id='4' nsuri='http://www.w3.org/1999/xhtml' name='b:spaced'/>" + + "</symbols>"; + + private SymbolTable symbols; + + + protected void setUp() throws Exception { + symbols = new SymbolTable(DOMParser.toDocument(SYMBOLS).getDocumentElement()); + } + + + public void testMissingSymbol() { + assertNull(symbols.getSymbolInfo((short) 123)); + + assertEquals(-1, symbols.getSymbol("missing")); + assertEquals(-1, symbols.getSymbol("missing", false)); + assertEquals(-1, symbols.getSymbol("missing", "urn:namespace:uri")); + assertEquals(-1, symbols.getSymbol("missing", "urn:namespace:uri", false)); + + assertFalse(symbols.isDirty()); + } + + public void testSimpleSymbol() { + SymbolTable.SymbolInfo si = symbols.getSymbolInfo((short) 1); + assertNull(si.getNamespaceURI()); + assertEquals("simple", si.getQName()); + assertEquals(1, si.getSymbolID()); + + assertEquals(1, symbols.getSymbol("simple")); + + assertFalse(symbols.isDirty()); + } + + public void testNamespacedSymbol() { + SymbolTable.SymbolInfo si; + + si = symbols.getSymbolInfo((short) 2); + assertEquals("http://www.w3.org/1999/xhtml", si.getNamespaceURI()); + assertEquals("spaced", si.getQName()); + assertEquals(2, si.getSymbolID()); + + si = symbols.getSymbolInfo((short) 3); + assertEquals("http://www.w3.org/1999/xhtml", si.getNamespaceURI()); + assertEquals("a:spaced", si.getQName()); + assertEquals(3, si.getSymbolID()); + + si = symbols.getSymbolInfo((short) 4); + assertEquals("http://www.w3.org/1999/xhtml", si.getNamespaceURI()); + assertEquals("b:spaced", si.getQName()); + assertEquals(4, si.getSymbolID()); + + assertEquals(2, symbols.getSymbol("spaced", "http://www.w3.org/1999/xhtml")); + assertEquals(3, symbols.getSymbol("a:spaced", "http://www.w3.org/1999/xhtml")); + assertEquals(4, symbols.getSymbol("b:spaced", "http://www.w3.org/1999/xhtml")); + + assertEquals(-1, symbols.getSymbol("spaced")); + assertEquals(-1, symbols.getSymbol("a:spaced")); + assertEquals(-1, symbols.getSymbol("x:spaced", "http://www.w3.org/1999/xhtml")); + + assertFalse(symbols.isDirty()); + } + + public void testAddSymbol() { + assertEquals(-1, symbols.getSymbol("missing")); + assertTrue(symbols.getSymbol("missing", true) != -1); + assertTrue(symbols.isDirty()); + + short s = symbols.getSymbol("missing"); + assertTrue(s != -1); + + SymbolTable.SymbolInfo si = symbols.getSymbolInfo(s); + assertEquals("missing", si.getQName()); + assertNull(si.getNamespaceURI()); + assertEquals(s, si.getSymbolID()); + } + + public void testAddNamespacedSymbol() { + assertEquals(-1, symbols.getSymbol("missing", "urn:unknown")); + assertTrue(symbols.getSymbol("missing", "urn:unknown", true) != -1); + assertTrue(symbols.isDirty()); + + short s = symbols.getSymbol("missing", "urn:unknown"); + assertTrue(s != -1); + + SymbolTable.SymbolInfo si = symbols.getSymbolInfo(s); + assertEquals("missing", si.getQName()); + assertEquals("urn:unknown", si.getNamespaceURI()); + assertEquals(s, si.getSymbolID()); + } + + public void testGetNormalizedSymbol() { + assertEquals(1, SymbolTable.getNormalizedSymbol(symbols, "simple", null, false)); + + assertEquals(-1, SymbolTable.getNormalizedSymbol(symbols, "[http://www.w3.org/1999/xhtml]spaced", null, false)); + assertEquals(-1, SymbolTable.getNormalizedSymbol(symbols, "[http://www.w3.org/1999/xhtml]a:spaced", null, false)); + assertEquals(-1, SymbolTable.getNormalizedSymbol(symbols, "[http://www.w3.org/1999/xhtml]b:spaced", null, false)); + + short s = SymbolTable.getNormalizedSymbol(symbols, "[http://www.w3.org/1999/xhtml]spaced", null, true); + + assertEquals(s, SymbolTable.getNormalizedSymbol(symbols, "[http://www.w3.org/1999/xhtml]spaced", null, false)); + assertEquals(s, SymbolTable.getNormalizedSymbol(symbols, "[http://www.w3.org/1999/xhtml]a:spaced", null, false)); + assertEquals(s, SymbolTable.getNormalizedSymbol(symbols, "[http://www.w3.org/1999/xhtml]b:spaced", null, false)); + assertEquals(s, SymbolTable.getNormalizedSymbol(symbols, "[http://www.w3.org/1999/xhtml]x:spaced", null, false)); + + NamespaceMap m = new NamespaceMap(); + m.setNamespace("z", "http://www.w3.org/1999/xhtml"); + assertEquals(s, SymbolTable.getNormalizedSymbol(symbols, "z:spaced", m, false)); + } +} Propchange: xml/xindice/trunk/java/tests/src/org/apache/xindice/xml/SymbolTableTest.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: xml/xindice/trunk/java/tests/src/org/apache/xindice/xml/SymbolTableTest.java ------------------------------------------------------------------------------ svn:keywords = Id Revision Author Date