Project

General

Profile

« Previous | Next » 

Revision 2269

Added by Matt Jones over 20 years ago

Beginning new method for building the xml_index table. This uses the
JDBC resultset directly rather than DBSaxNode, and recurses through the
records of the table. The new function 'buildIndex()' would be called, but
currently is not linked in to any code, so it shouldn't get in the way.
Checking in to checkpoint the code for the weekend.

View differences:

src/edu/ucsb/nceas/metacat/DocumentImpl.java
43 43
import java.sql.SQLException;
44 44
import java.util.Enumeration;
45 45
import java.util.Hashtable;
46
import java.util.HashMap;
46 47
import java.util.Iterator;
47 48
import java.util.Stack;
48 49
import java.util.TreeSet;
......
107 108
    private String docname = null;
108 109
    private String doctype = null;
109 110
    private String validateType = null; //base on dtd or schema
110
    // DOCTITLE attr cleared from the db
111
    //  private String doctitle = null;
112 111
    private String createdate = null;
113 112
    private String updatedate = null;
114 113
    private String system_id = null;
......
1152 1151
        out.flush();
1153 1152
    }
1154 1153

  
1154
    /**
1155
     * Build the index record for this document.  This requires that the 
1156
     * DocumentImpl instance exists, so first call the constructor that reads
1157
     * the document from the database.
1158
     */
1159
    public void buildIndex() throws McdbException
1160
    {
1161
        MetaCatUtil util = new MetaCatUtil();
1162
        TreeSet nodeRecordLists = getNodeRecordList(rootnodeid);
1163
        Stack openElements = new Stack();
1164
        boolean atRootElement = true;
1165
        boolean previousNodeWasElement = false;
1166

  
1167
        // Build a map of the same records that are present in the
1168
        // TreeSet so that any node can be easily accessed
1169
        HashMap nodeRecordMap = new HashMap();
1170
        Iterator it = nodeRecordLists.iterator();
1171
        while (it.hasNext()) {
1172
            NodeRecord currentNode = (NodeRecord) it.next();
1173
            Long nodeId = new Long(currentNode.getNodeId());
1174
            nodeRecordMap.put(nodeId, currentNode);
1175
        }
1176

  
1177
        // Step through all of the node records we were given
1178
        it = nodeRecordLists.iterator();
1179
        while (it.hasNext()) {
1180
            NodeRecord currentNode = (NodeRecord) it.next();
1181
            if (currentNode.nodetype.equals("ELEMENT") ||
1182
                currentNode.nodetype.equals("ATTRIBUTE") ) {
1183

  
1184
                System.err.println("Starting Node: " + 
1185
                    currentNode.getNodeId() + " (" + 
1186
                    currentNode.getParentNodeId() + "): " + 
1187
                    currentNode.getNodeName() + " (" +
1188
                    currentNode.getNodeType() + ")" +
1189
                    "\n");
1190
                traverseParents(nodeRecordMap, currentNode.getNodeId(), "");
1191
            }
1192
            /*
1193
            util.debugMessage("[Got Node ID: " + currentNode.nodeid + " ("
1194
                    + currentNode.parentnodeid + ", " + currentNode.nodeindex
1195
                    + ", " + currentNode.nodetype + ", " + currentNode.nodename
1196
                    + ", " + currentNode.nodedata + ")]", 40);
1197
            */
1198
            // Print the end tag for the previous node if needed
1199
            //
1200
            // This is determined by inspecting the parent nodeid for the
1201
            // currentNode. If it is the same as the nodeid of the last element
1202
            // that was pushed onto the stack, then we are still in that
1203
            // previous
1204
            // parent element, and we do nothing. However, if it differs, then
1205
            // we
1206
            // have returned to a level above the previous parent, so we go into
1207
            // a loop and pop off nodes and print out their end tags until we
1208
            // get
1209
            // the node on the stack to match the currentNode parentnodeid
1210
            //
1211
            // So, this of course means that we rely on the list of elements
1212
            // having been sorted in a depth first traversal of the nodes, which
1213
            // is handled by the NodeComparator class used by the TreeSet
1214
            if (!atRootElement) {
1215
                NodeRecord currentElement = (NodeRecord) openElements.peek();
1216
                if (currentNode.parentnodeid != currentElement.nodeid) {
1217
                    while (currentNode.parentnodeid != currentElement.nodeid) {
1218
                        currentElement = (NodeRecord) openElements.pop();
1219
                        util.debugMessage("\n POPPED2: "
1220
                                + currentElement.nodename, 60);
1221
                        if (previousNodeWasElement) {
1222
                            //out.print(">");
1223
                            previousNodeWasElement = false;
1224
                        }
1225
                        if (currentElement.nodeprefix != null) {
1226
                            //out.print("</" + currentElement.nodeprefix + ":"
1227
                                    //+ currentElement.nodename + ">");
1228
                        } else {
1229
                            //System.err.print("/");
1230
                            //out.print("</" + currentElement.nodename + ">");
1231
                        }
1232
                        currentElement = (NodeRecord) openElements.peek();
1233
                    }
1234
                }
1235
            }
1236

  
1237
            // Handle the DOCUMENT node
1238
            if (currentNode.nodetype.equals("DOCUMENT")) {
1239
                // Do nothing
1240
            // Handle the ELEMENT nodes
1241
            } else if (currentNode.nodetype.equals("ELEMENT")) {
1242
                if (atRootElement) {
1243
                    atRootElement = false;
1244
                } else {
1245
                    if (previousNodeWasElement) {
1246
                        //out.print(">");
1247
                    }
1248
                }
1249

  
1250
                openElements.push(currentNode);
1251
                util.debugMessage("\n PUSHED2: " + currentNode.nodename, 60);
1252
                previousNodeWasElement = true;
1253
                if (currentNode.nodeprefix != null) {
1254
                    //out.print("<" + currentNode.nodeprefix + ":"
1255
                            //+ currentNode.nodename);
1256
                } else {
1257
                    //out.print("<" + currentNode.nodename);
1258
                    //System.err.print(currentNode.nodename + "\n");
1259
                    //System.err.print("/");
1260
                    //System.err.print(currentNode.nodename + "\n");
1261
                }
1262

  
1263
            // Handle the ATTRIBUTE nodes
1264
            } else if (currentNode.nodetype.equals("ATTRIBUTE")) {
1265
                if (currentNode.nodeprefix != null) {
1266
                    //out.print(" " + currentNode.nodeprefix + ":"
1267
                            //+ currentNode.nodename + "=\""
1268
                            //+ currentNode.nodedata + "\"");
1269
                } else {
1270
                    //out.print(" " + currentNode.nodename + "=\""
1271
                            //+ currentNode.nodedata + "\"");
1272
                    //System.err.print(currentNode.nodename);
1273
                }
1274

  
1275
            // Handle the NAMESPACE nodes
1276
            } else if (currentNode.nodetype.equals("NAMESPACE")) {
1277
                // Do nothing
1278

  
1279
            // Handle the TEXT nodes
1280
            } else if (currentNode.nodetype.equals("TEXT")) {
1281
                if (previousNodeWasElement) {
1282
                    //out.print(">");
1283
                }
1284
                previousNodeWasElement = false;
1285
            // Handle the COMMENT nodes
1286
            } else if (currentNode.nodetype.equals("COMMENT")) {
1287
                if (previousNodeWasElement) {
1288
                    //out.print(">");
1289
                }
1290
                previousNodeWasElement = false;
1291

  
1292
            // Handle the PI nodes
1293
            } else if (currentNode.nodetype.equals("PI")) {
1294
                if (previousNodeWasElement) {
1295
                    //out.print(">");
1296
                }
1297
                previousNodeWasElement = false;
1298
            // Handle the DTD nodes (docname, publicid, systemid)
1299
            } else if (currentNode.nodetype.equals(DTD)) {
1300
                // Do nothing
1301
            // Handle any other node type (do nothing)
1302
            } else {
1303
                // Any other types of nodes are not handled.
1304
                // Probably should throw an exception here to indicate this
1305
            }
1306
            //out.flush();
1307
        }
1308

  
1309
        // Print the final end tag for the root element
1310
        while (!openElements.empty()) {
1311
            NodeRecord currentElement = (NodeRecord) openElements.pop();
1312
            util.debugMessage("\n POPPED2: " + currentElement.nodename, 60);
1313
            if (currentElement.nodeprefix != null) {
1314
                //out.print("</" + currentElement.nodeprefix + ":"
1315
                        //+ currentElement.nodename + ">");
1316
            } else {
1317
                //out.print("</" + currentElement.nodename + ">");
1318
            }
1319
        }
1320
        //out.flush();
1321
    }
