コード例 #1
0
ファイル: GAMLWriterV10.java プロジェクト: potikanond/GAM
  @Override
  public void write(GAMObject go) {

    try {
      XMLEventFactory efac = XMLEventFactory.newInstance();

      XMLEvent startDoc = efac.createStartDocument("UTF-8", "1.0");
      XMLEvent endDoc = efac.createEndDocument();

      XMLEvent startGAML = efac.createStartElement("", "", "gaml");
      XMLEvent attrVersion = efac.createAttribute("version", "1.0");
      XMLEvent endGAML = efac.createEndElement("", "", "gaml");

      XMLEvent startServer = efac.createStartElement("", "", "server");
      String server = go.getServer();
      if (server == null) {
        server = "";
      }
      XMLEvent charServer = efac.createCharacters(server);
      XMLEvent endServer = efac.createEndElement("", "", "server");

      XMLEvent startModel = efac.createStartElement("", "", "model");
      XMLEvent attrName = efac.createAttribute("name", go.getName());
      XMLEvent endModel = efac.createEndElement("", "", "model");

      /*<?xml version="1.0" encoding="UTF-8"?>*/
      writer.add(startDoc);
      writer.add(newLine);

      /*<gaml>*/
      writer.add(startGAML);
      writer.add(attrVersion);
      writer.add(newLine);

      /*<server>*/
      writer.add(tab);
      writer.add(startServer);
      writer.add(charServer);
      writer.add(endServer);
      writer.add(newLine);

      /*<model>*/
      writer.add(tab);
      writer.add(startModel);
      writer.add(attrName);
      writer.add(newLine);

      /*<meta>*/
      writeMetaModel(writer, go);

      /*<tags>*/
      Vector<String> tags = go.getTags();
      if (tags.size() > 0) {
        String sIndent2 = "  " + "  ";
        XMLEvent indent2 = efac.createDTD(sIndent2);

        writeTags(writer, indent2, tags);
      }

      /*<annotations>*/
      Properties annotations = go.getAnnotations();
      if (annotations.size() > 0) {
        String sIndent2 = "  " + "  ";
        XMLEvent indent2 = efac.createDTD(sIndent2);

        writeAnnotations(writer, indent2, annotations);
      }

      /*<sections>*/
      if (go.getSections().size() > 0) {
        writeSections(writer, go);
      }

      /*<structures>*/
      if (go.getStructures().size() > 0) {
        writeStructures(writer, go);
      }

      /*</model>*/
      writer.add(tab);
      writer.add(endModel);
      writer.add(newLine);

      /*<user-annotations>*/
      Properties userAnnotations = go.getUserAnnotations();
      if (userAnnotations.size() > 0) {
        String sIndent1 = "  ";
        XMLEvent indent1 = efac.createDTD(sIndent1);

        writeUserAnnotations(writer, indent1, userAnnotations);
      }

      /*</gaml>*/
      writer.add(endGAML);
      writer.add(newLine);

      writer.add(endDoc);
      writer.flush();

    } catch (XMLStreamException e) {
      System.out.println("GAMLWriterV10::write - error writing GAM");
      e.printStackTrace();
    }
  }