Example #1
0
  /**
   * Returns the result of an XSL Transformation as a JDOM document.
   *
   * <p>If the result of the transformation is a list of nodes, this method attempts to convert it
   * into a JDOM document. If successful, any subsequent call to {@link #getResult} will return an
   * empty list.
   *
   * <p><strong>Warning</strong>: The XSLT 1.0 specification states that the output of an XSL
   * transformation is not a well-formed XML document but a list of nodes. Applications should thus
   * use {@link #getResult} instead of this method or at least expect <code>null</code> documents to
   * be returned.
   *
   * @return the transformation result as a JDOM document or <code>null</code> if the result of the
   *     transformation can not be converted into a well-formed document.
   * @see #getResult
   */
  public Document getDocument() {
    Document doc = null;

    // Retrieve result from the document builder if not set.
    this.retrieveResult();

    if (result instanceof Document) {
      doc = (Document) result;
    } else {
      if ((result instanceof List) && (queried == false)) {
        // Try to create a document from the result nodes
        try {
          JDOMFactory f = this.getFactory();
          if (f == null) {
            f = new DefaultJDOMFactory();
          }

          doc = f.document(null);
          doc.setContent((List) result);

          result = doc;
        } catch (RuntimeException ex1) {
          // Some of the result nodes are not valid children of a
          // Document node. => return null.
        }
      }
    }
    queried = true;

    return (doc);
  }
  /**
   * Processes the messages from the server
   *
   * @param message
   */
  private synchronized void processServerMessage(String message) {
    SAXBuilder builder = new SAXBuilder();
    String what = new String();
    Document doc = null;

    try {
      doc = builder.build(new StringReader(message));
      Element root = doc.getRootElement();
      List childs = root.getChildren();
      Iterator i = childs.iterator();
      what = ((Element) i.next()).getName();
    } catch (Exception e) {
    }

    if (what.equalsIgnoreCase("LOGIN") == true) _login(doc);
    else if (what.equalsIgnoreCase("LOGOUT") == true) _logout(doc);
    else if (what.equalsIgnoreCase("MESSAGE") == true) _message(doc);
    else if (what.equalsIgnoreCase("WALL") == true) _wall(doc);
    else if (what.equalsIgnoreCase("CREATEGROUP") == true) _creategroup(doc);
    else if (what.equalsIgnoreCase("JOINGROUP") == true) _joingroup(doc);
    else if (what.equalsIgnoreCase("PARTGROUP") == true) _partgroup(doc);
    else if (what.equalsIgnoreCase("GROUPMESSAGE") == true) _groupmessage(doc);
    else if (what.equalsIgnoreCase("KICK") == true) _kick(doc);
    else if (what.equalsIgnoreCase("LISTUSER") == true) _listuser(doc);
    else if (what.equalsIgnoreCase("LISTGROUP") == true) _listgroup(doc);
  }
