Ejemplo n.º 1
0
 /**
  * Contains the XML representation of the node and all its descendants.
  *
  * @return an XML representation of this node and all its descendants
  */
 @Override
 public String jsxGet_xml() {
   final XMLSerializer seralizer = new XMLSerializer();
   seralizer.setParentScope(getWindow());
   seralizer.setPrototype(getPrototype(seralizer.getClass()));
   return seralizer.jsxFunction_serializeToString(jsxGet_documentElement());
 }
Ejemplo n.º 2
0
  public void serializeAttributes(BeanT bean, XMLSerializer target)
      throws SAXException, IOException, XMLStreamException {
    for (AttributeProperty<BeanT> p : attributeProperties)
      try {
        if (retainPropertyInfo) {
          final Property parentProperty = target.getCurrentProperty();
          target.currentProperty.set(p);
          p.serializeAttributes(bean, target);
          target.currentProperty.set(parentProperty);
        } else {
          p.serializeAttributes(bean, target);
        }
        if (p.attName.equals(WellKnownNamespace.XML_SCHEMA_INSTANCE, "nil")) {
          isNilIncluded = true;
        }
      } catch (AccessorException e) {
        target.reportError(null, e);
      }

    try {
      if (inheritedAttWildcard != null) {
        Map<QName, String> map = inheritedAttWildcard.get(bean);
        target.attWildcardAsAttributes(map, null);
      }
    } catch (AccessorException e) {
      target.reportError(null, e);
    }
  }
Ejemplo n.º 3
0
 /**
  * Contains the XML representation of the node and all its descendants.
  *
  * @return an XML representation of this node and all its descendants
  */
 @Override
 @JsxGetter({@WebBrowser(IE), @WebBrowser(CHROME)})
 public String getXml() {
   final XMLSerializer seralizer = new XMLSerializer();
   seralizer.setParentScope(getWindow());
   seralizer.setPrototype(getPrototype(seralizer.getClass()));
   return seralizer.serializeToString(getDocumentElement());
 }
Ejemplo n.º 4
0
 private OnWire _adaptM(XMLSerializer serializer, InMemory v) throws MarshalException {
   XmlAdapter<OnWire, InMemory> a = serializer.getAdapter(adapter);
   try {
     return a.marshal(v);
   } catch (Exception e) {
     serializer.handleError(e, v, null);
     throw new MarshalException(e);
   }
 }
Ejemplo n.º 5
0
 private OnWire adaptM(Marshaller m, InMemory v) throws JAXBException {
   XMLSerializer serializer = ((MarshallerImpl) m).serializer;
   serializer.pushCoordinator();
   try {
     return _adaptM(serializer, v);
   } finally {
     serializer.popCoordinator();
   }
 }
Ejemplo n.º 6
0
 private XMLSerializer getXmlSerializer() {
   final XMLSerializer xmlSerializer = new XMLSerializer();
   xmlSerializer.setEscapeLowerChars(true);
   xmlSerializer.setKeepArrayName(true);
   xmlSerializer.setPerformAutoExpansion(true);
   xmlSerializer.setKeepCData(true);
   xmlSerializer.setTypeHintsEnabled(false);
   xmlSerializer.setTypeHintsCompatibility(false);
   xmlSerializer.setRootName("Document");
   xmlSerializer.setKeepProcessingInstructions(true);
   xmlSerializer.setHandleEmptyElementsAsNull(false);
   return xmlSerializer;
 }
Ejemplo n.º 7
0
  public void test_should_parse_2nd_document()
      throws IOException, ParserConfigurationException, SAXException {

    final XMLSerializer xmlSerializer = getXmlSerializer();

    String fixture = toString("net/sf/json/xml/idml_document2.idms");

    JSON json = xmlSerializer.read(fixture);
    final String result = xmlSerializer.write(json);

    final Diff diff = compareXML(stripWhiteSpace(fixture), stripWhiteSpace(result));

    assertTrue("Found difference: " + diff.toString(), diff.identical());
  }
Ejemplo n.º 8
0
 public void serializeBody(BeanT bean, XMLSerializer target)
     throws SAXException, IOException, XMLStreamException {
   if (superClazz != null) {
     superClazz.serializeBody(bean, target);
   }
   try {
     for (Property<BeanT> p : properties) {
       if (retainPropertyInfo) {
         target.currentProperty.set(p);
       }
       boolean isThereAnOverridingProperty = p.isHiddenByOverride();
       if (!isThereAnOverridingProperty || bean.getClass().equals(jaxbType)) {
         p.serializeBody(bean, target, null);
       } else if (isThereAnOverridingProperty) {
         // need to double check the override - it should be safe to do after the model has been
         // created because it's targeted to override properties only
         Class beanClass = bean.getClass();
         if (Utils.REFLECTION_NAVIGATOR.getDeclaredField(beanClass, p.getFieldName()) == null) {
           p.serializeBody(bean, target, null);
         }
       }
     }
   } catch (AccessorException e) {
     target.reportError(null, e);
   }
 }
Ejemplo n.º 9
0
 void marshal(InMemory o, XMLSerializer out) throws IOException, SAXException, XMLStreamException {
   try {
     core.marshal(_adaptM(XMLSerializer.getInstance(), o), out);
   } catch (MarshalException e) {
     // recover from error by not marshalling this element.
   }
 }
Ejemplo n.º 10
0
 public final void serializeRoot(Object array, XMLSerializer target) throws SAXException {
   target.reportError(
       new ValidationEventImpl(
           ValidationEvent.ERROR,
           Messages.UNABLE_TO_MARSHAL_NON_ELEMENT.format(array.getClass().getName()),
           null,
           null));
 }
