@Test
  public void testSimpleXmlHandling() throws Exception {
    // From bytes.
    Persister p = new Persister();
    Example e = p.read(Example.class, new ByteArrayInputStream(utf8Xml));
    assertThat(e.text).isEqualTo(" \u0096 ");

    // round trip.
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    p.write(e, baos);
    e = p.read(Example.class, new ByteArrayInputStream(utf8Xml));
    assertThat(e.text).isEqualTo(" \u0096 ");
  }
Пример #2
0
  public void testAnnotationType() throws Exception {
    Strategy strategy = new AnnotationStrategy();
    Persister persister = new Persister(strategy);
    StringWriter writer = new StringWriter();
    AnnotationExample example = persister.read(AnnotationExample.class, SOURCE);

    persister.write(example, writer);

    String text = writer.toString();

    assertElementHasAttribute(text, "/annotationExample", "age", "10");
    assertElementHasAttribute(text, "/annotationExample/name", "key", "name");

    AnnotationExample result = persister.read(AnnotationExample.class, text);

    assertEquals(example.name, result.name);
    assertEquals(example.age, result.age);

    validate(result, persister);
  }
Пример #3
0
 public void testXML() throws Exception {
   @Root
   @Namespace
   class C1 {
     @Element private int i;
     @Element private int j;
   }
   C1 c1 = new C1();
   Persister serializer =
       new Persister(new Format("<?xml version=\"1.0\" encoding= \"UTF-8\" ?>"));
   StringWriter sw = new StringWriter();
   serializer.write(c1, sw);
   System.out.println(sw.getBuffer().toString());
   String str = "<c1><i>1</i><j>2</j></c1>";
   serializer.read(c1, str);
 }
Пример #4
0
  /** Open attributes from an XML file. May return an empty value set, but never null. */
  static AttributeValueSets openAttributes() {
    final IPath pathHint = FileDialogs.recallPath(REMEMBER_DIRECTORY);
    final IPath readLocation = FileDialogs.openReadXML(pathHint);
    if (readLocation != null) {
      FileDialogs.rememberDirectory(REMEMBER_DIRECTORY, readLocation);
      try {
        final Persister persister = new Persister();
        final AttributeValueSets avs =
            persister.read(AttributeValueSets.class, readLocation.toFile());

        return avs;
      } catch (Exception e) {
        Utils.showError(
            new Status(
                IStatus.ERROR,
                WorkbenchCorePlugin.PLUGIN_ID,
                "Failed to read attributes from: " + readLocation.toOSString(),
                e));
      }
    }

    return new AttributeValueSets();
  }
Пример #5
0
  public void processEmailTemplates(List<String> templatePaths) {

    final String ADMIN = "admin";

    Persister persister = new Persister();
    for (String templatePath : templatePaths) {

      log.debug("Processing template: " + templatePath);

      InputStream in = getClass().getClassLoader().getResourceAsStream(templatePath);

      if (in == null) {
        log.warn("Could not load resource from '" + templatePath + "'. Skipping ...");
        continue;
      }

      EmailTemplate template = null;
      try {
        template = persister.read(EmailTemplate.class, in);
      } catch (Exception e) {
        log.warn(
            "Error processing template: '"
                + templatePath
                + "', "
                + e.getClass()
                + ":"
                + e.getMessage()
                + ". Skipping ...");
        continue;
      }

      // check if we have an existing template of this key and locale
      // its possible the template has no locale set
      // The locale could also be the Default
      Locale loc = null;
      if (template.getLocale() != null
          && !"".equals(template.getLocale())
          && !EmailTemplate.DEFAULT_LOCALE.equals(template.getLocale())) {
        loc = LocaleUtils.toLocale(template.getLocale());
      }

      EmailTemplate existingTemplate = getEmailTemplateNoDefault(template.getKey(), loc);
      if (existingTemplate == null) {
        // no existing, save this one
        Session sakaiSession = sessionManager.getCurrentSession();
        sakaiSession.setUserId(ADMIN);
        sakaiSession.setUserEid(ADMIN);
        saveTemplate(template);
        sakaiSession.setUserId(null);
        sakaiSession.setUserId(null);
        log.info(
            "Saved email template: " + template.getKey() + " with locale: " + template.getLocale());
        continue; // skip to next
      }

      // check version, if local one newer than persisted, update it - SAK-17679
      // also update the locale - SAK-20987
      int existingTemplateVersion =
          existingTemplate.getVersion() != null ? existingTemplate.getVersion().intValue() : 0;
      if (template.getVersion() > existingTemplateVersion) {
        existingTemplate.setSubject(template.getSubject());
        existingTemplate.setMessage(template.getMessage());
        existingTemplate.setHtmlMessage(template.getHtmlMessage());
        existingTemplate.setVersion(template.getVersion());
        existingTemplate.setOwner(template.getOwner());
        existingTemplate.setLocale(template.getLocale());

        Session sakaiSession = sessionManager.getCurrentSession();
        sakaiSession.setUserId(ADMIN);
        sakaiSession.setUserEid(ADMIN);
        updateTemplate(existingTemplate);
        sakaiSession.setUserId(null);
        sakaiSession.setUserId(null);
        log.info(
            "Updated email template: "
                + template.getKey()
                + " with locale: "
                + template.getLocale());
      }
    }
  }