Exemplo n.º 1
0
 private ArrayList<Element> serializeFields(
     Field[] fields, Object object, Document doc) // serializes the fields
     {
   Class currentClass = object.getClass();
   ArrayList<Element> elements = new ArrayList<Element>();
   for (Field f : fields) {
     try {
       if (!f.getType().isPrimitive()) // Field not primitive, is a reference to another object
       {
       } else // field is primitive
       {
         Element newField = new Element("field");
         newField.setAttribute("name", f.getName());
         newField.setAttribute("declaringclass", f.getDeclaringClass().getName());
         Element newValue = new Element("value");
         newValue.addContent(f.get(object).toString());
         newField.addContent(newValue);
         elements.add(newField);
       }
     } catch (Exception e) {
       e.printStackTrace();
     }
   }
   return elements;
 }
Exemplo n.º 2
0
  public Document serialize(Object object, Document doc) {
    Class c = object.getClass();
    Integer id = getID(object);
    map.put(object, id);
    String mapSize = Integer.toString(map.size());

    // creating serialization objects
    Element objectElement = new Element("object");
    objectElement.setAttribute(new Attribute("class", c.getName()));
    objectElement.setAttribute(new Attribute("id", mapSize));
    doc.getRootElement().addContent(objectElement);

    if (!c.isArray()) // class is not an array
    {
      Field[] fields = c.getDeclaredFields();
      ArrayList<Element> fieldXML = serializeFields(fields, object, doc);
      for (int i = 0; i < fieldXML.size(); i++) {
        objectElement.addContent(fieldXML.get(i));
      }
    } else // class is an array
    {
      Object array = object;
      objectElement.setAttribute(new Attribute("length", Integer.toString(Array.getLength(array))));
      if (c.getComponentType().isPrimitive()) // class is array of primitives
      {
        for (int i = 0; i < Array.getLength(array); i++) {
          Element value = new Element("value");
          value.setText(Array.get(c, i).toString());
          objectElement.addContent(value);
        }
      } else // class is array of references
      {
        for (int j = 0; j < Array.getLength(array); j++) {
          Element ref = new Element("reference");
          id = getID(Array.get(c, j));
          if (id != -1) {
            ref.setText(Integer.toString(id));
          }
        }
        for (int k = 0; k < Array.getLength(array); k++) {
          serialize(Array.get(array, k), doc);
        }
      }
    }
    if (currentElement == 0) {
      referenceID = 0;
    }

    return doc;
  }
Exemplo n.º 3
0
  public Element getLookupElement() {
    Element e = new Element("lookup");
    if (saveErrors.size() != 0) {
      saveErrors.clear();
    }
    ObjEntityView view = objEntityViewField.getObjEntityView();
    DataView dataView = view.getDataView();
    String fieldPath =
        "<b>"
            + dataView.getName()
            + "."
            + view.getName()
            + "."
            + objEntityViewField.getName()
            + "</b><br>";
    if (lookupObjEntityView != null) {
      e.setAttribute(new Attribute("obj-entity-view-name", lookupObjEntityView.getName()));
    } else {
      e.setAttribute(new Attribute("obj-entity-view-name", ""));
      saveErrors.add(fieldPath + "lookup hasn't attribute \"obj-entity-view-name\"<br><br>");
    }
    if (lookupField != null) {
      e.setAttribute(new Attribute("field-name", lookupField.getName()));
    } else {
      e.setAttribute(new Attribute("field-name", ""));
      saveErrors.add(fieldPath + "lookup hasn't attribute \"field-name\"<br><br>");
    }
    e.addContent("");

    return e;
  }
Exemplo n.º 4
0
	/**
	 *  新增循环节点
	 * @param cycNode 循环节点
	 * @return
	 */
	public Element addCycNode(Element cycNode)
	{
		Element node=cycNode;
		Element child=null;
		Element newNode=null;
		Element tmpNode=null;
		String sName="";
		if(cycNode==null)
			return newNode;
		newNode=new Element(node.getName());
		node.getParent().addContent(newNode);
		List cList=node.getChildren();
		for(int i=0;i<cList.size();i++)
		{
			child=(Element)cList.get(i);
			sName=child.getName();
			sName=sName.trim();
			tmpNode=new Element(sName);
			newNode.addContent(tmpNode);
		}
		return newNode;
	}
Exemplo n.º 5
0
 public void writeXML(Element e) {
   Element ne = new Element("jinput");
   ne.setAttribute("ident", ident);
   ne.setAttribute("value", "" + value);
   e.addContent(ne);
 }
Exemplo n.º 6
0
 /*
 public boolean typedNew(KeyEvent e,NewBinding.EvBindStatus status)
 	{
 	return false;
 	}
 */
 public void writeXML(Element e) {
   Element ne = new Element("keycode");
   ne.setAttribute("keyCode", "" + keyCode);
   ne.setAttribute("modifier", "" + modifierEx);
   e.addContent(ne);
 }
Exemplo n.º 7
0
 public void writeXML(Element e) {
   Element ne = new Element("char");
   ne.setAttribute("char", "" + key);
   e.addContent(ne);
 }
