| | 17 | import javax.xml.parsers.DocumentBuilder; |
|---|
| | 18 | import javax.xml.parsers.DocumentBuilderFactory; |
|---|
| | 19 | import javax.xml.parsers.ParserConfigurationException; |
|---|
| | 20 | import javax.xml.transform.OutputKeys; |
|---|
| | 21 | import javax.xml.transform.Transformer; |
|---|
| | 22 | import javax.xml.transform.TransformerException; |
|---|
| | 23 | import javax.xml.transform.TransformerFactory; |
|---|
| | 24 | import javax.xml.transform.dom.DOMSource; |
|---|
| | 25 | import javax.xml.transform.stream.StreamResult; |
|---|
| | 26 | |
|---|
| 15 | | import org.eclipse.core.runtime.preferences.ConfigurationScope; |
|---|
| 16 | | import org.eclipse.core.runtime.preferences.IEclipsePreferences; |
|---|
| 17 | | import org.osgi.service.prefs.BackingStoreException; |
|---|
| | 30 | import org.eclipse.core.runtime.IPath; |
|---|
| | 31 | import org.w3c.dom.Document; |
|---|
| | 32 | import org.w3c.dom.Element; |
|---|
| | 33 | import org.w3c.dom.Node; |
|---|
| | 34 | import org.w3c.dom.NodeList; |
|---|
| | 35 | import org.xml.sax.InputSource; |
|---|
| | 36 | import org.xml.sax.SAXException; |
|---|
| | 37 | import org.xml.sax.helpers.DefaultHandler; |
|---|
| 51 | | servers = new ArrayList<TracServer>(); |
|---|
| 52 | | try |
|---|
| 53 | | { |
|---|
| 54 | | TracServer server = new TracServer( "Entorno.Tpvs", |
|---|
| 55 | | new URL( "http://entorno.tpvs/trac" ), |
|---|
| 56 | | "mme002es", "bjc2100", false ); |
|---|
| 57 | | server.connect(); |
|---|
| 58 | | servers.add( server ); |
|---|
| 59 | | |
|---|
| 60 | | server = new TracServer( "Ubuntu", new URL( "http://ubuntu" ), "", |
|---|
| 61 | | "", true ); |
|---|
| 62 | | server.connect(); |
|---|
| 63 | | servers.add( server ); |
|---|
| 64 | | |
|---|
| 65 | | } catch ( MalformedURLException e ) |
|---|
| 66 | | { |
|---|
| 67 | | } |
|---|
| 72 | | IEclipsePreferences prefs = new ConfigurationScope().getNode( CATEGORY ); |
|---|
| | 73 | IPath libPath = Activator.getDefault().getStateLocation(); |
|---|
| | 74 | libPath = libPath.append("servers.xml"); //$NON-NLS-1$ |
|---|
| | 75 | File file = libPath.toFile(); |
|---|
| | 76 | if (file.exists()) { |
|---|
| | 77 | try { |
|---|
| | 78 | InputStream stream = new FileInputStream(file); |
|---|
| | 79 | DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder(); |
|---|
| | 80 | parser.setErrorHandler(new DefaultHandler()); |
|---|
| | 81 | Element root = parser.parse(new InputSource(stream)).getDocumentElement(); |
|---|
| | 82 | if(!root.getNodeName().equals("serverInfos")) { //$NON-NLS-1$ |
|---|
| | 83 | return; |
|---|
| | 84 | } |
|---|
| | 85 | |
|---|
| | 86 | NodeList list = root.getChildNodes(); |
|---|
| | 87 | int length = list.getLength(); |
|---|
| | 88 | for (int i = 0; i < length; ++i) { |
|---|
| | 89 | Node node = list.item(i); |
|---|
| | 90 | short type = node.getNodeType(); |
|---|
| | 91 | if (type == Node.ELEMENT_NODE) { |
|---|
| | 92 | Element element = (Element) node; |
|---|
| | 93 | String nodeName = element.getNodeName(); |
|---|
| | 94 | if (nodeName.equalsIgnoreCase("server")) { //$NON-NLS-1$ |
|---|
| | 95 | String name = element.getAttribute("name"); //$NON-NLS-1$ |
|---|
| | 96 | String url = element.getAttribute("url"); //$NON-NLS-1$ |
|---|
| | 97 | String username = element.getAttribute("username"); //$NON-NLS-1$ |
|---|
| | 98 | String password = element.getAttribute("password"); //$NON-NLS-1$ |
|---|
| | 99 | String anonymous = element.getAttribute("anonymous"); //$NON-NLS-1$ |
|---|
| | 100 | TracServer server = new TracServer(name,new URL(url),username,password,Boolean.parseBoolean(anonymous)); |
|---|
| | 101 | server.connect(); |
|---|
| | 102 | servers.add(server); |
|---|
| | 103 | } |
|---|
| | 104 | } |
|---|
| | 105 | } |
|---|
| | 106 | |
|---|
| | 107 | } catch (IOException e) { |
|---|
| | 108 | Log.error("Error is occured.", e); |
|---|
| | 109 | } catch (ParserConfigurationException e) { |
|---|
| | 110 | Log.error("Error is occured.", e); |
|---|
| | 111 | } catch (SAXException e) { |
|---|
| | 112 | Log.error("Error is occured.", e); |
|---|
| | 113 | } |
|---|
| | 114 | } |
|---|
| | 115 | |
|---|
| | 116 | Activator.getDefault().getStateLocation(); |
|---|
| | 117 | } |
|---|
| | 118 | |
|---|
| | 119 | /* |
|---|
| | 120 | * (non-Javadoc) |
|---|
| | 121 | * |
|---|
| | 122 | * @see java.lang.Iterable#iterator() |
|---|
| | 123 | */ |
|---|
| | 124 | public Iterator<TracServer> iterator() |
|---|
| | 125 | { |
|---|
| | 126 | if ( servers == null ) return null; |
|---|
| 84 | | for ( String key : keys ) |
|---|
| 85 | | { |
|---|
| 86 | | IEclipsePreferences node = (IEclipsePreferences) prefs.node( key ); |
|---|
| 87 | | String value = node.get( "test", "hola" ); |
|---|
| 88 | | Log.info( "Test: " + value ); |
|---|
| 89 | | } |
|---|
| 90 | | } |
|---|
| 91 | | |
|---|
| 92 | | private void savePreferences() |
|---|
| 93 | | { |
|---|
| | 135 | return instance; |
|---|
| | 136 | } |
|---|
| | 137 | |
|---|
| | 138 | private static TracServerList instance; |
|---|
| | 139 | |
|---|
| | 140 | @Override |
|---|
| | 141 | protected void notifyChanged() { |
|---|
| | 142 | try { |
|---|
| | 143 | savePreferences(); |
|---|
| | 144 | } catch (ParserConfigurationException e) { |
|---|
| | 145 | } catch (IOException e) { |
|---|
| | 146 | } catch (TransformerException e) { |
|---|
| | 147 | } |
|---|
| | 148 | super.notifyChanged(); |
|---|
| | 149 | } |
|---|
| | 150 | private void savePreferences() throws ParserConfigurationException, IOException, TransformerException |
|---|
| | 151 | { |
|---|
| | 152 | FileOutputStream stream= null; |
|---|
| | 153 | try { |
|---|
| | 154 | String xml = getServerInfoAsXML(); |
|---|
| | 155 | IPath libPath = Activator.getDefault().getStateLocation(); |
|---|
| | 156 | libPath = libPath.append("servers.xml"); |
|---|
| | 157 | File file = libPath.toFile(); |
|---|
| | 158 | if (!file.exists()) { |
|---|
| | 159 | file.createNewFile(); |
|---|
| | 160 | } |
|---|
| | 161 | stream = new FileOutputStream(file); |
|---|
| | 162 | stream.write(xml.getBytes("UTF8")); //$NON-NLS-1$ |
|---|
| | 163 | } catch (IOException e) { |
|---|
| | 164 | Log.error("exception is occured.",e); |
|---|
| | 165 | } catch (ParserConfigurationException e) { |
|---|
| | 166 | Log.error("exception is occured.",e); |
|---|
| | 167 | } catch (TransformerException e) { |
|---|
| | 168 | Log.error("exception is occured.",e); |
|---|
| | 169 | } finally { |
|---|
| | 170 | if (stream != null) { |
|---|
| | 171 | try { |
|---|
| | 172 | stream.close(); |
|---|
| | 173 | } catch (IOException e1) { |
|---|
| | 174 | } |
|---|
| | 175 | } |
|---|
| | 176 | } |
|---|
| | 177 | |
|---|
| | 178 | |
|---|
| 109 | | /* |
|---|
| 110 | | * (non-Javadoc) |
|---|
| 111 | | * |
|---|
| 112 | | * @see java.lang.Iterable#iterator() |
|---|
| 113 | | */ |
|---|
| 114 | | public Iterator<TracServer> iterator() |
|---|
| 115 | | { |
|---|
| 116 | | if ( servers == null ) return null; |
|---|
| 117 | | |
|---|
| 118 | | return servers.iterator(); |
|---|
| 119 | | } |
|---|
| 120 | | |
|---|
| 121 | | public static TracServerList getInstance() |
|---|
| 122 | | { |
|---|
| 123 | | if ( instance == null ) instance = new TracServerList(); |
|---|
| 124 | | |
|---|
| 125 | | return instance; |
|---|
| 126 | | } |
|---|
| 127 | | |
|---|
| 128 | | private static TracServerList instance; |
|---|
| 129 | | |
|---|
| | 194 | private String getServerInfoAsXML() throws ParserConfigurationException, IOException, TransformerException { |
|---|
| | 195 | Document doc = getDocument(); |
|---|
| | 196 | Element config = doc.createElement("serverInfos"); //$NON-NLS-1$ |
|---|
| | 197 | doc.appendChild(config); |
|---|
| | 198 | for(TracServer server : TracServerList.getInstance()){ |
|---|
| | 199 | Element serverElement = doc.createElement("server"); //$NON-NLS-1$ |
|---|
| | 200 | serverElement.setAttribute("name", server.getName()); //$NON-NLS-1$ |
|---|
| | 201 | serverElement.setAttribute("url", server.getUrl().toString()); //$NON-NLS-1$ |
|---|
| | 202 | serverElement.setAttribute("username", server.getUsername()); //$NON-NLS-1$ |
|---|
| | 203 | serverElement.setAttribute("password", server.getPassword()); //$NON-NLS-1$ |
|---|
| | 204 | serverElement.setAttribute("anonymous", Boolean.toString(server.isAnonymous())); //$NON-NLS-1$ |
|---|
| | 205 | config.appendChild(serverElement); |
|---|
| | 206 | } |
|---|
| | 207 | return serializeDocument(doc); |
|---|
| | 208 | } |
|---|
| | 209 | |
|---|
| | 210 | private Document getDocument() throws ParserConfigurationException { |
|---|
| | 211 | DocumentBuilderFactory dfactory= DocumentBuilderFactory.newInstance(); |
|---|
| | 212 | DocumentBuilder docBuilder= dfactory.newDocumentBuilder(); |
|---|
| | 213 | Document doc= docBuilder.newDocument(); |
|---|
| | 214 | return doc; |
|---|
| | 215 | } |
|---|
| | 216 | |
|---|
| | 217 | private String serializeDocument(Document doc) throws IOException, TransformerException { |
|---|
| | 218 | ByteArrayOutputStream s= new ByteArrayOutputStream(); |
|---|
| | 219 | |
|---|
| | 220 | TransformerFactory factory= TransformerFactory.newInstance(); |
|---|
| | 221 | Transformer transformer= factory.newTransformer(); |
|---|
| | 222 | transformer.setOutputProperty(OutputKeys.METHOD, "xml"); //$NON-NLS-1$ |
|---|
| | 223 | transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$ |
|---|
| | 224 | |
|---|
| | 225 | DOMSource source= new DOMSource(doc); |
|---|
| | 226 | StreamResult outputTarget= new StreamResult(s); |
|---|
| | 227 | transformer.transform(source, outputTarget); |
|---|
| | 228 | |
|---|
| | 229 | return s.toString("UTF8"); //$NON-NLS-1$ |
|---|
| | 230 | } |
|---|
| | 231 | |
|---|