lenya/src/java/org/apache/lenya/cms/publication DefaultSiteTree.java,1.1,1.2

Christian Egli <[email protected]>
Newsgroups gmane.comp.cms.wyona.cvs
Message-ID <[email protected]>
Update of /repository/lenya/src/java/org/apache/lenya/cms/publication
In directory erbium:/tmp/cvs-serv16675/src/java/org/apache/lenya/cms/publication

Modified Files:
	DefaultSiteTree.java 
Log Message:
Can't get dom4j to work with namespaces. For that reason dom4j is now
gone, now longer is the parent node found with xpath but with a simple
and straight-forward recursive traversal. All the other xml
manipulations are now done with org.w3c.dom.* and the Document- and
NamespaceHelper classes.


Index: DefaultSiteTree.java
===================================================================
RCS file: /repository/lenya/src/java/org/apache/lenya/cms/publication/DefaultSiteTree.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** DefaultSiteTree.java	7 May 2003 07:45:16 -0000	1.1
--- DefaultSiteTree.java	7 May 2003 16:44:57 -0000	1.2
***************
*** 48,51 ****
--- 48,53 ----
  import java.util.List;
  import java.util.Map;
+ import java.util.HashMap;
+ import java.util.ArrayList;
  import java.util.Iterator;
  import java.util.StringTokenizer;
***************
*** 55,83 ****
  import java.io.IOException;
  
! import org.dom4j.Document;
! import org.dom4j.DocumentHelper;
! import org.dom4j.DocumentException;
! import org.dom4j.Element;
! import org.dom4j.XPath;
! import org.dom4j.io.SAXReader;
  
  
  
  public class DefaultSiteTree
      implements SiteTree {
      static Category log = Category.getInstance(DefaultSiteTree.class);
  
!     private Document doc = null;
      private File treefile = null;
  
!     public DefaultSiteTree(String treefilename) throws DocumentException {
   	this(new File(treefilename));
- 	log.debug(treefilename);
      }
  
!     public DefaultSiteTree(File treefile) throws DocumentException {
          // Read tree
  	this.treefile = treefile;
!         Document doc = new SAXReader().read("file:" + treefile);
      }
  
--- 57,97 ----
  import java.io.IOException;
  
! import org.w3c.dom.Document;
! import org.w3c.dom.Element;
! import org.w3c.dom.Node;
! import org.w3c.dom.NodeList;
! import org.w3c.dom.NamedNodeMap;
  
+ import org.xml.sax.SAXException;
+ import javax.xml.parsers.ParserConfigurationException;
+ import javax.xml.transform.TransformerConfigurationException;
+ import javax.xml.transform.TransformerConfigurationException;
+ import javax.xml.transform.TransformerException;
+ import java.io.IOException;
  
+ import org.apache.lenya.xml.DocumentHelper;
+ import org.apache.lenya.xml.NamespaceHelper;
  
  public class DefaultSiteTree
      implements SiteTree {
      static Category log = Category.getInstance(DefaultSiteTree.class);
+     
+     public static final String NAMESPACE_URI = "http://www.lenya.org/2003/sitetree";
+     public static final String NODE_NAME = "node";
+     public static final String LABEL_NAME = "label";
  
!     private Document document = null;
      private File treefile = null;
  
!     public DefaultSiteTree(String treefilename)
! 	throws ParserConfigurationException, SAXException, IOException {
   	this(new File(treefilename));
      }
  
!     public DefaultSiteTree(File treefile)
! 	throws ParserConfigurationException, SAXException, IOException {
          // Read tree
  	this.treefile = treefile;
! 	document = DocumentHelper.readDocument(treefile);
      }
  
***************
*** 86,154 ****
      }
  
      public void addNode(String parentid, String id, Label[] labels,
  			String href, String suffix, boolean link) {
          // Get parent element
          StringTokenizer st = new StringTokenizer(parentid, "/");
!         String xpath_string = "/site"; // Trunk of tree
! 	
!         while (st.hasMoreTokens()) {
!             xpath_string = xpath_string + "/node[@id='" + st.nextToken() + "']";
!         }
  	
!         log.debug("XPATH: " + xpath_string);
  	
!         XPath xpathSelector = DocumentHelper.createXPath(xpath_string);
!         List nodes = xpathSelector.selectNodes(doc);
  	
!         if (nodes.isEmpty()) {
!             log.error(".act(): No nodes: " + xpath_string);
!             log.error(".act(): No child added!");
  	    
              return;
          }
  	
!         Element parent_element = (Element) nodes.get(0);
!         log.debug("PARENT ELEMENT: " + parent_element.getPath());
  	
!         // Check if child already exists
!         String newChildXPath = xpath_string + "/" + "node";
!         log.debug("CHECK: " + newChildXPath);
  	
!         if (doc.selectSingleNode(newChildXPath + "[@id='" + id + "']") != null) {
!             log.error("Exception: XPath exists: " + newChildXPath + "[@id='" + id + "']");
!             log.error("No child added");
  	    
!             return;
!         }
  
          // Add node
!         Element node = parent_element.addElement("node").addAttribute("id", id);
! 	if (href != null && href.length() > 0) {
! 	    node.addAttribute("href", href);
! 	}
  	if (suffix != null && suffix.length() > 0) {
! 	    node.addAttribute("suffix", suffix);
  	}
  	if (link == true) {
! 	    node.addAttribute("link", "true");
  	}
  	for (int i = 0; i < labels.length; i++) {
  	    String labelName = labels[i].getLabel();
! 	    node.addElement("label").setText(labelName);
  	    String labelLanguage = labels[i].getLanguage();
  	    if (labelLanguage != null && labelLanguage.length() > 0) {
! 		node.addAttribute("xml:lang", labelLanguage);
  	    }
  	}
! 	log.debug("Tree has been modified: " + doc.asXML());
      }
  
      public void deleteNode(String id) {}
  
!     public void serialize() throws IOException {
!         // Write the tree
!         FileWriter fileWriter = new FileWriter(treefile);
!         doc.write(fileWriter);
!         fileWriter.close();
      }
  }
