Jaxb XSD validationevent isn't working after migration to WebLogic12

一世执手 提交于 2021-02-20 04:53:25

问题


I am trying to validate Jaxb objects against an xsd by marshalling them. This was working fine in weblogic11 but after migration to weblogic12c it isn't working.

For example, even if the mandatory tags are missing, there is no error and xml is being formed successfully!

I noticed event.getLinkedException() is returning null.

ValidateXml.java


import java.io.File;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;

public class ValidateXML
{

  public Map<String, List<Document>> validateDocument(List<Document> documentList, ObjectFactory factory)
  {
    StringWriter stringWriter = new StringWriter();
    List validDoc = new ArrayList();
    List failedDoc = new ArrayList();
    Map output = new HashMap();
    try {
      JAXBContext jc = JAXBContext.newInstance(new Class[] { Documents.class });
      Marshaller m = jc.createMarshaller();
      m.setProperty("jaxb.fragment", Boolean.TRUE);
      m.setProperty("jaxb.formatted.output", Boolean.TRUE);
      m.setProperty("jaxb.encoding", "UTF-8");
      String fileLocation = "// directory path";
      String xsdFile = "//XSD filename "
      fileLocation = fileLocation + xsdFile;
      SchemaFactory sf = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
      Source schemaFile = new StreamSource(new File(fileLocation));
      Schema schema = null;
      try {
        schema = sf.newSchema(schemaFile);
      }
      catch (Exception e) {
         System.out.println("Application Info Batch Description Schema loading Error.." + e.getMessage());
      }
      MyValidationEventHandler errorHandler = null;
      for (int i = 0; i < documentList.size(); i++) {
        Document doc = (Document)documentList.get(i);
       // some business lgic here ... 
        errorHandler = new MyValidationEventHandler();
        m.setSchema(schema);
        m.setEventHandler(errorHandler);
        m.marshal(documents, stringWriter);
        boolean valid = errorHandler.isValidDocument();
        if (valid) {
          validDoc.add(doc);
        }
        else {
          failedDoc.add(doc);
        }
        errorHandler = null;
      }
      errorHandler = null;
    }
    catch (JAXBException e)
    {
       System.out.println("Application Info Batch Description Schema JAXBException Error.." + e.getMessage());
    }
    catch (Exception e) {
       System.out.println("Application Info Batch Description Schema Exception .." + e.getMessage());
    }
    output.put("Valid", validDoc);
    output.put("Error", failedDoc);
    return output;
  }

  
}

MyValidationEventHandler.java



import javax.xml.bind.ValidationEvent;
import javax.xml.bind.ValidationEventHandler;

public class MyValidationEventHandler
  implements ValidationEventHandler
{
  private boolean validCase = true;

  public boolean handleEvent(ValidationEvent event)
  {
    
      System.out.println("Application Info Batch Description MyValidationEventHandler.." + event.getLinkedException());
    
    if (event.getLinkedException() != null) {
      this.validCase = false;
    }
    return true;
  }

  public boolean isValidDocument()
  {
    return this.validCase;
  }
}

Have searched over web but couldn't identify the root cause. It is mentioned in some places that there are some compatibility issues with WebLogic 12. Can someone please help me to resolve or identify the issue and deblock the situation!

Kind regards.

来源:https://stackoverflow.com/questions/65256644/jaxb-xsd-validationevent-isnt-working-after-migration-to-weblogic12

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!