001: /*
002: * Copyright 2001-2006 C:1 Financial Services GmbH
003: *
004: * This software is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License Version 2.1, as published by the Free Software Foundation.
007: *
008: * This software is distributed in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
011: * Lesser General Public License for more details.
012: *
013: * You should have received a copy of the GNU Lesser General Public
014: * License along with this library; if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
016: */
017:
018: package de.finix.contelligent.client.util.bookmark;
019:
020: import java.io.UnsupportedEncodingException;
021: import java.net.URLDecoder;
022: import java.net.URLEncoder;
023:
024: import javax.swing.tree.DefaultMutableTreeNode;
025:
026: import org.w3c.dom.Attr;
027: import org.w3c.dom.Document;
028: import org.w3c.dom.Element;
029: import org.w3c.dom.NamedNodeMap;
030: import org.w3c.dom.Node;
031: import org.w3c.dom.NodeList;
032:
033: import de.finix.contelligent.client.i18n.Resources;
034:
035: public class BookmarkUtil {
036:
037: /**
038: *
039: *
040: * @param bookmarkFolder
041: *
042: * @return
043: */
044: public static Node convertBookmarkFolderToBookmarkFolderDomNode(
045: BookmarkFolder bookmarkFolder, Document xmlDocument) {
046: // <folder>
047: Element bookmarkFolderNode = (Element) xmlDocument
048: .createElement("folder");
049:
050: // name=""
051: Attr attributName = xmlDocument.createAttribute("name");
052: // before saving, encode name to mask special characters
053: try {
054: String name = URLEncoder.encode(bookmarkFolder.getName(),
055: "UTF-8");
056: attributName.setValue(name);
057: } catch (UnsupportedEncodingException e) {
058: attributName.setValue(bookmarkFolder.getName());
059: }
060: bookmarkFolderNode.setAttributeNode(attributName);
061: // id=""
062: Attr attributID = xmlDocument.createAttribute("id");
063: attributID.setValue(Integer.toString(bookmarkFolder.getId()));
064: bookmarkFolderNode.setAttributeNode(attributID);
065:
066: return bookmarkFolderNode;
067: }
068:
069: /**
070: *
071: *
072: * @param bookmarkFolderNode
073: *
074: * @return
075: */
076: public static BookmarkFolder convertBookmarkFolderDomNodeToBookmarkFolder(
077: Node bookmarkFolderNode) {
078:
079: if (!"folder".equals(bookmarkFolderNode.getNodeName())) {
080: return null;
081: }
082:
083: NamedNodeMap childNodes = bookmarkFolderNode.getAttributes();
084:
085: String name = null;
086: int bid = -1;
087:
088: for (int i = 0; i < childNodes.getLength(); i++) {
089: Node childNode = childNodes.item(i);
090:
091: if ("name".equals(childNode.getNodeName())) {
092: name = childNode.getTextContent();
093: // decode name to eliminate maskings of special characters
094: try {
095: name = URLDecoder.decode(name, "UTF-8");
096: } catch (UnsupportedEncodingException e) {
097: }
098: }
099: if ("id".equals(childNode.getNodeName())) {
100: bid = Integer.parseInt(childNode.getTextContent());
101: }
102: }
103: return new BookmarkFolder(name, bid);
104: }
105:
106: /**
107: *
108: *
109: * @param bookmark
110: *
111: * @return
112: */
113: public static Node convertBookmarkToBookmarkDomNode(
114: Bookmark bookmark, Document xmlDocument) {
115: // <bookmark>
116: Node bookmarkNode = (Node) xmlDocument
117: .createElement("bookmark");
118: // <name>
119: Node nameNode = bookmarkNode.appendChild(xmlDocument
120: .createElement("name"));
121: // before saving, encode name to mask special characters
122: try {
123: String name = URLEncoder
124: .encode(bookmark.getName(), "UTF-8");
125: nameNode.appendChild(xmlDocument.createTextNode(name));
126: } catch (UnsupportedEncodingException e) {
127: nameNode.appendChild(xmlDocument.createTextNode(bookmark
128: .getName()));
129: }
130: // <componentPath>
131: Node componentPathNode = bookmarkNode.appendChild(xmlDocument
132: .createElement("componentPath"));
133: componentPathNode.appendChild(xmlDocument
134: .createTextNode(bookmark.getComponentPath()));
135: // <id>
136: Node idNode = bookmarkNode.appendChild(xmlDocument
137: .createElement("id"));
138: idNode.appendChild(xmlDocument.createTextNode(Integer
139: .toString(bookmark.getId())));
140: // <type>
141: Node typeNode = bookmarkNode.appendChild(xmlDocument
142: .createElement("type"));
143: typeNode.appendChild(xmlDocument.createTextNode(bookmark
144: .getType()));
145:
146: return bookmarkNode;
147: }
148:
149: /**
150: *
151: *
152: * @param bookmarkNode
153: *
154: * @return
155: */
156: public static Bookmark convertBookmarkDomNodeToBookmark(
157: Node bookmarkNode) {
158:
159: if (!"bookmark".equals(bookmarkNode.getNodeName())) {
160: return null;
161: }
162:
163: NodeList childNodes = bookmarkNode.getChildNodes();
164:
165: String componentPath = null;
166: String name = null;
167: int id = -1;
168: String type = null;
169:
170: for (int i = 0; i < childNodes.getLength(); i++) {
171: Node childNode = childNodes.item(i);
172:
173: if ("componentPath".equals(childNode.getNodeName())) {
174: componentPath = childNode.getTextContent();
175: }
176: if ("name".equals(childNode.getNodeName())) {
177: name = childNode.getTextContent();
178: // decode name to eliminate maskings of special characters
179: try {
180: name = URLDecoder.decode(name, "UTF-8");
181: } catch (UnsupportedEncodingException e) {
182: }
183: }
184: if ("id".equals(childNode.getNodeName())) {
185: id = Integer.parseInt(childNode.getTextContent());
186: }
187: if ("type".equals(childNode.getNodeName())) {
188: type = childNode.getTextContent();
189: }
190: }
191:
192: if (id == -1) {
193: id = (int) Math.round(Integer.MAX_VALUE * Math.random()); // TODO id anders ermitteln! schauen welche id's noch frei sind und davon eine vergeben.
194:
195: Node idNode = bookmarkNode.appendChild(BookmarkManager
196: .getInstance().getBookmarks().createElement("id"));
197: idNode.appendChild(BookmarkManager.getInstance()
198: .getBookmarks()
199: .createTextNode(Integer.toString(id)));
200: }
201:
202: return new Bookmark(componentPath, name, id, type);
203: }
204:
205: /**
206: *
207: *
208: * @param bookmarkFolder
209: *
210: * @return
211: */
212: public static DefaultMutableTreeNode convertBookmarkFolderToBookmarkFolderJTreeNode(
213: BookmarkFolder bookmarkFolder) {
214:
215: DefaultMutableTreeNode treeNode = new DefaultMutableTreeNode(
216: bookmarkFolder.getName());
217:
218: return treeNode;
219: }
220:
221: /**
222: * Creates a JTree of BookmarkFolders and Bookmarks out of the DOM Tree which contains BookmarkFolders and Bookmarks.
223: *
224: * @param document The DOM Tree which contains BookmarkFolders and Bookmarks.
225: * @param foldersOnly Convert and add only bookmarkFolders into JTree? If <code>false</code> also bookmarks will be added.
226: *
227: * @return JTree of of BookmarkFolders and Bookmarks.
228: */
229: public static BookmarkTree convertDomTreeToJTree(Document document,
230: boolean foldersOnly) {
231:
232: String name = Resources.getLocalStringForPackage("bookmarks",
233: "languages.language");
234: int id = -1; // -1 = root node
235:
236: DefaultMutableTreeNode root = new DefaultMutableTreeNode(name);
237: BookmarkFolder bookmarkFolder = new BookmarkFolder(name, id);
238: root.setUserObject(bookmarkFolder);
239:
240: convertDomTreeToJTreeRecursive(document.getDocumentElement(),
241: root, foldersOnly);
242: BookmarkTree jTree = new BookmarkTree(root);
243:
244: return jTree;
245: }
246:
247: /**
248: * This method visits all the nodes in a DOM tree and creates the hierarchical structure of bookmarkFolders and bookmarks and adds them below the root/parent node.
249: *
250: * @param node
251: * @param root
252: * @param foldersOnly Convert and add only bookmarkFolders into JTree? If <code>false</code> also bookmarks will be added.
253: */
254: private static void convertDomTreeToJTreeRecursive(Node node,
255: DefaultMutableTreeNode parent, boolean foldersOnly) {
256:
257: // If node has any children, visit each one
258: NodeList list = node.getChildNodes();
259:
260: for (int i = 0; i < list.getLength(); i++) {
261: // Get child node
262: Node childNode = list.item(i);
263:
264: if (childNode.getNodeType() == Node.ELEMENT_NODE) {
265:
266: if ("folder".equals(childNode.getNodeName())) {
267:
268: NamedNodeMap attributNodes = childNode
269: .getAttributes();
270:
271: String name = null;
272: int id = -1;
273:
274: DefaultMutableTreeNode folder = null;
275:
276: // get the value of attribute name as folder name
277: for (int j = 0; j < attributNodes.getLength(); j++) {
278: Node attributNode = attributNodes.item(j);
279:
280: if ("name".equals(attributNode.getNodeName())) {
281: name = attributNode.getTextContent();
282:
283: // decode name to eliminate maskings of special characters
284: try {
285: name = URLDecoder.decode(name, "UTF-8");
286: } catch (UnsupportedEncodingException e) {
287: }
288: }
289:
290: if ("id".equals(attributNode.getNodeName())) {
291: id = Integer.parseInt(attributNode
292: .getTextContent());
293: }
294: }
295: folder = new DefaultMutableTreeNode(name);
296: BookmarkFolder bookmarkFolder = new BookmarkFolder(
297: name, id);
298: folder.setUserObject(bookmarkFolder);
299:
300: parent.add(folder);
301:
302: convertDomTreeToJTreeRecursive(childNode, folder,
303: foldersOnly);
304:
305: continue;
306: }
307:
308: if (!foldersOnly) {
309: if ("bookmark".equals(childNode.getNodeName())) {
310:
311: NodeList attributNodes = childNode
312: .getChildNodes();
313:
314: String componentPath = null;
315: String name = null;
316: int id = -1;
317: String type = null;
318:
319: DefaultMutableTreeNode bookmarkNode = null;
320:
321: // get the value of child node name as bookmark name
322: for (int j = 0; j < attributNodes.getLength(); j++) {
323: Node attributNode = attributNodes.item(j);
324:
325: if ("componentPath".equals(attributNode
326: .getNodeName())) {
327: componentPath = attributNode
328: .getTextContent();
329: }
330:
331: if ("name".equals(attributNode
332: .getNodeName())) {
333: name = attributNode.getTextContent();
334:
335: // decode name to eliminate maskings of special characters
336: try {
337: name = URLDecoder.decode(name,
338: "UTF-8");
339: } catch (UnsupportedEncodingException e) {
340: }
341: }
342:
343: if ("id".equals(attributNode.getNodeName())) {
344: id = Integer.parseInt(attributNode
345: .getTextContent());
346: }
347:
348: if ("type".equals(childNode.getNodeName())) {
349: type = childNode.getTextContent();
350: }
351: }
352: bookmarkNode = new DefaultMutableTreeNode(name);
353: Bookmark bookmark = new Bookmark(componentPath,
354: name, id, type);
355: bookmarkNode.setUserObject(bookmark);
356:
357: parent.add(bookmarkNode);
358:
359: convertDomTreeToJTreeRecursive(childNode,
360: bookmarkNode, foldersOnly);
361:
362: continue;
363: }
364: }
365: }
366: }
367: }
368: }
|