1. Trang chủ
  2. » Công Nghệ Thông Tin

Tài liệu XML, XSLT, Java, and JSP: A Case Study in Developing a Web Application- P12 pdf

50 337 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 50
Dung lượng 165,92 KB

Nội dung

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 { 15 1089-9 XC 6/26/01 7:40 AM Page 532 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 533 C.17 Filename: Projects\bonForum\src\de\tarent\forum\BonForumStore.java 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; } } 15 1089-9 XC 6/26/01 7:40 AM Page 533 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 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 15 1089-9 XC 6/26/01 7:40 AM Page 534 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 535 C.17 Filename: Projects\bonForum\src\de\tarent\forum\BonForumStore.java 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]; 15 1089-9 XC 6/26/01 7:40 AM Page 535 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 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 15 1089-9 XC 6/26/01 7:40 AM Page 536 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 537 C.17 Filename: Projects\bonForum\src\de\tarent\forum\BonForumStore.java 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 15 1089-9 XC 6/26/01 7:40 AM Page 537 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 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”)) { 15 1089-9 XC 6/26/01 7:40 AM Page 538 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 539 C.17 Filename: Projects\bonForum\src\de\tarent\forum\BonForumStore.java 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 15 1089-9 XC 6/26/01 7:40 AM Page 539 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 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 <sessionId:nodeName> 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); 15 1089-9 XC 6/26/01 7:40 AM Page 540 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 541 C.17 Filename: Projects\bonForum\src\de\tarent\forum\BonForumStore.java 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 15 1089-9 XC 6/26/01 7:40 AM Page 541 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. [...]... “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)... (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) * Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark 15 1089-9 XC 6/26/01 7:40 AM Page 544 544 Appendix C Source Code for bonForum Web Application * @param pathName String * @param pathNameHashtable 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);... chat actorKeys = getGuestKeysInChat(chatNodeKeyKey); } else { // actor is host, get list of hosts in chat actorKeys = getHostKeysInChat(chatNodeKeyKey); } // chatActor strings contain actorNickname, age:actorAge and rating:actorRating // Here is an example: // John Doe age:12 rating:5 // NOTE THIS DEPENDS ON XSL DOCUMENT RIGHT NOW! // strip off the age and rating int inx = chatActor.lastIndexOf(“age:”);... outputTreeMap; } /** Outputs the messages in the session’s current chat from bonForumXML as a TreeMap * The TreeMap can be used as a sorted list of the messages for that session’s chat * * @param command String (reserved argument, available from ChoiceTag) * @param option1 String (reserved argument, available from ChoiceTag) * @param option2 String (reserved argument, available from ChoiceTag) * @param option3... the messages for that session’s chat * * @param command String (reserved argument, available from ChoiceTag) * @param option1 String (reserved argument, available from ChoiceTag) * @param option2 String (reserved argument, available from ChoiceTag) * @param option3 String (reserved argument, available from ChoiceTag) * @param session HttpSession (e.g., pageContext.getSession()) * @return TreeMap */ protected... argument, available from ChoiceTag) * @param pathToSubTreeRootNode String * @param option1 String (reserved argument, available from ChoiceTag) * @param option2 String (reserved argument, available from ChoiceTag) * @return TreeMap */ protected TreeMap outputBufferPathNames(String command, String pathToSubTreeRootNode, String option1, String option2) { // the command argument can later be used to provide tag-visible... (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”, “outputBufferChatMessages(), exception getting session attribute chatMessagesNavigator, using \”last\””); navigation = “last”; } Please... You can add an option arg to do that! Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark 15 1089-9 XC 6/26/01 7:40 AM Page 546 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... bonBufferXML IS READY>>>”); } } } /** Adds one attribute (name=value) to a chat node in bonForumXML ForestHashtable * * @param chatNodeKeyKey String (“::chat”) * @param attrName String * @param attrValue String * * @return NodeKey of chat node affected, or null if key no good */ public NodeKey addChatNodeAttribute(String chatNodeKeyKey, String attrName, String attrValue) { . each element in the “subjects” subtree * is also in a pathNameHashtable, with a pathName key). * * @param pathName String * @param pathNameHashtable PathNameHashtable. Code for bonForum Web Application * @param pathName String * @param pathNameHashtable PathNameHashtable * @param forestHashtable ForestHashtable * @return

Ngày đăng: 24/12/2013, 07:17

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN