Example #1
0
 /**
  * Converts a model to JSON.
  *
  * @return model JSON, or empty
  */
 public String toJSON() {
   try {
     return buildObjectMapper().writeValueAsString(this.entity);
   } catch (Exception e) {
     Log.error(e.getMessage(), e);
     return Empty;
   }
 }
Example #2
0
  /**
   * Returns a new model instance.
   *
   * @param modelJSON a model in JSON format
   * @return a new model, or null
   */
  public ModelType fromJSON(String modelJSON) {
    if (StringUtils.defaultString(modelJSON).isEmpty()) {
      return null;
    }

    try {
      return buildObjectMapper().readValue(modelJSON, this.entityClass);
    } catch (Exception e) {
      Log.error(e.getMessage(), e);
      return null;
    }
  }
Example #3
0
 /**
  * Used to update the preferences with possibly new values from the root document. This method is
  * only used within this class to prevent recursion from creating a <code>Logger</code> instance.
  *
  * @see java.util.logging.Logger
  */
 private static void silentUpdate() {
   try {
     // Record all of the preferences in the odin.prefs.xml file
     prefsOdin.exportNode(new FileOutputStream(sPrefsPath));
   } catch (Exception x) {
     // If an error was encountered during the update then write the details
     // to the System.err as there isn't a log to use.
     System.err.println(
         "Couldn't update the preferences with values from the server's root document.");
     x.printStackTrace();
   }
 }
Example #4
0
 /**
  * Converts a model to XML.
  *
  * @return model XML, or empty
  */
 public String toXML() {
   try {
     ByteArrayOutputStream stream = new ByteArrayOutputStream();
     Marshaller m = buildJAXBContext().createMarshaller();
     m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
     m.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
     m.marshal(this.entity, stream);
     return stream.toString(XML_ENCODING).trim();
   } catch (Exception e) {
     Log.error(e.getMessage(), e);
     return Empty;
   }
 }
Example #5
0
  /**
   * Returns a new model instance.
   *
   * @param modelXML a model in XML format
   * @return a new model, or null
   */
  public ModelType fromXML(String modelXML) {
    if (StringUtils.defaultString(modelXML).isEmpty()) {
      return null;
    }

    try {
      byte[] xmlData = modelXML.getBytes(XML_ENCODING);
      ByteArrayInputStream stream = new ByteArrayInputStream(xmlData);
      return (ModelType) buildJAXBContext().createUnmarshaller().unmarshal(stream);
    } catch (Exception e) {
      Log.error(e.getMessage(), e);
      return null;
    }
  }
Example #6
0
 /**
  * This class provides exactly the same functions are the getOdinPrefs method but doesn't record
  * anything in the Log
  *
  * <p>It should <b>only</b> be used by the LoggerFactory class to prevent a loop. All other
  * classes should use the getOdinPrefs method to utilise the logger.
  *
  * <p>It also retrieves much of the information from the Server's Root XML document and reads it
  * into the preferences for later reads.
  *
  * @return A new instance of Preferences to be used locally
  */
 public static Preferences getInitialPreferences() {
   /* As mentioned in the JavaDoc comments, this is the same code but omitting the use of the log. It doesn't harm if
    * a class other than LoggerFactory uses it, we just lose the logs if something goes wrong.
    */
   if (prefsOdin == null) {
     try {
       String prefsFilePath = getPrefsPath() + prefsFileName;
       File f = new File(prefsFilePath);
       if (f.exists()) {
         sPrefsPath = prefsFilePath;
         BufferedInputStream bisXMLPrefs =
             new BufferedInputStream(new FileInputStream(prefsFilePath));
         if (bisXMLPrefs != null) {
           prefsOdin.importPreferences(bisXMLPrefs);
         }
       } else {
         File fBackup = new File(prefsFileName);
         if (fBackup.exists()) {
           // Using the backup preferences in the application directory.
           sPrefsPath = prefsFileName;
         }
         BufferedInputStream bisXMLPrefs =
             new BufferedInputStream(new FileInputStream(prefsFileName));
         prefsOdin.importPreferences(bisXMLPrefs);
       }
     } catch (FileNotFoundException fnfX) {
       System.err.println("Couldn't retrieve either preferences.");
     } catch (Exception x) {
       // If any errors are thrown then put them in System.err rather than the Logger
       System.err.println(
           "An error was caught while trying to obtain preferences for the logger.");
       x.printStackTrace();
     }
   }
   prefsOdin = Preferences.userNodeForPackage(OdinPreferences.class);
   // Get hold of the JAXB data contained within the Server Root document to find out where we
   // should store everything.
   getRootDocument();
   prefsOdin.put("odin.jaxb.dir.Archive", rDocument.getLocations().getArchive());
   prefsOdin.put("odin.jaxb.dir.Convert", rDocument.getLocations().getConversion());
   prefsOdin.put("odin.jaxb.dir.Mime", rDocument.getLocations().getFormat());
   prefsOdin.put("odin.jaxb.dir.Log", rDocument.getLocations().getLogging());
   prefsOdin.put("odin.jaxb.dir.Prefs", rDocument.getLocations().getPreference());
   prefsOdin.put("odin.jaxb.dir.Hazard", rDocument.getLocations().getQuarantine());
   prefsOdin.put("odin.jaxb.dir.Store", rDocument.getLocations().getRepository());
   silentUpdate();
   return prefsOdin;
 }
Example #7
0
  public static void main(String[] args) {
    // TestObjects testObjects = new TestObjects();
    // testObjects.getTheme().getIntro();
    // List<ScreenNode> blah2 = testObjects.getTheme().getIntro();
    // testObjects.getLocale().getAct(0);
    // Game game = testObjects.getStructure().createGame();

    try {
      // readXml();
      createXsd();
      // createXml(testObjects);
      // readXmlGenerated();
      // test();
    } catch (Exception e) {
      e.printStackTrace();
    }

    // theme.setCharacters(characters);
    // theme.setSubject(subject);
    //
    // List<LearningAct> learningObjectives = new ArrayList<LearningAct>();
    // learningObjectives.add(learningAct);
    //
    // locale.setCharacters(characters);
    // locale.setLearningActs(learningObjectives);
    // locale.setTheme(theme);
    //
    // structure.setTheme(theme);
    // structure.setLocale(locale);
    // game = structure.createGame();
    //
    // try {
    // writeGameXml();
    // createXsd();
    // createXml(testObjects);
    // readXmlGenerated();
    // test();
    // } catch (Exception e) {
    // e.printStackTrace();
    // }
    System.out.println();
  }