1 19 20 package org.netbeans.modules.editor.bookmarks; 21 22 import java.net.MalformedURLException ; 23 import java.net.URI ; 24 import java.net.URISyntaxException ; 25 import java.net.URL ; 26 import java.util.ArrayList ; 27 import java.util.Collection ; 28 import java.util.Iterator ; 29 import java.util.List ; 30 import javax.xml.parsers.DocumentBuilder ; 31 import javax.xml.parsers.DocumentBuilderFactory ; 32 import javax.xml.parsers.ParserConfigurationException ; 33 import org.openide.ErrorManager; 34 import org.w3c.dom.DOMException ; 35 import org.w3c.dom.Document ; 36 import org.w3c.dom.Element ; 37 import org.w3c.dom.Node ; 38 import org.w3c.dom.NodeList ; 39 40 46 47 class BookmarksXMLHandler { 48 49 static final String EDITOR_BOOKMARKS_NAMESPACE_URI = "http://www.netbeans.org/ns/editor-bookmarks/1"; static final String EDITOR_BOOKMARKS_ELEM = "editor-bookmarks"; private static final String FILE_ELEM = "file"; private static final String URL_ELEM = "url"; private static final String LINE_ELEM = "line"; 56 private BookmarksXMLHandler() { 57 } 58 59 public static void loadFileBookmarksMap( 60 FileBookmarksMap fileBookmarksMap, Element bookmarksElem, URL baseURL) { 61 62 Node fileElem = skipNonElementNode(bookmarksElem.getFirstChild()); 63 while (fileElem != null) { 64 assert FILE_ELEM.equals(fileElem.getNodeName()); 65 Node urlElem = skipNonElementNode(fileElem.getFirstChild()); 66 assert URL_ELEM.equals(urlElem.getNodeName()); 67 Node 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 if (lineCount == lineIndexesArray.length) { 74 lineIndexesArray = reallocateIntArray(lineIndexesArray, lineCount, lineCount << 1); 75 } 76 try { 78 Node lineElemText = lineElem.getFirstChild(); 79 String lineNumberString = lineElemText.getNodeValue(); 80 int lineNumber = Integer.parseInt(lineNumberString); 81 lineIndexesArray[lineCount++] = lineNumber; 82 } catch (DOMException e) { 83 ErrorManager.getDefault().notify(e); 84 } catch (NumberFormatException e) { 85 ErrorManager.getDefault().notify(e); 86 } 87 lineElem = skipNonElementNode(lineElem.getNextSibling()); 88 } 89 90 try { 91 URL url; 92 try { 93 Node urlElemText = urlElem.getFirstChild(); 94 String relOrAbsURLString = urlElemText.getNodeValue(); 95 URI uri = new URI (relOrAbsURLString); 96 if (!uri.isAbsolute() && baseURL != null) { url = new URL (baseURL, relOrAbsURLString); 98 } else { url = new URL (relOrAbsURLString); 100 } 101 } catch (URISyntaxException e) { 102 ErrorManager.getDefault().notify(e); 103 url = null; 104 } catch (MalformedURLException 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 e) { 116 ErrorManager.getDefault().notify(e); 117 } 118 119 fileElem = skipNonElementNode(fileElem.getNextSibling()); 120 } 121 } 122 123 private static Node skipNonElementNode(Node node) { 124 while (node != null && node.getNodeType() != Node.ELEMENT_NODE) { 125 node = node.getNextSibling(); 126 } 127 return node; 128 } 129 130 public static Element saveFileBookmarksMap( 131 FileBookmarksMap fileBookmarksMap, URI baseURI) { 132 133 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 134 try { 135 DocumentBuilder builder = factory.newDocumentBuilder(); 136 Document xmlDoc = builder.newDocument(); 137 Element bookmarksElem = xmlDoc.createElementNS( 138 EDITOR_BOOKMARKS_NAMESPACE_URI, EDITOR_BOOKMARKS_ELEM); 139 for (Iterator 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 e) { 148 ErrorManager.getDefault().notify(e); 149 return null; 150 } 151 } 152 153 private static void saveFileBookmarks(FileBookmarks fileBookmarks, 154 Document xmlDoc, Element bookmarksElem, URI baseURI) throws DOMException { 155 156 Element fileElem = xmlDoc.createElementNS(EDITOR_BOOKMARKS_NAMESPACE_URI, FILE_ELEM); 157 158 Element urlElem = xmlDoc.createElementNS(EDITOR_BOOKMARKS_NAMESPACE_URI, URL_ELEM); 159 String relOrAbsURL = fileBookmarks.getURL().toExternalForm(); 160 if (baseURI != null) { 162 try { 163 URI absURI = new URI (relOrAbsURL); 164 URI relURI = baseURI.relativize(absURI); 165 relOrAbsURL = relURI.toString(); 166 } catch (URISyntaxException e) { 167 ErrorManager.getDefault().notify(e); 168 } 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 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 |