KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openedit > links > XmlLinkLoader


1 /*
2  * Created on Dec 23, 2004
3  */

4 package org.openedit.links;
5
6 import java.io.Reader JavaDoc;
7 import java.io.StringWriter JavaDoc;
8 import java.io.Writer JavaDoc;
9 import java.util.Iterator JavaDoc;
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 /**
26  * @author cburkey
27  *
28  */

29 public class XmlLinkLoader
30 {
31     protected PageManager fieldPageManager;
32     
33
34     public LinkTree loadLinks(Page inPage) throws OpenEditException
35     {
36         //take in this XML file and use it to sync up to root node and below
37
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     /**
45      * @param inRoot
46      * @param inTree
47      * @throws OpenEditException
48      */

49     protected void readElementsInto(Element element, LinkTree inTree, Page inPage) throws OpenEditException
50     {
51         for (Iterator JavaDoc iter = element.elementIterator("a"); iter.hasNext();)
52         {
53             Element child = (Element) iter.next();
54             String JavaDoc 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 JavaDoc id = element.attributeValue("id");
65         String JavaDoc 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 JavaDoc 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         //may be overriden
85
}
86
87     /**
88      * Returns the link information as an XML string in the specified encoding.
89      *
90      * @param inTree The link tree
91      * @param inEncoding The encoding
92      *
93      * @return An XML document representing the link tree
94      *
95      * @throws Exception
96      */

97     public String JavaDoc saveLinks(LinkTree inTree, String JavaDoc inEncoding) throws Exception JavaDoc
98     {
99         StringWriter JavaDoc out = new StringWriter JavaDoc();
100         saveLinks(inTree, out, inEncoding);
101         return out.toString();
102     }
103
104     public void saveLinks(LinkTree inTree, Writer JavaDoc inXmlOutput, String JavaDoc inEncoding) throws OpenEditException
105     {
106         Document doc = DocumentHelper.createDocument();
107         Element root = DocumentHelper.createElement("linktree");
108 // root.addAttribute("textprefix", inTree.getTextPrefix());
109
// root.addAttribute("textpostfix", inTree.getTextPostfix());
110
// root.addAttribute("linkprefix", inTree.getLinkPrefix());
111
// root.addAttribute("linkpostfix", inTree.getLinkPostfix());
112
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 JavaDoc ex)
125         {
126             throw new OpenEditException(ex);
127         }
128         finally
129         {
130             FileUtils.safeClose(inXmlOutput);
131         }
132     }
133
134     /**
135      * @param inTree
136      * @param inRoot
137      */

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 JavaDoc 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