intTypePromotion=1
zunia.vn Tuyển sinh 2024 dành cho Gen-Z zunia.vn zunia.vn
ADSENSE

XML, XSLT, Java, and JSP: A Case Study in Developing a Web Application- P12

Chia sẻ: Thanh Cong | Ngày: | Loại File: PDF | Số trang:50

136
lượt xem
34
download
 
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

XML, XSLT, Java, and JSP: A Case Study in Developing a Web Application- P12: Là một nhà phát triển Web, bạn biết những thách thức trong việc xây dựng các ứng dụng mạnh mẽ trên nhiều nền tảng. Tạo các ứng dụng di động trở nên thật sự có thể bằng cách sử dụng Java cho code và XML để tổ chức và quản lý dữ liệu. "XML, XSLT, Java, và JSP: Một trường hợp học" sẽ giúp bạn tối đa hóa khả năng của XML, XSLT, Java, và JSP trong các ứng dụng web của bạn....

Chủ đề:
Lưu

Nội dung Text: XML, XSLT, Java, and JSP: A Case Study in Developing a Web Application- P12

  1. 532 Appendix C Source Code for bonForum Web Application if(pathToSubTreeRootNode == null || pathToSubTreeRootNode.length() < 1) { pathToSubTreeRootNode = “”; } parentNodeInDestination = parentNodeInDestination.trim(); if(parentNodeInDestination == null || parentNodeInDestination.length() < 1) { parentNodeInDestination = “things”; } // parse document try { DOMParser parser = new DOMParser(); parser.parse(xmlUri); Document document = parser.getDocument(); try { loadForumXML(pathToSubTreeRootNode, parentNodeInDestination, document, “pathNameHashtable”, sessionId); } catch(Exception ee) { log(sessionId, “err”, “caught exception trying to load into bonForumXML: “+ xmlUri); } } catch(Exception ex) { log(sessionId, “err”, “caught exception trying to parse: “ + xmlUri); } } /** Loads XML from URI into “bonBufferXML” ForestHashtable member. * * * @param pathToSubTreeRootNode String * @param xmlUri String * @param nodeKeyHashtableName String * @param sessionId String */ protected void loadBufferXMLFromURI(String pathToSubTreeRootNode, String parentNodeInDestination, String xmlUri, String nodeKeyHashtableName, String sessionId) { pathToSubTreeRootNode = pathToSubTreeRootNode.trim(); if(pathToSubTreeRootNode == null || pathToSubTreeRootNode.length() < 1) { pathToSubTreeRootNode = “”; } parentNodeInDestination = parentNodeInDestination.trim(); if(parentNodeInDestination == null || parentNodeInDestination.length() < 1) { parentNodeInDestination = “things”; } // parse document try {
  2. C.17 Filename: Projects\bonForum\src\de\tarent\forum\BonForumStore.java 533 DOMParser parser = new DOMParser(); parser.parse(xmlUri); Document document = parser.getDocument(); try { loadBufferXML(pathToSubTreeRootNode, parentNodeInDestination, document, “pathNameHashtable”, sessionId); } catch(Exception ee) { log(sessionId, “err”, “caught exception trying to load into bonBufferXML: “+ xmlUri); } } catch(Exception ex) { log(sessionId, “err”, “caught exception trying to parse: “ + xmlUri); } } /** Loads XML from DOM node into “bonForumXML” ForestHashtable member. * * * @param pathToSubTreeRootNode String * @param parentNodeInDestination String * @param node Node * @param nodeKeyHashtableName String * @param sessionId String */ protected void loadForumXML(String pathToSubTreeRootNode, String parentNodeInDestination, Node node, String nodeKeyHashtableName, String sessionId) { Object parentNodeKey = null; String nodeKeyPathName = “”; boolean foundParentNodeKey = true; // assume success if(parentNodeInDestination.equals(“actors”)) { parentNodeKey = (Object)getActorsNodeKey(); } else if(parentNodeInDestination.equals(“actions”)) { parentNodeKey = (Object)getActionsNodeKey(); } else if(parentNodeInDestination.equals(“things”)) { parentNodeKey = (Object)getThingsNodeKey(); } else { if (getBonForumXML().getNodeNameHashtable().containsKey(parentNodeInDestination)) { parentNodeKey = getBonForumXML().getNodeNameHashtable().get(parentNodeInDestination); } else { foundParentNodeKey = false; } }
  3. 534 Appendix C Source Code for bonForum Web Application if(parentNodeKey != null && foundParentNodeKey) { try { // needs test and finishing, especially pathToSubTreeRootNode argument loadXMLSubTreeIntoForestHashtable(node, parentNodeKey, bonForumXML, nodeKeyPathName, nodeKeyHashtableName, sessionId); } catch(Exception ee) { log(sessionId, “err”, “loadForumXML() caught Exception invoking loadXMLSubTreeIntoForestHashtable()”); } } } /** Loads XML from DOM node into the bonBufferXML ForestHashtable. * * * @param pathToSubTreeRootNode String * @param parentNodeInDestination String * @param node Node * @param nodeKeyHashtableName String * @param sessionId String */ protected void loadBufferXML(String pathToSubTreeRootNode, String parentNodeInDestination, Node node, String nodeKeyHashtableName, String sessionId) { Object parentNodeKey = null; String nodeKeyPathName = “”; boolean foundParentNodeKey = true; // assume success if(parentNodeInDestination.equals(“actors”)) { parentNodeKey = (Object)getBufferActorsNodeKey(); } else if(parentNodeInDestination.equals(“actions”)) { parentNodeKey = (Object)getBufferActionsNodeKey(); } else if(parentNodeInDestination.equals(“things”)) { parentNodeKey = (Object)getBufferThingsNodeKey(); } else { if (bonBufferXML.getNodeNameHashtable().containsKey(parentNodeInDestination)) { parentNodeKey = bonBufferXML.getNodeNameHashtable().get(parentNodeInDestination); } else { foundParentNodeKey = false; } } if(parentNodeKey != null && foundParentNodeKey) { try { // needs test and finishing, especially pathToSubTreeRootNode argument
  4. C.17 Filename: Projects\bonForum\src\de\tarent\forum\BonForumStore.java 535 loadXMLSubTreeIntoForestHashtable(node, parentNodeKey, bonBufferXML, nodeKeyPathName, nodeKeyHashtableName, sessionId); } catch(Exception ee) { log(sessionId, “err”, “loadBufferXML() caught Exception invoking loadXMLSubTreeIntoForestHashtable()”); } } } /** Loads the specified node, recursively, into a ForestHashtable. * NOTE: only loads element nodes with attributes, and any text node children. * * @param node Node * @param parentNodeKey Object * @param forestHashtable ForestHashtable * @param nodeKeyPathName String * @param nodeKeyHashtableName String * @param sessionId String */ protected void loadXMLSubTreeIntoForestHashtable(Node node, Object parentNodeKey, ForestHashtable forestHashtable, String nodeKeyPathName, String nodeKeyHashtableName, String sessionId) { String nodeName = “”; String nodeAttributes = “”; String nodeContent = “”; Object nextParentNodeKey = parentNodeKey; String pathName = “”; if ( node == null ) { return; } // the ForestHashtable instance must exist for this method to work int type = node.getNodeType(); switch(type) { // process document node case Node.DOCUMENT_NODE: { loadXMLSubTreeIntoForestHashtable(((Document)node).getDocumentElement(), parentNodeKey, forestHashtable, nodeKeyPathName, nodeKeyHashtableName, sessionId); break; } // process element with attributes, and also get text nodes for content case Node.ELEMENT_NODE: { nodeName = node.getNodeName(); // LATER: here can wait for element named by pathToSubTreeRootNode, ignoring ancestors Attr attrs[] = BonForumUtils.sortAttributes(node.getAttributes()); nodeAttributes = “”; for ( int i = 0; i < attrs.length; i++ ) { Attr attr = attrs[i];
  5. 536 Appendix C Source Code for bonForum Web Application nodeAttributes += (“ “); nodeAttributes += (attr.getNodeName()); nodeAttributes += (“=\””); nodeAttributes += (normalize(attr.getNodeValue())); nodeAttributes += (“\””); } // adds node and gets its nodeKey BonNode bonNode; bonNode = forestHashtable.addChildNodeToNonRootNode(nodeName, nodeAttributes, nodeContent, (NodeKey)parentNodeKey, nodeKeyHashtableName, sessionId); nextParentNodeKey = (Object)forestHashtable.nodeKeyFromBonNode(bonNode); // optionally save nodeKey in a hashtable to use for fast lookups, pathname sorting, etc. if(nodeKeyHashtableName != null && nodeKeyHashtableName != “”) { if(nodeKeyHashtableName.equals(“pathNameHashtable”)) { // here optionally save nodeKey with a pathName key // only save descendants of bonForum.things.subjects if (nodeKeyPathName.equals(“”)) { if( (!(nodeName.equals(“bonForum”))) && (!(nodeName.equals(“things”))) && (!(nodeName.equals(“subjects”))) ) { nodeKeyPathName = nodeName; } } else { // build the pathName by concatenating node just added nodeKeyPathName = nodeKeyPathName + “.” + nodeName; } if(!nodeKeyPathName.equals(“”)) { forestHashtable.getPathNameHashtable().put(nodeKeyPathName, nextParentNodeKey); } } else { // later we can add other types of hashtables here } } // here we get text nodes for our content, and recursively visit element descendants // we are ignoring ENTITY_REFERENCE_NODE, CDATA_SECTION_NODE, PROCESSING_INSTRUCTION_NODE NodeList children = node.getChildNodes(); if ( children != null ) { int len = children.getLength(); for ( int i = 0; i < len; i++ ) { // get text if any, else recursively visit element children
  6. C.17 Filename: Projects\bonForum\src\de\tarent\forum\BonForumStore.java 537 int nodeType = children.item(i).getNodeType(); if(nodeType == Node.TEXT_NODE) { nodeContent += “ “ + normalize(children.item(i).getNodeValue().trim()); } else if(nodeType == Node.ELEMENT_NODE) { // recursion to visit all subnodes loadXMLSubTreeIntoForestHashtable(children.item(i), nextParentNodeKey, forestHashtable, nodeKeyPathName, nodeKeyHashtableName, sessionId); } } } // edit the node to add content from text node nodeContent = nodeContent.trim(); if(nodeContent.length() > 0) { NodeKey nk = getBonForumXML().editBonNode((NodeKey)(nextParentNodeKey), null, null, nodeContent); } break; } // end case Node.ELEMENT_NODE } // end switch } /** Adds a node to “bonForumXML” ForestHashtable member. * * @param command = “bonAddElement” (others could follow) * @param parentNodeKeyKey String * @param nameAndAttributes String * @param content String * @param forestHashtableName String * @param nodeKeyHashtableName String * @param sessionId String * @return Object that can be cast to a NodeKey */ protected Object add(String command, String parentNodeKeyKey, String nameAndAttributes, String content, String forestHashtableName, String nodeKeyHashtableName, String sessionId) { BonNode bonNode = new BonNode(); NodeKey nonRootNodeKey = new NodeKey(); nonRootNodeKey = (NodeKey)addNode(bonNode, nonRootNodeKey, command, parentNodeKeyKey, nameAndAttributes, content, forestHashtableName, nodeKeyHashtableName, sessionId); return (Object)(nonRootNodeKey); } /** Adds a node to “bonBufferXML” ForestHashtable member. * * @param command = “bonAddElement” (others could follow) * @param parentNodeKeyKey String * @param nameAndAttributes String * @param content String
  7. 538 Appendix C Source Code for bonForum Web Application * @param forestHashtableName String * @param nodeKeyHashtableName String * @param sessionId String * @return Object that can be cast to a NodeKey */ protected Object addToBuffer(String command, String parentNodeKeyKey, String nameAndAttributes, String content, String forestHashtableName, String nodeKeyHashtableName, String sessionId) { BonNode bonNode = new BonNode(); NodeKey nonRootNodeKey = new NodeKey(); nonRootNodeKey = (NodeKey)addNode(bonNode, nonRootNodeKey, command, parentNodeKeyKey, nameAndAttributes, content, forestHashtableName, nodeKeyHashtableName, sessionId); return (Object)(nonRootNodeKey); } /** Adds a node to “bonForumXML”, or “bonBufferXML”, or other ForestHashtable. * * @param bonNode BonNode * @param nonRootNodeKey NodeKey * @param command String =”bonAddElement” (others could follow) * @param parentNodeKeyKey String * @param nameAndAttributes String * @param content String * @param forestHashtableName String * @param nodeKeyHashtableName String * @param sessionId String * @return Object that can be cast to a NodeKey */ protected Object addNode(BonNode bonNode, NodeKey nonRootNodeKey, String command, String parentNodeKeyKey, String nameAndAttributes, String content, String forestHashtableName, String nodeKeyHashtableName, String sessionId) { boolean fast = false; ForestHashtable forestHashtable; if(forestHashtableName.equals(“bonForumXML”)) { forestHashtable = bonForumXML; if (parentNodeKeyKey.equals(“actors”)) { nonRootNodeKey = getActorsNodeKey(); fast = true; } else if (parentNodeKeyKey.equals(“actions”)) { nonRootNodeKey = getActionsNodeKey(); fast = true; } else if (parentNodeKeyKey.equals(“things”)) { nonRootNodeKey = getThingsNodeKey(); fast = true; } } else if(forestHashtableName.equals(“bonBufferXML”)) {
  8. C.17 Filename: Projects\bonForum\src\de\tarent\forum\BonForumStore.java 539 forestHashtable = bonBufferXML; if (parentNodeKeyKey.equals(“actors”)) { nonRootNodeKey = getBufferActorsNodeKey(); fast = true; } else if (parentNodeKeyKey.equals(“actions”)) { nonRootNodeKey = getBufferActionsNodeKey(); fast = true; } else if (parentNodeKeyKey.equals(“things”)) { nonRootNodeKey = getBufferThingsNodeKey(); fast = true; } } else { forestHashtable = null; } if (command.equals(“bonAddElement”)) { try { if(!fast) { // not a “fast” parent nodeKey // // There are two possibilities (more later): // // 1. The parentNodeKey is saved in nodeNameHashtable. // The parentNodeKeyKey format has one of two formats: // // The first format is: // sessionID_creationTimeMillis:parentElementName // (e.g., “To1012mC7576871324604071At:985400336097:chat”) // // The second format is: // sessionID:parentElementName // (e.g., “To1012mC7576871324604071At:messageKey”) // // 2. The parentNodeKey is saved in pathNameHashtable. // The parentNodeKeyKey used is the pathName to the // subject node (e.g., “Vehicles.Motorcycles.Triumph”). // // If the parentNodeKeyKey is not “actors”, “actions”, “things”, // then the parentNodeKeyKey is a key into a Hashtable // with the name nodeKeyHashtableName
  9. 540 Appendix C Source Code for bonForum Web Application // if(nodeKeyHashtableName.equals(“pathNameHashtable”)) { if(forestHashtable.getPathNameHashtable().containsKey(parentNodeKeyKey)) { nonRootNodeKey = (NodeKey)forestHashtable.getPathNameHashtable().get(parentNodeKeyKey); } else { log(sessionId, “err”, “add() DID NOT FIND parentNodeKeyKey IN pathNameHashtable: “ + parentNodeKeyKey); } } // The key should be in format // we could later check the incoming sessionId prefix is valid // using isRequestedSessionIdValid(prefix) else if (nodeKeyHashtableName.equals(“nodeNameHashtable”)) { String temp = “” + forestHashtable.getNodeNameHashtable().size(); if (forestHashtable.getNodeNameHashtable().containsKey(parentNodeKeyKey)) { nonRootNodeKey = (NodeKey)forestHashtable.getNodeNameHashtable().get(parentNodeKeyKey); } else { log(sessionId, “err”, “add() DID NOT FIND parentNodeKeyKey IN nodeNameHashtable: “ + parentNodeKeyKey); } } else { log(sessionId, “err”, “UNRECOGNIZED nodeKeyHashtableName in add(): “ + parentNodeKeyKey); } } try { if(nonRootNodeKey != null) { if(nonRootNodeKey.aKey != null && nonRootNodeKey.aKey.length() > 0) { String name = “”; String attributes = “”; if(nameAndAttributes == null) { nameAndAttributes = “”; } int inx = nameAndAttributes.trim().indexOf(‘ ‘); if (inx > -1) { // space between name and attributes name = nameAndAttributes.substring(0, inx);
  10. C.17 Filename: Projects\bonForum\src\de\tarent\forum\BonForumStore.java 541 attributes = nameAndAttributes.substring(inx).trim(); } else { name = nameAndAttributes; } bonNode = forestHashtable.addChildNodeToNonRootNode(name, attributes, content, nonRootNodeKey, nodeKeyHashtableName, sessionId); } } } catch (Exception ex) { log(sessionId, “err”, “addChildNodeToNonRootNode() caused exception in add()”); } } catch (Exception ee) { log(sessionId, “err”, “Exception in add()”); } } return (Object)(bonNode.nodeKey); } /** Removes a (session-dependent) node from “bonForumXML” ForestHashtable member. * @param command String = “bonRemoveElement” (others later) * @param nodeKeyKey String * @param leafOnly String if uppercased is “TRUE” nodes with children not removed * @param forestHashtableName String */ protected void remove(String command, String nodeKeyKey, String leafOnly, String forestHashtableName) { removeNode (command, nodeKeyKey, leafOnly, forestHashtableName); } /** Removes a (session-dependent) node from “bonBufferXML” ForestHashtable member. * @param command String = “bonRemoveElement” (others later) * @param nodeKeyKey String * @param leafOnly String if uppercased is “TRUE” nodes with children not removed * @param forestHashtableName String */ protected void removeFromBuffer(String command, String nodeKeyKey, String leafOnly, String forestHashtableName) { removeNode (command, nodeKeyKey, leafOnly, forestHashtableName); } /** Removes a node from a ForestHashtable. * (for now: only non-global nodes removed) * @param command String = “bonRemoveElement” (others later) * @param nodeKeyKey String
  11. 542 Appendix C Source Code for bonForum Web Application * @param leafOnly String if uppercased is “TRUE” nodes with children not removed * @param forestHashtableName String */ protected void removeNode (String command, String nodeKeyKey, String leafOnly, String forestHashtableName) { NodeKey nonRootNodeKey = new NodeKey(); boolean fast = false; // For now: just a choice between existing ForestHashtable instances ForestHashtable forestHashtable = null; if(forestHashtableName.equals(“bonForumXML”)) { forestHashtable = bonForumXML; if (nodeKeyKey.equals(“actors”)) { nonRootNodeKey = getActorsNodeKey(); fast = true; } else if (nodeKeyKey.equals(“actions”)) { nonRootNodeKey = getActionsNodeKey(); fast = true; } else if (nodeKeyKey.equals(“things”)) { nonRootNodeKey = getThingsNodeKey(); fast = true; } } else if(forestHashtableName.equals(“bonBufferXML”)) { if (nodeKeyKey.equals(“actors”)) { nonRootNodeKey = getBufferActorsNodeKey(); fast = true; } else if (nodeKeyKey.equals(“actions”)) { nonRootNodeKey = getBufferActionsNodeKey(); fast = true; } else if (nodeKeyKey.equals(“things”)) { nonRootNodeKey = getBufferThingsNodeKey(); fast = true; } } NodeKey nodeKey = new NodeKey(); if (command.equals(“bonRemoveElement”)) { try { if(!fast) { // does not have a “fast” global parent nodeKey // For now, only non-fast nodes can be removed! // // Each NodeKey instance is saved in nodeNameHashtable. // “fast” nodes have a nodeKeyKey of // “actors”, “actions”, or “things”, // “non-fast” nodes hava a nodeKeyKey
  12. C.17 Filename: Projects\bonForum\src\de\tarent\forum\BonForumStore.java 543 // (a key into the nodeNameHashtable) // with a key format that is either: // sessionId_creationTimeInMillis:nodeName // or, optionally: // sessionId:nodeName // We could later check the incoming sessionId prefix // is valid using isRequestedSessionIdValid(prefix) if (forestHashtable.getNodeNameHashtable().containsKey(nodeKeyKey)) { nodeKey = (NodeKey)forestHashtable.getNodeNameHashtable().get(nodeKeyKey); } } try { if(nodeKey.aKey != null && nodeKey.aKey.length() > 0) { boolean deleteLeafOnly = false; if(leafOnly.equalsIgnoreCase(“TRUE”)) { deleteLeafOnly = true; } forestHashtable.deleteNode((NodeKey)nodeKey, deleteLeafOnly); } } catch (Exception ex) { log(sessionId, “err”, “deleteNode() caused exception in remove()”); } } catch (Exception ee) { log(sessionId, “err”, “Exception in remove()”); } } } /** Gets the NodeKey for a pathName in a pathNameHashtable. * (The NodeKey for each element in the “subjects” subtree * is also in a pathNameHashtable, with a pathName key). * * @param pathName String * @param pathNameHashtable PathNameHashtable * @return NodeKey for the pathName */ protected NodeKey subjectNodeKeyFromPathName(String pathName, PathNameHashtable pathNameHashtable) { return (NodeKey)pathNameHashtable.get(pathName); } /** Returns the BonNode for a subject from a ForestHashtable. * (The subject pathName is a key value for a NodeKey in a PathNameHashtable). *
  13. 544 Appendix C Source Code for bonForum Web Application * @param pathName String * @param pathNameHashtable PathNameHashtable * @param forestHashtable ForestHashtable * @return NodeKey for the pathName */ protected BonNode subjectBonNodeFromPathName(String pathName, PathNameHashtable pathNameHashtable, ForestHashtable forestHashtable) { NodeKey nodeKey = subjectNodeKeyFromPathName(pathName, pathNameHashtable); if(forestHashtable.getNodeNameHashtable().contains(nodeKey)) { return (BonNode)forestHashtable.get(nodeKey); } else { return(null); } } /** Returns the pathName to a NodeKey in a ForestHashtable. * * @param nodeKey NodeKey * @param forestHashtable ForestHashtable * @return The pathName for the nodeKey as a String */ public String pathNameFromNodeKey(NodeKey nodeKey, ForestHashtable forestHashtable) { String temp = “”; BonNode bonNode; NodeKey parentNodeKey; try { bonNode = forestHashtable.getBonNode(nodeKey); temp = bonNode.nodeName; parentNodeKey = bonNode.parentNodeKey; boolean done = false; while (!done) { if(bonNode.parentNodeKey.aKey.equals(“”)) { done = true; break; } else { bonNode = forestHashtable.getBonNode(parentNodeKey); temp = bonNode.nodeName + “.” + temp; parentNodeKey = bonNode.parentNodeKey; } } } catch(Exception ee) { log(sessionId, “err”, “Exception caught in pathNameFromNodeKey()”); } return temp; }
  14. C.17 Filename: Projects\bonForum\src\de\tarent\forum\BonForumStore.java 545 /** Returns the pathKey to a NodeKey in a ForestHashtable. * A pathKey is made up of the combined nodeKeys of all the nodes from the root * to the nodeKey argument. * Each nodeKey is a triple-valued key used by a ForestHashtable. * An Example of a pathKey is: * 238493049323-384930493039-584954059453.463748293847-564738473623- 827347382374 * The nodeKeys is a pathKey are separated by a period, ‘.’, and the * AKey, BKey and CKey in each nodeKey are separated by a dash, ‘-’. * * @param nodeKey NodeKey * @param forestHashtable ForestHashtable * @return The pathKey for the nodeKey as a String */ public String pathKeyFromNodeKey(NodeKey nodeKey, ForestHashtable forestHashtable) { String temp = “”; BonNode bonNode; NodeKey parentNodeKey; try { bonNode = forestHashtable.getBonNode(nodeKey); temp = bonNode.nodeKey.aKey + “-” + bonNode.nodeKey.bKey + “-” + bonNode.nodeKey.cKey; parentNodeKey = bonNode.parentNodeKey; boolean done = false; while (!done) { bonNode = forestHashtable.getBonNode(parentNodeKey); temp = bonNode.nodeKey.aKey + “-” + bonNode.nodeKey.bKey + “-” + bonNode.nodeKey.cKey + “.” + temp; if(bonNode.parentNodeKey.aKey.equals(“”)) { done = true; break; } else { parentNodeKey = bonNode.parentNodeKey; } } } catch(Exception ee) { log(sessionId, “err”, “Exception caught in pathKeyFromNodeKey()”); } return temp; } /** Outputs pathNames and nodeKeys from a bonForumXML subTree as a TreeMap. * (except for chatItems!). * The TreeMap can be used as a sorted list of the paths to all the nodes. * * NOTE: if you need method to output chatItem elements, * You can add an option arg to do that!
  15. 546 Appendix C Source Code for bonForum Web Application * (chatItem elements have names that are equal to: * “sessionID_” + chatCreatorHostSessionId + chatNodeCreationTimeInMillis), * * @param command String (unused, available argument) * @param pathToSubTreeRootNode String * @param option1 String (reserved argument) * @param option2 String (reserved argument) * @return TreeMap */ protected TreeMap outputForumPathNames(String command, String pathToSubTreeRootNode, String option1, String option2) { // the command argument can later be used to provide tag-visible sub- types of this method if(!command.equals(“bonForumXML”)) { if(command.equals(“bonBufferXML”)) { return outputBufferPathNames(command, pathToSubTreeRootNode, option1, option2); } else { TreeMap errorTreeMap = new TreeMap(); errorTreeMap.put(“0”, “::::::::::::error in command::::::::::::::”); return errorTreeMap; // later this error causes exception? } } log(sessionId, “err”, “Hello, outputForumPathNames!”); BonNode bonNode = null; NodeKey nodeKey = new NodeKey(); TreeMap outputTreeMap = new TreeMap(); Enumeration enumeration = getBonForumXML().elements(); while(enumeration.hasMoreElements()) { bonNode = (BonNode)enumeration.nextElement(); nodeKey = bonNode.nodeKey; // LATER: pass option2 to pathNameFromNodeKey to replace all but last nodeName with nbsp, for example // LATER: pass option3 to pathNameFromNodeKey as the separator String pathName = pathNameFromNodeKey(nodeKey, bonForumXML); // ASSUMPTION: if element has chatTopic attribute it is chatItem, // and we want to suppress these in users display of forum pathNames (e.g., chat subjects) if (pathToSubTreeRootNode != null && pathToSubTreeRootNode.length() > 0) { if(bonNode.nodeAttributes.indexOf(“chatTopic=\””) < 0) { if(pathName.indexOf(pathToSubTreeRootNode) == 0) { // strip off leftmost nodes pathName = pathName.substring(pathToSubTreeRootNode.length()); // strip off period separator
  16. C.17 Filename: Projects\bonForum\src\de\tarent\forum\BonForumStore.java 547 if(pathName.indexOf(“.”) == 0) { pathName = pathName.substring(1); } if(pathName.length() > 0) { outputTreeMap.put(pathName, nodeKey.aKey); } } } } else { if(pathName.length() > 0) { outputTreeMap.put(pathName, nodeKey.aKey); } } } if(outputTreeMap.size()
  17. 548 Appendix C Source Code for bonForum Web Application outputForumPathNames) outputTreeMap.put(pathName, nodeKey.aKey); } if(outputTreeMap.size()
  18. C.17 Filename: Projects\bonForum\src\de\tarent\forum\BonForumStore.java 549 String itemKey = normalize((String)session.getAttribute(“itemKey”)); if(itemKey.trim().length() < 1) { log(sessionId, “err”, “outputForumChatMessages() ERROR: session has no itemKey!”); } else { // get all messages that have this chat’s itemKey while(enumeration.hasMoreElements()) { // get bonNode and its name, attributes and content bonNode = (BonNode)enumeration.nextElement(); String name = bonNode.nodeName; String attributes = bonNode.nodeAttributes; String content = bonNode.nodeContent; // if the bonNode is a message if(name.equals(“message”)) { /* THESE WERE DEBUGGING TESTS FOR getAttributeValue // test value with escaped double quote in it (xml error!) String testStr = getBonForumXML().getAttributeValue(attributes, “type”); //log(sessionId, “”, “outputForumChatMessages(), testStr: “ + testStr); // test attribute value not there (programming error) testStr = getBonForumXML().getAttributeValue(attributes, “notThere”); //log(sessionId, “”, “outputForumChatMessages(), testStr: “ + testStr); // test value without closing double quote (xml error) testStr = getBonForumXML().getAttributeValue(attributes, “test1”); //log(sessionId, “”, “outputForumChatMessages(), testStr: “ + testStr); */ // get itemKey attribute value String messageItemKey = getBonForumXML().getAttributeValue(attributes, “itemKey”); if(messageItemKey == null) { // error, handle later log(sessionId, “err”, “outputForumChatMessages(), ERROR! NO ItemKey found in message attributes!”); continue; } // if its this session’s itemKey, it is also this session’s chat if(messageItemKey.equals(itemKey)) { // get message timeMillis attribute value messageTimeMillis = getBonForumXML().getAttributeValue(attributes, “timeMillis”); if(messageTimeMillis == null) {
  19. 550 Appendix C Source Code for bonForum Web Application // error, handle later log(sessionId, “err”, “outputForumChatMessages(), ERROR! NO timeMillis found in message attributes!”); continue; } } else { continue; } // assume content is “:: “ followed by chatMessage // put content in TreeMap with timeMillis as key tempTreeMap.put(messageTimeMillis, content); } // end if } // end while } // end else // keep track of last message by its time session.setAttribute(“lastMessageTimeMillis”, messageTimeMillis); // should we make sure here that currentTimeMillis is > largest messageTimeMillis? // the TreeMap chatHistoryTreeMap now contains the sorted chat lines // ready to be put into the “output” choiceTag attribute, // in a manner analogous to that use in the “bonOutputTable” choiceTag command. // Here we get a sorted subset of the messages according to session attributes. // This allows user to set size of output and page through messages // messages array gets sorted message times in reverse order String[] messages = (String[]) tempTreeMap.keySet().toArray(new String[0]); // keep track of how many there were at this output time int numberOfMessages = messages.length; String quantity = (String)session.getAttribute(“chatMessagesPageSize”); if (quantity == null) { log(sessionId, “err”, “outputForumChatMessages(), quantity == null”); quantity = “10”; // initialize } int numberPerPage = -1; try { numberPerPage = Integer.parseInt(quantity); } catch (NumberFormatException nFE) { log(sessionId, “err”, “outputForumChatMessages(), cannot parse quantity requested as int: “ + quantity); numberPerPage = 10; } if(numberPerPage > numberOfMessages) { numberPerPage = numberOfMessages; }
  20. C.17 Filename: Projects\bonForum\src\de\tarent\forum\BonForumStore.java 551 int numberOfPages = 0; try { numberOfPages = (numberOfMessages / numberPerPage); if((numberOfMessages % numberPerPage) != 0) { ++numberOfPages; } } catch(Exception ee) { numberOfPages = 0; } session.setAttribute(“chatNumberOfPages”, Integer.toString(numberOfPages)); String pagesFromEnd = (String)session.getAttribute(“pagesFromEnd”); int pagesToSkip = -1; try { pagesToSkip = Integer.parseInt(pagesFromEnd); } catch (NumberFormatException nFE) { log(sessionId, “err”, “outputForumChatMessages(), cannot parse session attribute pagesFromEnd: “ + pagesFromEnd); pagesToSkip = -1; } if(pagesToSkip >= numberOfPages) { pagesToSkip = numberOfPages - 1; } int pageNumber = numberOfPages - pagesToSkip; if(pageNumber > numberOfPages) { pageNumber = numberOfPages; } session.setAttribute(“chatPageNumber”, Integer.toString(pageNumber)); String navigation; try { navigation = (String)session.getAttribute(“chatMessagesNavigator”); if(!navigation.equals(“same”) && !navigation.equals(“first”) && !navigation.equals(“previous”) && !navigation.equals(“next”) && !navigation.equals(“last”)) { navigation = “last”; } } catch(Exception ee) { log(sessionId, “err”, “outputForumChatMessages(), no session attribute chatMessagesNavigator, using \”last\””); navigation = “last”; session.setAttribute(“chatMessagesNavigator”, “last”); } // We have reversed the sort order of messages in tempTreeMap, // therefore these navigation cases will seem to be backwards! // Also, it means that if there are more than one page of messages, // then the first page of messages may be less than full, // while the last will stay full. (This is more convenient, since the
ADSENSE

CÓ THỂ BẠN MUỐN DOWNLOAD

 

Đồng bộ tài khoản
2=>2