apex-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ishark <...@git.apache.org>
Subject [GitHub] incubator-apex-malhar pull request: Added XSD validation to XML pa...
Date Wed, 16 Dec 2015 01:30:15 GMT
Github user ishark commented on a diff in the pull request:

    https://github.com/apache/incubator-apex-malhar/pull/137#discussion_r47726307
  
    --- Diff: library/src/main/java/com/datatorrent/lib/formatter/XmlFormatter.java ---
    @@ -57,45 +55,32 @@ public XmlFormatter()
       }
     
       @Override
    -  public void activate(Context context)
    +  public void setup(OperatorContext context)
       {
    -    if (prettyPrint) {
    -      xstream = new XStream();
    -    } else {
    -      xstream = new XStream(new XppDriver()
    -      {
    -        @Override
    -        public HierarchicalStreamWriter createWriter(Writer out)
    -        {
    -          return new CompactWriter(out, getNameCoder());
    -        }
    -      });
    -    }
    -    if (alias != null) {
    -      try {
    -        xstream.alias(alias, clazz);
    -      } catch (Throwable e) {
    -        throw new RuntimeException("Unable find provided class");
    -      }
    -    }
    -    if (dateFormat != null) {
    -      xstream.registerConverter(new DateConverter(dateFormat, new String[] {}));
    +    JAXBContext ctx;
    +    try {
    +      ctx = JAXBContext.newInstance(getClazz());
    +      marshaller = ctx.createMarshaller();
    +      marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
    +      marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, prettyPrint);
    +    } catch (JAXBException e) {
    +      DTThrowable.wrapIfChecked(e);
         }
       }
     
       @Override
    -  public void deactivate()
    -  {
    -
    -  }
    -
    -  @Override
       public String convert(Object tuple)
       {
         try {
    -      return xstream.toXML(tuple);
    -    } catch (XStreamException e) {
    -      logger.debug("Error while converting tuple {} {} ",tuple,e.getMessage());
    +      StringWriter writer = new StringWriter();
    +      if (getAlias() != null) {
    +        marshaller.marshal(new JAXBElement(new QName(getAlias()), tuple.getClass(), tuple),
writer);
    +      } else {
    +        marshaller.marshal(new JAXBElement(new QName(getClazz().getSimpleName()), tuple.getClass(),
tuple), writer);
    +      }
    +      return writer.toString();
    +    } catch (Exception e) {
    +      logger.debug("Error while converting tuple {} {} ", tuple, e.getMessage());
           return null;
    --- End diff --
    
    This is because we do not want the operator to stop if one of the input strings is incorrect.
The super class emits the tuple to error port if parsing fails.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

Mime
View raw message