--- 100,211 ----
      }
  
+     protected Node findNode(Node node, List ids) {
+ 	if (ids.size() < 1) {
+ 	    return node;
+ 	} else {
+ 	    NodeList nodes = node.getChildNodes();
+ 	    for (int i = 0; i < nodes.getLength(); i++) {
+ 		NamedNodeMap attributes = nodes.item(i).getAttributes();
+ 		if (attributes != null) {
+ 		    Node idAttribute = attributes.getNamedItem("id");
+ 		    if (idAttribute != null && idAttribute.getNodeValue().equals(ids.get(0))) {
+ 			return findNode(nodes.item(i), ids.subList(1, ids.size()));
+ 		    }
+ 		}
+ 	    }
+ 	}
+ 	// node wasn't found
+ 	return null;
+     }
+ 
      public void addNode(String parentid, String id, Label[] labels,
  			String href, String suffix, boolean link) {
          // Get parent element
          StringTokenizer st = new StringTokenizer(parentid, "/");
! 	ArrayList ids = new ArrayList();
! 	while (st.hasMoreTokens()) {
! 	    ids.add(st.nextToken());
! 	}
! 
! 	Element root = document.getDocumentElement();
  	
! 	NamespaceHelper helper = new NamespaceHelper(NAMESPACE_URI, "", document);
  	
! 	Element elements[] = helper.getChildren(root);
  	
! 	Node parentNode = findNode(root, ids);
! 
!         if (parentNode == null) {
!             log.error("No nodes: " + parentid + ". No child added");
  	    
              return;
          }
  	
! 	log.debug("PARENT ELEMENT: " + parentNode);
! 	System.out.println("Parent node " + parentNode);
  	
! //         // Check if child already exists
! //         String newChildXPath = xpath_string + "/" + "node";
! //         log.debug("CHECK: " + newChildXPath);
  	
! //         if (doc.selectSingleNode(newChildXPath + "[@id='" + id + "']") != null) {
! //             log.error("Exception: XPath exists: " + newChildXPath + "[@id='" + id + "']");
! //             log.error("No child added");
  	    
! //             return;
! //         }
  
          // Add node
! 	Element child = helper.createElement(NODE_NAME);
! 	child.setAttribute("id", id);
!  	if (href != null && href.length() > 0) {
!  	    child.setAttribute("href", href);
!  	}
  	if (suffix != null && suffix.length() > 0) {
! 	    child.setAttribute("suffix", suffix);
  	}
  	if (link == true) {
! 	    child.setAttribute("link", "true");
  	}
  	for (int i = 0; i < labels.length; i++) {
  	    String labelName = labels[i].getLabel();
! 	    Element label = helper.createElement(LABEL_NAME, labelName);
  	    String labelLanguage = labels[i].getLanguage();
  	    if (labelLanguage != null && labelLanguage.length() > 0) {
! 		label.setAttribute("xml:lang", labelLanguage);
  	    }
+ 	    child.appendChild(label);
  	}
! 
! 	parentNode.appendChild(child);
! 	log.debug("Tree has been modified: " + root);
      }
  
      public void deleteNode(String id) {}
  
!     public void serialize()
! 	throws IOException,
! 	       TransformerConfigurationException,
! 	       TransformerException  {
! 	DocumentHelper.writeDocument(document, treefile);
!     }
! 
!     public static void main(String[] args) {
! 	try {
! 	    DefaultSiteTree sitetree = new DefaultSiteTree(args[0]);
! 	    Label label = new Label("Foo", null);
! 	    Label[] labels = { label };
! 	    
! 	    sitetree.addNode("/tutorial", "foo", labels);
! 	    
! 	    Label label_de = new Label("Qualität", "de");
! 	    Label label_en = new Label("Quality", "en");
! 	    Label[] labels2 = { label_de, label_en  };
! 	    sitetree.addNode("/tutorial/features", "here", labels2);
! 	    
! 	    sitetree.serialize();
! 	} catch (Exception e) {
! 	    e.printStackTrace();
! 	}
      }
  }
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.