KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > bookmarks > BookmarksXMLHandler


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.editor.bookmarks;
21
22 import java.net.MalformedURLException JavaDoc;
23 import java.net.URI JavaDoc;
24 import java.net.URISyntaxException JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Collection JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30 import javax.xml.parsers.DocumentBuilder JavaDoc;
31 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
32 import javax.xml.parsers.ParserConfigurationException JavaDoc;
33 import org.openide.ErrorManager;
34 import org.w3c.dom.DOMException JavaDoc;
35 import org.w3c.dom.Document JavaDoc;
36 import org.w3c.dom.Element JavaDoc;
37 import org.w3c.dom.Node JavaDoc;
38 import org.w3c.dom.NodeList JavaDoc;
39
40 /**
41  * Management of the bookmarks persistence.
42  *
43  * @author Miloslav Metelka
44  * @version 1.00
45  */

46
47 class BookmarksXMLHandler {
48     
49     // Element names
50
static final String JavaDoc EDITOR_BOOKMARKS_NAMESPACE_URI = "http://www.netbeans.org/ns/editor-bookmarks/1"; // NOI18N
51
static final String JavaDoc EDITOR_BOOKMARKS_ELEM = "editor-bookmarks"; // NOI18N
52
private static final String JavaDoc FILE_ELEM = "file"; // NOI18N
53
private static final String JavaDoc URL_ELEM = "url"; // NOI18N
54
private static final String JavaDoc LINE_ELEM = "line"; // NOI18N
55

56     private BookmarksXMLHandler() {
57     }
58
59     public static void loadFileBookmarksMap(
60     FileBookmarksMap fileBookmarksMap, Element JavaDoc bookmarksElem, URL JavaDoc baseURL) {
61         
62         Node JavaDoc fileElem = skipNonElementNode(bookmarksElem.getFirstChild());
63         while (fileElem != null) {
64             assert FILE_ELEM.equals(fileElem.getNodeName());
65             Node JavaDoc urlElem = skipNonElementNode(fileElem.getFirstChild());
66             assert URL_ELEM.equals(urlElem.getNodeName());
67             Node JavaDoc lineElem = skipNonElementNode(urlElem.getNextSibling());
68             int[] lineIndexesArray = new int[1];
69             int lineCount = 0;
70             while (lineElem != null) {
71                 assert LINE_ELEM.equals(lineElem.getNodeName());
72                 // Check whether there is enough space in the line number array
73
if (lineCount == lineIndexesArray.length) {
74                     lineIndexesArray = reallocateIntArray(lineIndexesArray, lineCount, lineCount << 1);
75                 }
76                 // Fetch the line number from the node
77
try {
78                     Node JavaDoc lineElemText = lineElem.getFirstChild();
79                     String JavaDoc lineNumberString = lineElemText.getNodeValue();
80                     int lineNumber = Integer.parseInt(lineNumberString);
81                     lineIndexesArray[lineCount++] = lineNumber;
82                 } catch (DOMException JavaDoc e) {
83                     ErrorManager.getDefault().notify(e);
84                 } catch (NumberFormatException JavaDoc e) {
85                     ErrorManager.getDefault().notify(e);
86                 }
87                 lineElem = skipNonElementNode(lineElem.getNextSibling());
88             }
89             
90             try {
91                 URL JavaDoc url;
92                 try {
93                     Node JavaDoc urlElemText = urlElem.getFirstChild();
94                     String JavaDoc relOrAbsURLString = urlElemText.getNodeValue();
95                     URI JavaDoc uri = new URI JavaDoc(relOrAbsURLString);
96                     if (!uri.isAbsolute() && baseURL != null) { // relative URI
97
url = new URL JavaDoc(baseURL, relOrAbsURLString);
98                     } else { // absolute URL or don't have base URL
99
url = new URL JavaDoc(relOrAbsURLString);
100                     }
101                 } catch (URISyntaxException JavaDoc e) {
102                     ErrorManager.getDefault().notify(e);
103                     url = null;
104                 } catch (MalformedURLException JavaDoc e) {
105                     ErrorManager.getDefault().notify(e);
106                     url = null;
107                 }
108                 
109                 if (url != null) {
110                     if (lineCount != lineIndexesArray.length) {
111                         lineIndexesArray = reallocateIntArray(lineIndexesArray, lineCount, lineCount);
112                     }
113                     fileBookmarksMap.put(new FileBookmarks(url, lineIndexesArray));
114                 }
115             } catch (DOMException JavaDoc e) {
116                 ErrorManager.getDefault().notify(e);
117             }
118
119             fileElem = skipNonElementNode(fileElem.getNextSibling());
120         }
121     }
122     
123     private static Node JavaDoc skipNonElementNode(Node JavaDoc node) {
124         while (node != null && node.getNodeType() != Node.ELEMENT_NODE) {
125             node = node.getNextSibling();
126         }
127         return node;
128     }
129     
130     public static Element JavaDoc saveFileBookmarksMap(
131     FileBookmarksMap fileBookmarksMap, URI JavaDoc baseURI) {
132
133         DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
134         try {
135             DocumentBuilder JavaDoc builder = factory.newDocumentBuilder();
136             Document JavaDoc xmlDoc = builder.newDocument();
137             Element JavaDoc bookmarksElem = xmlDoc.createElementNS(
138                     EDITOR_BOOKMARKS_NAMESPACE_URI, EDITOR_BOOKMARKS_ELEM);
139             for (Iterator JavaDoc it = fileBookmarksMap.all().iterator(); it.hasNext();) {
140                 FileBookmarks bookmarks = (FileBookmarks)it.next();
141                 if (bookmarks.getBookmarkCount() > 0) {
142                     saveFileBookmarks(bookmarks, xmlDoc, bookmarksElem, baseURI);
143                 }
144             }
145             return bookmarksElem;
146
147         } catch (ParserConfigurationException JavaDoc e) {
148             ErrorManager.getDefault().notify(e);
149             return null;
150         }
151     }
152
153     private static void saveFileBookmarks(FileBookmarks fileBookmarks,
154     Document JavaDoc xmlDoc, Element JavaDoc bookmarksElem, URI JavaDoc baseURI) throws DOMException JavaDoc {
155         
156         Element JavaDoc fileElem = xmlDoc.createElementNS(EDITOR_BOOKMARKS_NAMESPACE_URI, FILE_ELEM);
157
158         Element JavaDoc urlElem = xmlDoc.createElementNS(EDITOR_BOOKMARKS_NAMESPACE_URI, URL_ELEM);
159         String JavaDoc relOrAbsURL = fileBookmarks.getURL().toExternalForm();
160         // Possibly relativize the URL
161
if (baseURI != null) {
162             try {
163                 URI JavaDoc absURI = new URI JavaDoc(relOrAbsURL);
164                 URI JavaDoc relURI = baseURI.relativize(absURI);
165                 relOrAbsURL = relURI.toString();
166             } catch (URISyntaxException JavaDoc e) {
167                 ErrorManager.getDefault().notify(e);
168                 // leave the original full URL
169
}
170         }
171
172         urlElem.appendChild(xmlDoc.createTextNode(relOrAbsURL));
173         fileElem.appendChild(urlElem);
174         
175         int bookmarkCount = fileBookmarks.getBookmarkCount();
176         for (int i = 0; i < bookmarkCount; i++) {
177             int bookmarkLine = fileBookmarks.getBookmarkLineIndex(i);
178             Element JavaDoc lineElem = xmlDoc.createElementNS(EDITOR_BOOKMARKS_NAMESPACE_URI, LINE_ELEM);
179             lineElem.appendChild(xmlDoc.createTextNode(Integer.toString(bookmarkLine)));
180             fileElem.appendChild(lineElem);
181         }
182         
183         bookmarksElem.appendChild(fileElem);
184     }
185     
186    static int[] reallocateIntArray(int[] intArray, int count, int newLength) {
187         int[] newIntArray = new int[newLength];
188         System.arraycopy(intArray, 0, newIntArray, 0, count);
189         return newIntArray;
190     }
191     
192 }
193
194
Popular Tags