1322

  
1323
    /** 
1324
     * Recurse up the parent node hierarchy and add each node to the 
1325
     * hashmap of paths to be indexed.
1326
     *
1327
     * @param records the set of records hashed by nodeId
1328
     * @param id the id of the current node to be processed
1329
     * @param children the string representation of all child nodes of this id
1330
     */
1331
    private void traverseParents(HashMap records, long id, String children) {
1332
        NodeRecord current = (NodeRecord)records.get(new Long(id));
1333
        String currentName = current.getNodeName();
1334
        if (current.nodetype.equals("ELEMENT") ||
1335
            current.nodetype.equals("ATTRIBUTE") ) {
1336

  
1337
            System.err.print(currentName +"\n");
1338
            currentName = "/" + currentName;
1339
            System.err.print(currentName +"\n");
1340
            long parentId = current.getParentNodeId();
1341
            currentName = currentName + children;
1342
            if (parentId != 0) {
1343
                traverseParents(records, parentId, currentName);
1344
            }
1345
            if (!children.equals("")) {
1346
                System.err.print(current.getNodeName() + children +"\n");
1347
                System.err.print('/' + current.getNodeName() + children +"\n");
1348
            }
1349
        }
1350
    }
1351

  
1155 1352
    private boolean isRevisionOnly(DocumentIdentifier docid) throws Exception
1156 1353
    {
1157 1354
        //System.out.println("inRevisionOnly");

Also available in: Unified diff