Example #3
0
  /**
   * Método public static void leerArchivoXML(ListaUsuarios listaDeUsuarios): Este método permite
   * leer un archivo XML que contiene los datos de los usuarios a través de jdom
   */
  public static void leerArchivoXML(ListaUsuario listaDeUsuarios) {
    try {
      SAXBuilder builder = new SAXBuilder();

      /* Se crea un documento nuevo con el nombre del archivo */
      Document doc = builder.build(nombreArchivo);

      /* Se obtiene la raíz del archivo (la etiqueta inicial) */
      Element raiz = doc.getRootElement();

      /* Se puede obtener el atributo de la raíz (de la etiqueta) */
      System.out.println(raiz.getAttributeValue("tipo"));

      /* Se obtienen todos los hijos cuya etiqueta esa "usuario"  */
      /* y se asignan esos hijos a un List                        */
      List listaUsuarios = raiz.getChildren("usuario");

      System.out.println("Formada por:" + listaUsuarios.size() + " usuarios");
      System.out.println("------------------");

      /* Se genera un iterador para recorrer el List que se generó */
      Iterator i = listaUsuarios.iterator();

      /* Se recorre el List */
      while (i.hasNext()) {
        /* Se obtiene cada uno y se asigna a un objeto de tipo Element */
        Element e = (Element) i.next();

        /* Se obtiene el nombre, apellido y cargo de cada una de las etiquetas  */
        /* hijas de usuario, es decir, nombre, apellido y cargo                 */
        Element nick = e.getChild("nick");
        Element clave = e.getChild("clave");
        Element nombre = e.getChild("nombre");
        Element apellido = e.getChild("apellido");
        Element fechanac = e.getChild("fechanac");
        Element avatar = e.getChild("avatar");

        /* Se crea un nodo nuevo con la información y se agrega a la lista de usuarios */
        Usuario elUsuario =
            new Usuario(
                nick.getText(),
                clave.getText(),
                nombre.getText(),
                apellido.getText(),
                fechanac.getText(),
                avatar.getText());

        listaDeUsuarios.AgregarElemento(elUsuario);
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
 /**
  * Process kick from server
  *
  * @param doc
  */
 private void _kick(Document doc) {
   Element root = doc.getRootElement();
   Element kick = root.getChild("kick");
   String user = kick.getChild("user").getText();
   String answer = kick.getChild("answer").getText();
   if (answer.equals("OK") == true) sendMessage(SERVER, user + " was kicked from " + _group);
 }
 /**
  * Process Message from server
  *
  * @param doc
  */
 private void _message(Document doc) {
   Element root = doc.getRootElement();
   Element message = root.getChild("message");
   String sender = message.getChild("sender").getText();
   String mesg = message.getChild("mesg").getText();
   sendMessage(NORMAL, sender + ": " + mesg);
 }
 /**
  * Process Partgroup from server
  *
  * @param doc
  */
 private void _partgroup(Document doc) {
   Element root = doc.getRootElement();
   Element part = root.getChild("partgroup");
   String group = part.getChild("group").getText();
   String answer = part.getChild("answer").getText();
   if (group.equals(_group) == true) {
     sendMessage(SERVER, group + ": " + answer);
   }
 }
Example #7
0
  // Empieza La Carga de la Partida
  public static void leerArchivoXML(ListaPartida listaDePartidas) {
    try {
      SAXBuilder builder = new SAXBuilder();

      /* Se crea un documento nuevo con el nombre del archivo */
      Document doc = builder.build(nombreArchivoPartida);

      /* Se obtiene la raíz del archivo (la etiqueta inicial) */
      Element raiz = doc.getRootElement();

      /* Se puede obtener el atributo de la raíz (de la etiqueta) */
      System.out.println(raiz.getAttributeValue("tipo"));

      /* Se obtienen todos los hijos cuya etiqueta esa "usuario"  */
      /* y se asignan esos hijos a un List                        */
      List listaPartida = raiz.getChildren("partida");

      System.out.println("Formada por:" + listaPartida.size() + " Partidas");
      System.out.println("------------------");

      /* Se genera un iterador para recorrer el List que se generó */
      Iterator i = listaPartida.iterator();

      /* Se recorre el List */
      while (i.hasNext()) {
        /* Se obtiene cada uno y se asigna a un objeto de tipo Element */
        Element e = (Element) i.next();

        /* Se obtiene el nombre, apellido y cargo de cada una de las etiquetas  */
        /* hijas de usuario, es decir, nombre, apellido y cargo                 */
        Element nick = e.getChild("Usuario");
        Element ID = e.getChild("ID");
        Element fechaactual = e.getChild("fechaacual");
        Element fechainicio = e.getChild("fechainicio");
        /* Se crea un nodo nuevo con la información y se agrega a la lista de usuarios */
        Partida laPartida =
            new Partida(
                nick.getText(), ID.getContentSize(), fechaactual.getText(), fechainicio.getText());
        listaDePartidas.AgregarElemento(laPartida);
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
 /**
  * Process Groupmessage from server
  *
  * @param doc
  */
 private void _groupmessage(Document doc) {
   Element root = doc.getRootElement();
   Element message = root.getChild("groupmessage");
   String group = message.getChild("group").getText();
   String sender = message.getChild("sender").getText();
   String mesg = message.getChild("mesg").getText();
   if (group.equals(_group) == true) {
     sendMessage(NORMAL, group + "(" + sender + "): " + mesg);
   }
 }
 /**
  * Process Joingroup from server
  *
  * @param doc
  */
 private void _joingroup(Document doc) {
   Element root = doc.getRootElement();
   Element join = root.getChild("joingroup");
   String group = join.getChild("group").getText();
   String answer = join.getChild("answer").getText();
   sendMessage(SERVER, "JoinGroup: " + answer);
   if (answer.equals("OK") == true) {
     sendMessage(SERVER, "Group " + group + " joined.");
     _group = group;
   }
 }
Example #10
0
 /**
  * Process Creategroup from server
  *
  * @param doc
  */
 private void _creategroup(Document doc) {
   Element root = doc.getRootElement();
   Element create = root.getChild("creategroup");
   String group = create.getChild("group").getText();
   String answer = create.getChild("answer").getText();
   sendMessage(SERVER, "Group creation is: " + answer);
   if (answer.equals("OK") == true) {
     sendMessage(SERVER, "Group " + group + " joined.");
     _group = group;
   }
 }
Example #11
0
  /**
   * Process listuser from server
   *
   * @param doc
   */
  private void _listuser(Document doc) {
    StringBuffer userlist = new StringBuffer();

    Element root = doc.getRootElement();
    Element list = root.getChild("listuser");
    List users = list.getChildren("user");
    Iterator i = users.iterator();
    while (i.hasNext()) {
      userlist.append(((Element) i.next()).getText());
      userlist.append("\n");
    }
    sendMessage(SERVER, "Users: \n" + userlist);
  }
Example #12
0
  /**
   * Process listgroup from server
   *
   * @param doc
   */
  private void _listgroup(Document doc) {
    StringBuffer grouplist = new StringBuffer();

    Element root = doc.getRootElement();
    Element list = root.getChild("listgroup");
    List groups = list.getChildren("group");
    Iterator i = groups.iterator();
    while (i.hasNext()) {
      Element temp = (Element) i.next();
      grouplist.append(temp.getChild("name").getText());
      grouplist.append("\t");
      grouplist.append(temp.getChild("description").getText());
      grouplist.append("\n");
    }
    sendMessage(SERVER, "Groups: \n" + grouplist);
  }
Example #13
0
 /**
  * Process Wall from server
  *
  * @param doc
  */
 private void _wall(Document doc) {
   Element root = doc.getRootElement();
   Element wall = root.getChild("wall");
   String mesg = wall.getChild("mesg").getText();
   sendMessage(SERVER, mesg);
 }
Example #14
0
 /**
  * Process Logout from server
  *
  * @param doc
  */
 private void _logout(Document doc) {
   Element root = doc.getRootElement();
   Element logout = root.getChild("logout");
   String answer = logout.getChild("answer").getText();
   sendMessage(SERVER, "Logout is: " + answer);
 }