Exemplo n.º 8
0
  /**
   * Método public static void guardarArchivoXML(ListaUsuarios listaDeUsuarios): Este método permite
   * guardar la lista de usuarios en un archivo XML. El procesamiento se hace con jdom
   */
  public static void guardarArchivoXML(ListaUsuario listaDeUsuarios) {
    Usuario nodoAuxiliar;

    /* Se crea una raiz de la estructura */
    Element root = new Element("usuarios");

    /* Es posible agregar atributos a la estructura inicial */
    root.setAttribute("tipo", "lista de usuarios");

    Iterator iterador = listaDeUsuarios.getIterator();

    while (iterador.hasNext()) {
      /* Se crea la etiqueta "usuario" */
      Element usuarios = new Element("usuario");

      nodoAuxiliar = (Usuario) iterador.next();

      /* Se crean las etiquetas nombre, apellido y cargo */
      Element nick = new Element("nick");
      Element clave = new Element("clave");
      Element nombre = new Element("nombre");
      Element apellido = new Element("apellido");
      Element fechanac = new Element("fechanac");
      Element avatar = new Element("avatar");

      /* Se inicializa cada etiqueta con sus valores de la lista */
      nick.setText(nodoAuxiliar.getNickname());
      clave.setText(nodoAuxiliar.getClave());
      nombre.setText(nodoAuxiliar.getNombre());
      apellido.setText(nodoAuxiliar.getApellido());
      fechanac.setText(nodoAuxiliar.getFechanaci());
      avatar.setText(nodoAuxiliar.getAvatar());

      /* Se añaden las etiquetas a la etiqueta principal (usuario)    */
      /* estableciendo que un usuario tiene nombre, apellido y cargo  */
      usuarios.addContent(nick);
      usuarios.addContent(clave);
      usuarios.addContent(nombre);
      usuarios.addContent(apellido);
      usuarios.addContent(fechanac);
      usuarios.addContent(avatar);

      /* Se añade el nuevo usuario a la estructura XML */
      root.addContent(usuarios);
    }

    /* Se crea un documento nuevo */
    Document doc = new Document(root);

    try {
      /* Se genera un flujo de salida de datos XML */
      XMLOutputter out = new XMLOutputter();

      /* Se asocia el flujo de salida con el archivo donde se guardaran los datos */
      FileOutputStream file = new FileOutputStream(nombreArchivo);

      /* Se manda el documento generado hacia el archivo XML */
      out.output(doc, file);

      /* Se limpia el buffer ocupado por el objeto file y se manda a cerrar el archivo */
      file.flush();
      file.close();

      /* En este caso se manda a imprimir el archivo por la consola   */
      /* ESTE PROCESO NO ES OBLIGATORIO PARA PROCESAR EL XML          */
      out.output(doc, System.out);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
Exemplo n.º 9
0
  public static void guardarArchivoXML(ListaPartida listaDePartidas, boolean valor) {
    Ficha nodoAuxiliarPote;
    Ficha nodoAuxiliarUsuario;
    Ficha nodoAuxiliarServidor;
    Partida partidaActual;
    Ficha fichaActual;

    /* Se crea una raiz de la estructura */
    Element root = new Element("fichas");

    /* Es posible agregar atributos a la estructura inicial */
    root.setAttribute("tipo", "lista de fichas");

    Iterator iteradorPartida = listaDePartidas.getIterator();
    Iterator iteradorFichaUsuario;
    Iterator iteradorFichaPote;
    Iterator iteradorFichaServidor;

    Element ID;
    Element fichaPote;
    Element fichaUsuario;
    Element fichaServidor;
    Element fichaX;
    Element fichaY;

    while (iteradorPartida.hasNext()) {
      ID = new Element("ID");
      fichaPote = new Element("fichapote");
      fichaUsuario = new Element("fichausuario");
      fichaServidor = new Element("fichaservidor");

      partidaActual = (Partida) iteradorPartida.next();

      ID.setAttribute("id", Integer.toString(partidaActual.getID()));

      iteradorFichaPote = partidaActual.getFichapote().getIterator();

      while (iteradorFichaPote.hasNext()) {
        fichaX = new Element("X");
        fichaY = new Element("Y");
        fichaActual = (Ficha) iteradorFichaPote.next();
        fichaX.setText(Integer.toString(fichaActual.getX()));
        fichaY.setText(Integer.toString(fichaActual.getY()));
        fichaPote.addContent(fichaX);
        fichaPote.addContent(fichaY);
      }

      ID.addContent(fichaPote);

      iteradorFichaUsuario = partidaActual.getFichausuario().getIterator();

      while (iteradorFichaUsuario.hasNext()) {
        fichaX = new Element("X");
        fichaY = new Element("Y");
        fichaActual = (Ficha) iteradorFichaUsuario.next();
        fichaX.setText(Integer.toString(fichaActual.getX()));
        fichaY.setText(Integer.toString(fichaActual.getY()));
        fichaUsuario.addContent(fichaX);
        fichaUsuario.addContent(fichaY);
      }

      ID.addContent(fichaUsuario);

      iteradorFichaServidor = partidaActual.getFichaservidor().getIterator();

      while (iteradorFichaServidor.hasNext()) {
        fichaX = new Element("X");
        fichaY = new Element("Y");
        fichaActual = (Ficha) iteradorFichaServidor.next();
        fichaX.setText(Integer.toString(fichaActual.getX()));
        fichaY.setText(Integer.toString(fichaActual.getY()));
        fichaServidor.addContent(fichaX);
        fichaServidor.addContent(fichaY);
      }

      ID.addContent(fichaServidor);

      root.addContent(ID);
    }

    /* Se crea un documento nuevo */
    Document doc = new Document(root);

    try {
      /* Se genera un flujo de salida de datos XML */
      XMLOutputter out = new XMLOutputter();

      /* Se asocia el flujo de salida con el archivo donde se guardaran los datos */
      FileOutputStream file = new FileOutputStream(nombreArchivoFicha);

      /* Se manda el documento generado hacia el archivo XML */
      out.output(doc, file);

      /* Se limpia el buffer ocupado por el objeto file y se manda a cerrar el archivo */
      file.flush();
      file.close();

      /* En este caso se manda a imprimir el archivo por la consola   */
      /* ESTE PROCESO NO ES OBLIGATORIO PARA PROCESAR EL XML          */
      out.output(doc, System.out);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }