pastebin - collaborative debugging

pastebin is a collaborative debugging tool allowing you to share and modify code snippets while chatting on IRC, IM or a message board.

This site is developed to XHTML and CSS2 W3C standards. If you see this paragraph, your browser does not support those standards and you need to upgrade. Visit WaSP for a variety of options.

Java pastebin - collaborative debugging tool View Help


Posted by metalklesk on Sun 24 May 04:31
report abuse | download | new post

  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5.  
  6. package org.modelo;
  7.  
  8. import java.io.File;
  9. import java.io.FileNotFoundException;
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;
  12. import java.util.logging.Level;
  13. import java.util.logging.Logger;
  14. import javax.xml.parsers.DocumentBuilderFactory;
  15. import javax.xml.parsers.ParserConfigurationException;
  16. import javax.xml.transform.Transformer;
  17. import javax.xml.transform.TransformerException;
  18. import javax.xml.transform.TransformerFactory;
  19. import javax.xml.transform.dom.DOMSource;
  20. import javax.xml.transform.stream.StreamResult;
  21. import org.w3c.dom.Document;
  22. import org.w3c.dom.Element;
  23. import org.w3c.dom.NodeList;
  24. import org.xml.sax.SAXException;
  25.  
  26. /**
  27.  *
  28.  * @author metalklesk
  29.  */
  30. public class AccesoDatos {
  31.  
  32.     private Document documento = null;
  33.     private String archivo = System.getProperty("user.home") + "/usuarios.xml";
  34.     private static AccesoDatos datos = new AccesoDatos();
  35.  
  36.     private AccesoDatos() {
  37.         try {
  38.             documento = (Document) (((DocumentBuilderFactory.newInstance()).newDocumentBuilder()).parse(new File(archivo)));
  39.         } catch (SAXException ex) {
  40.             Logger.getLogger(AccesoDatos.class.getName()).log(Level.SEVERE, null, ex);
  41.         } catch (IOException ex) {
  42.             Logger.getLogger(AccesoDatos.class.getName()).log(Level.SEVERE, null, ex);
  43.         } catch (ParserConfigurationException ex) {
  44.             Logger.getLogger(AccesoDatos.class.getName()).log(Level.SEVERE, null, ex);
  45.         }
  46.     }
  47.  
  48.     public static AccesoDatos getInstance() {
  49.         return datos;
  50.     }
  51.  
  52.     public Usuario getUsuario(String user, String passwd) {
  53.         Element raiz = (Element) documento.getDocumentElement();
  54.         NodeList hijos = raiz.getElementsByTagName("usuario");
  55.         Usuario usuario = null;
  56.  
  57.         for(int i=0; i<hijos.getLength(); i++) {
  58.             Element elemento = (Element) hijos.item(i);
  59.             String x = elemento.getAttribute("user");
  60.             String y = elemento.getAttribute("passwd");
  61.  
  62.             if(x.equalsIgnoreCase(user) && y.equalsIgnoreCase(passwd)) {
  63.                 usuario = new Usuario();
  64.                 usuario.setId(Integer.parseInt(elemento.getAttribute("id")));
  65.                 usuario.setUser(x);
  66.                 usuario.setPasswd(y);
  67.                 usuario.setActivo(Boolean.parseBoolean(elemento.getAttribute("activo")));
  68.  
  69.                 break;
  70.             }
  71.         }
  72.  
  73.         return usuario;
  74.     }
  75.  
  76.     public boolean setUsuario(Usuario usuario) {
  77.         Element raiz = (Element) documento.getDocumentElement();
  78.         NodeList hijos = raiz.getElementsByTagName("usuario");
  79.         boolean respuesta = false;
  80.  
  81.         for(int i=0; i<hijos.getLength(); i++) {
  82.             Element elemento = (Element) hijos.item(i);
  83.             int id = Integer.parseInt(elemento.getAttribute("id"));
  84.  
  85.             if(id == usuario.getId()) {
  86.                 elemento.setAttribute("id", String.valueOf(id));
  87.                 elemento.setAttribute("user", usuario.getUser());
  88.                 elemento.setAttribute("passwd", usuario.getPasswd());
  89.                 elemento.setAttribute("activo", String.valueOf(usuario.isActivo()));
  90.  
  91.                 respuesta = actualizarUsuarios();
  92.                 break;
  93.             }
  94.         }
  95.  
  96.         return respuesta;
  97.     }
  98.  
  99.     private boolean actualizarUsuarios() {
  100.         documento.getDocumentElement().normalize();
  101.  
  102.         try {
  103.             DOMSource source = new DOMSource(documento);
  104.             StreamResult result = new StreamResult(new FileOutputStream(archivo));
  105.  
  106.             TransformerFactory transFactory = TransformerFactory.newInstance();
  107.             Transformer transformer = transFactory.newTransformer();
  108.             transformer.transform(source, result);
  109.  
  110.             return true;
  111.         } catch (TransformerException ex) {
  112.             Logger.getLogger(AccesoDatos.class.getName()).log(Level.SEVERE, null, ex);
  113.             return false;
  114.         } catch (FileNotFoundException ex) {
  115.             Logger.getLogger(AccesoDatos.class.getName()).log(Level.SEVERE, null, ex);
  116.             return false;
  117.         }
  118.     }
  119. }

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with @@


Remember me so that I can delete my post