Ejemplo n.º 11
0
 public String getId(BeanT bean, XMLSerializer target) throws SAXException {
   if (idProperty != null) {
     try {
       return idProperty.getIdValue(bean);
     } catch (AccessorException e) {
       target.reportError(null, e);
     }
   }
   return null;
 }
Ejemplo n.º 12
0
  @Test
  public void testListSerializer() throws Exception {
    ListAtom list = ListAtom.newAtom();

    ListAtom sublist = list.newList();
    DictionaryAtom subdict = list.newDictionary();
    list.append("string");
    list.append(1.2f);
    list.append(12334);
    list.append(Boolean.FALSE);

    XMLSerializer ser = new XMLSerializer();

    Document jsonString = ser.serializeTo(list);

    ImmutableList fromserializer = ser.serializeFromList(jsonString);

    Assert.assertEquals(list, fromserializer);
  }
Ejemplo n.º 13
0
  @Test
  public void testDictSerializer() throws Exception {
    DictionaryAtom dict = DictionaryAtom.newAtom();

    ListAtom sublist = dict.newList("list");
    DictionaryAtom subdict = dict.newDictionary("dict");
    dict.setString("string", "string");
    dict.setReal("real", 1.2f);
    dict.setInt("int", 12334);
    dict.setBoolean("bool", Boolean.FALSE);

    XMLSerializer ser = new XMLSerializer();

    Document jsonString = ser.serializeTo(dict);

    ImmutableDict fromserializer = ser.serializeFrom(jsonString);

    Assert.assertEquals(dict, fromserializer);
  }
Ejemplo n.º 14
0
 public void serializeRoot(BeanT bean, XMLSerializer target)
     throws SAXException, IOException, XMLStreamException {
   if (tagName == null) {
     Class beanClass = bean.getClass();
     String message;
     if (beanClass.isAnnotationPresent(XmlRootElement.class)) {
       message = Messages.UNABLE_TO_MARSHAL_UNBOUND_CLASS.format(beanClass.getName());
     } else {
       message = Messages.UNABLE_TO_MARSHAL_NON_ELEMENT.format(beanClass.getName());
     }
     target.reportError(new ValidationEventImpl(ValidationEvent.ERROR, message, null, null));
   } else {
     target.startElement(tagName, bean);
     target.childAsSoleContent(bean, null);
     target.endElement();
     if (retainPropertyInfo) {
       target.currentProperty.remove();
     }
   }
 }
Ejemplo n.º 15
0
 public void serializeBody(Object array, XMLSerializer target)
     throws SAXException, IOException, XMLStreamException {
   int len = Array.getLength(array);
   for (int i = 0; i < len; i++) {
     Object item = Array.get(array, i);
     try {
       xducer.writeText(target, item, "arrayItem");
     } catch (AccessorException e) {
       target.reportError("arrayItem", e);
     }
   }
 }
Ejemplo n.º 16
0
 public final void serializeURIs(Object array, XMLSerializer target) throws SAXException {
   if (xducer.useNamespace()) {
     int len = Array.getLength(array);
     for (int i = 0; i < len; i++) {
       Object item = Array.get(array, i);
       try {
         xducer.declareNamespace(item, target);
       } catch (AccessorException e) {
         target.reportError("arrayItem", e);
       }
     }
   }
 }
Ejemplo n.º 17
0
 public void serializeURIs(BeanT bean, XMLSerializer target) throws SAXException {
   try {
     if (retainPropertyInfo) {
       final Property parentProperty = target.getCurrentProperty();
       for (Property<BeanT> p : uriProperties) {
         target.currentProperty.set(p);
         p.serializeURIs(bean, target);
       }
       target.currentProperty.set(parentProperty);
     } else {
       for (Property<BeanT> p : uriProperties) {
         p.serializeURIs(bean, target);
       }
     }
     if (inheritedAttWildcard != null) {
       Map<QName, String> map = inheritedAttWildcard.get(bean);
       target.attWildcardAsURIs(map, null);
     }
   } catch (AccessorException e) {
     target.reportError(null, e);
   }
 }
Ejemplo n.º 18
0
  /** Reports that the type of an object in a property is unexpected. */
  public static void handleTypeMismatchError(
      XMLSerializer serializer, Object parentObject, String fieldName, Object childObject)
      throws AbortSerializationException {

    ValidationEvent ve =
        new ValidationEventImpl(
            ValidationEvent.ERROR, // maybe it should be a fatal error.
            Messages.format(
                Messages.ERR_TYPE_MISMATCH,
                getUserFriendlyTypeName(parentObject),
                fieldName,
                getUserFriendlyTypeName(childObject)),
            new ValidationEventLocatorExImpl(parentObject, fieldName));

    serializer.reportError(ve);
  }
Ejemplo n.º 19
0
  /** Reports a print conversion error while marshalling. */
  public static void handlePrintConversionException(
      Object caller, Exception e, XMLSerializer serializer) throws SAXException {

    if (e instanceof SAXException)
      // assume this exception is not from application.
      // (e.g., when a marshaller aborts the processing, this exception
      //        will be thrown)
      throw (SAXException) e;

    String message = e.getMessage();
    if (message == null) {
      message = e.toString();
    }

    ValidationEvent ve =
        new PrintConversionEventImpl(
            ValidationEvent.ERROR, message, new ValidationEventLocatorImpl(caller), e);
    serializer.reportError(ve);
  }