1 4 package org.openedit.links; 5 6 import java.io.Reader ; 7 import java.io.StringWriter ; 8 import java.io.Writer ; 9 import java.util.Iterator ; 10 11 import org.dom4j.Document; 12 import org.dom4j.DocumentException; 13 import org.dom4j.DocumentHelper; 14 import org.dom4j.Element; 15 import org.dom4j.io.OutputFormat; 16 import org.dom4j.io.SAXReader; 17 import org.dom4j.io.XMLWriter; 18 19 import com.openedit.OpenEditException; 20 import com.openedit.page.Page; 21 import com.openedit.page.manage.PageManager; 22 import com.openedit.util.FileUtils; 23 import com.openedit.util.XmlUtil; 24 25 29 public class XmlLinkLoader 30 { 31 protected PageManager fieldPageManager; 32 33 34 public LinkTree loadLinks(Page inPage) throws OpenEditException 35 { 36 LinkTree tree = new LinkTree(); 38 39 Element root = new XmlUtil().getXml(inPage.getReader(),inPage.getCharacterEncoding()); 40 readElementsInto(root, tree, inPage); 41 return tree; 42 } 43 44 49 protected void readElementsInto(Element element, LinkTree inTree, Page inPage) throws OpenEditException 50 { 51 for (Iterator iter = element.elementIterator("a"); iter.hasNext();) 52 { 53 Element child = (Element) iter.next(); 54 String parentId = element.attributeValue("id"); 55 Link link = readElement(child, inPage); 56 57 inTree.addLink( parentId, link ); 58 readElementsInto(child, inTree, inPage); 59 } 60 } 61 62 protected Link readElement(Element element, Page inPage) throws OpenEditException 63 { 64 String id = element.attributeValue("id"); 65 String userdata = element.attributeValue("userdata"); 66 Link link = new Link(); 67 link.setId(id); 68 link.setUserData(userdata); 69 link.setText(element.attributeValue("text")); 70 71 String href = element.attributeValue("href"); 72 href = inPage.getPageSettings().replaceProperty(href); 73 link.setPath(href); 74 link.setRedirectPath(element.attributeValue("redirectpath")); 75 link.setAccessKey(element.attributeValue("accesskey")); 76 link.setAutoLoadChildren(Boolean.parseBoolean(element.attributeValue("autoloadchildren"))); 77 78 checkLink(element, link); 79 return link; 80 } 81 82 protected void checkLink(Element inElement, Link inLink) throws OpenEditException 83 { 84 } 86 87 97 public String saveLinks(LinkTree inTree, String inEncoding) throws Exception 98 { 99 StringWriter out = new StringWriter (); 100 saveLinks(inTree, out, inEncoding); 101 return out.toString(); 102 } 103 104 public void saveLinks(LinkTree inTree, Writer inXmlOutput, String inEncoding) throws OpenEditException 105 { 106 Document doc = DocumentHelper.createDocument(); 107 Element root = DocumentHelper.createElement("linktree"); 108 if (inTree.getRootLink() != null) 113 { 114 readLinkInto(inTree.getRootLink(), root); 115 } 116 doc.setRootElement(root); 117 118 OutputFormat format = OutputFormat.createPrettyPrint(); 119 format.setEncoding(inEncoding); 120 try 121 { 122 new XMLWriter(inXmlOutput, format).write(doc); 123 } 124 catch (Exception ex) 125 { 126 throw new OpenEditException(ex); 127 } 128 finally 129 { 130 FileUtils.safeClose(inXmlOutput); 131 } 132 } 133 134 138 private void readLinkInto(Link inLink, Element inElement) 139 { 140 Element element = inElement.addElement("a"); 141 element.addAttribute("href", inLink.getHref()); 142 element.addAttribute("userdata", inLink.getUserData()); 143 element.addAttribute("id", inLink.getId()); 144 element.addAttribute("text", inLink.getText()); 145 element.addAttribute( "redirectpath", inLink.getRedirectPath()); 146 if( inLink.isAutoLoadChildren()) 147 { 148 element.addAttribute( "autoloadchildren", "true"); 149 } 150 if (inLink.hasChildren()) 151 { 152 for (Iterator iter = inLink.getChildren().iterator(); iter.hasNext();) 153 { 154 Link link = (Link) iter.next(); 155 readLinkInto(link, element); 156 } 157 } 158 } 159 160 public PageManager getPageManager() 161 { 162 return fieldPageManager; 163 } 164 165 public void setPageManager(PageManager inPageManager) 166 { 167 fieldPageManager = inPageManager; 168 } 169 170 } 171 | Popular Tags |