001: /*
002: * Copyright 2001 Sun Microsystems, Inc. All rights reserved.
003: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
004: */
005:
006: package com.sun.portal.search.rdmgr;
007:
008: import com.sun.portal.search.util.*;
009: import com.sun.portal.search.soif.*;
010: import com.sun.portal.search.rdm.*;
011: import com.sun.portal.log.common.PortalLogger;
012:
013: import java.io.*;
014: import java.util.*;
015: import java.util.logging.Logger;
016: import java.util.logging.Level;
017:
018: /**
019: * Alias - Map aliases to SOIF-attribute names for rdmgr
020: *
021: * <pre> alias.conf files looks like (left is real attribute name, right are aliases):
022: * Classification Classification-Id CategoryTerm Category
023: */
024: public class Alias {
025:
026: // XXX this should be part of the schema
027:
028: Map a2p = new HashMap();
029:
030: /** read alias map from Schema into PBlock. */
031: public Alias(RDMSchema schema) throws Exception {
032: int i, j, n, nmv = 0;
033: String attr;
034: String alias_str = null;
035: n = schema.getNumEntriesInt();
036: for (i = 0; i < n; i++) {
037: if ((attr = schema.getSOIFAttribute(i)) != null
038: && (alias_str = schema.getAliases(i)) != null) {
039: String[] aliases = String2Array.string2Array(alias_str,
040: ',', true);
041: for (int k = 0; k < aliases.length; ++k)
042: a2p.put(aliases[k], attr);
043: }
044: }
045: }
046:
047: public int size() {
048: return a2p.size();
049: }
050:
051: /** This modifies the SOIF by renaming aliased attributes to their cnames */
052: // XXX should be handled by indexer code and should not modify the SOIF
053: public int map_soif(SOIF s) {
054: int n = 0;
055: if (a2p == null)
056: return n;
057: Set aliases = a2p.entrySet();
058: for (Iterator i = aliases.iterator(); i.hasNext();) {
059: Map.Entry me = (Map.Entry) i.next();
060: String alias = (String) me.getKey();
061: if (s.contains(alias)) {
062: String cname = (String) me.getValue();
063: SearchLogger.getLogger().log(Level.FINER,
064: "PSSH_CSPSRDMR0045",
065: new Object[] { alias, cname });
066: s.rename(alias, cname);
067: ++n;
068: }
069: }
070: return n;
071: }
072:
073: public String toString() {
074: Set es = a2p.entrySet();
075: String res = "";
076: for (Iterator i = es.iterator(); i.hasNext();) {
077: Map.Entry me = (Map.Entry) i.next();
078: res = res + me.getKey() + " -> " + me.getValue() + "\n";
079: }
080: return res;
081: }
082:
083: /* stuff below is legacy - remove asap */
084:
085: /** read alias map from file into PBlock. */
086: public Alias(String aliasfile) throws Exception {
087: BufferedReader r = new BufferedReader(new FileReader(aliasfile));
088: String line;
089: while ((line = r.readLine()) != null) {
090: if (line.length() == 0 || line.charAt(0) == '#')
091: continue; // skip blank lines and comments
092:
093: line = line.toLowerCase();
094:
095: if (parse(line) == -1) {
096: SearchLogger.getLogger().log(Level.WARNING,
097: "PSSH_CSPSRDMR0046", line);
098: continue;
099: }
100: }
101: }
102:
103: /** copy - create pb_param for each alias name. */
104: protected int copy(Map pb, String alias, String parent,
105: boolean unique) {
106: if (unique && pb.containsKey(alias)) {
107: SearchLogger.getLogger().log(Level.WARNING,
108: "PSSH_CSPSRDMR0047", alias);
109: return -1;
110: }
111: pb.put(alias, parent);
112: return 0;
113: }
114:
115: /** parse - processes one line from alias file. */
116: protected int parse(String line) {
117: String cname, alias;
118: StringTokenizer st = new StringTokenizer(line);
119: if ((cname = st.nextToken()) != null) {
120: while (st.hasMoreTokens()) {
121: alias = st.nextToken();
122: if (copy(a2p, alias, cname, true) == -1)
123: return -1;
124: SearchLogger.getLogger().log(Level.FINE,
125: "PSSH_CSPSRDMR0048",
126: new Object[] { alias, cname });
127: }
128: }
129: return 0;
130: }
131:
132: }
|