예제 #1
0
 private static XmlBindings getXmlBindings(Object metadata) {
   XmlBindings xmlBindings = null;
   Unmarshaller unmarshaller;
   // only create the JAXBContext for our XmlModel once
   JAXBContext jaxbContext = CompilerHelper.getXmlBindingsModelContext();
   try {
     unmarshaller = jaxbContext.createUnmarshaller();
     if (metadata instanceof File) {
       xmlBindings = (XmlBindings) unmarshaller.unmarshal((File) metadata);
     } else if (metadata instanceof InputSource) {
       xmlBindings = (XmlBindings) unmarshaller.unmarshal((InputSource) metadata);
     } else if (metadata instanceof BufferedInputStream) {
       xmlBindings = (XmlBindings) unmarshaller.unmarshal((BufferedInputStream) metadata);
     } else if (metadata instanceof InputStream) {
       xmlBindings = (XmlBindings) unmarshaller.unmarshal((InputStream) metadata);
     } else if (metadata instanceof Node) {
       xmlBindings = (XmlBindings) unmarshaller.unmarshal((Node) metadata);
     } else if (metadata instanceof Reader) {
       xmlBindings = (XmlBindings) unmarshaller.unmarshal((Reader) metadata);
     } else if (metadata instanceof Source) {
       xmlBindings = (XmlBindings) unmarshaller.unmarshal((Source) metadata);
     } else if (metadata instanceof URL) {
       xmlBindings = (XmlBindings) unmarshaller.unmarshal((URL) metadata);
     } else if (metadata instanceof XMLEventReader) {
       xmlBindings = (XmlBindings) unmarshaller.unmarshal((XMLEventReader) metadata);
     } else if (metadata instanceof XMLStreamReader) {
       xmlBindings = (XmlBindings) unmarshaller.unmarshal((XMLStreamReader) metadata);
     } else if (metadata instanceof String) {
       if (((String) metadata).length() == 0) {
         throw org.eclipse.persistence.exceptions.JAXBException.unableToLoadMetadataFromLocation(
             (String) metadata);
       }
       URL url = null;
       try {
         url = new URL((String) metadata);
       } catch (MalformedURLException ex) {
         url = Thread.currentThread().getContextClassLoader().getResource((String) metadata);
       }
       if (url != null) {
         xmlBindings = (XmlBindings) unmarshaller.unmarshal(url);
       } else {
         // throw exception
         throw org.eclipse.persistence.exceptions.JAXBException.unableToLoadMetadataFromLocation(
             (String) metadata);
       }
     } else {
       throw org.eclipse.persistence.exceptions.JAXBException
           .incorrectValueParameterTypeForOxmXmlKey();
     }
   } catch (JAXBException jaxbEx) {
     throw org.eclipse.persistence.exceptions.JAXBException.couldNotUnmarshalMetadata(jaxbEx);
   }
   return xmlBindings;
 }
예제 #2
0
  private Map getPropertiesFromJSON() throws JAXBException {
    Map props = new HashMap(getProperties());

    if (props != null) {

      Object bindingFilesObject = props.get(JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY);
      if (bindingFilesObject != null) {
        JAXBContext jaxbContext = CompilerHelper.getXmlBindingsModelContext();
        // unmarshal XML
        Unmarshaller u = jaxbContext.createUnmarshaller();
        Marshaller jsonMarshaller = jaxbContext.createMarshaller();
        // Marshal bindings to JSON (with no root)
        jsonMarshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json");
        jsonMarshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, false);
        if (bindingFilesObject instanceof Map) {
          Map<String, Object> bindingFiles = (Map<String, Object>) bindingFilesObject;

          Iterator<String> keyIter = bindingFiles.keySet().iterator();
          while (keyIter.hasNext()) {
            String nextKey = keyIter.next();
            Object nextBindings = bindingFiles.get(nextKey);
            ;
            if (nextBindings instanceof List) {
              List nextList = (List) bindingFiles.get(nextKey);
              for (int i = 0; i < nextList.size(); i++) {
                Object o = nextList.get(i);
                if (o instanceof Source) {
                  Source nextSource = (Source) o;
                  if (nextSource instanceof StreamSource) {
                    StreamSource ss = (StreamSource) nextSource;
                    StreamSource ss2 = new StreamSource(ss.getInputStream());
                    Object unmarshalledFromXML = u.unmarshal(ss2);
                    StringWriter sw = new StringWriter();
                    StreamResult newResult = new StreamResult(sw);
                    jsonMarshaller.marshal(unmarshalledFromXML, newResult);
                    StreamSource newSource = new StreamSource(new StringReader(sw.toString()));
                    nextList.set(i, newSource);
                  }
                }
              }
            } else if (nextBindings instanceof Source) {
              Object unmarshalledFromXML = u.unmarshal((Source) nextBindings);

              StringWriter sw = new StringWriter();
              StreamResult newResult = new StreamResult(sw);
              jsonMarshaller.marshal(unmarshalledFromXML, newResult);
              StreamSource newSource = new StreamSource(new StringReader(sw.toString()));
              bindingFiles.put(nextKey, newSource);
            }
          }
        } else if (bindingFilesObject instanceof List) {
          List bindingFilesList = (List) bindingFilesObject;
          for (int i = 0; i < bindingFilesList.size(); i++) {
            Object next = bindingFilesList.get(i);
            Object unmarshalledFromXML = getXmlBindings(next);
            StringWriter sw = new StringWriter();
            StreamResult newResult = new StreamResult(sw);
            jsonMarshaller.marshal(unmarshalledFromXML, newResult);
            StreamSource newSource = new StreamSource(new StringReader(sw.toString()));
            bindingFilesList.set(i, newSource);
          }
          props.put(JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY, bindingFilesList);
        } else {
          Object unmarshalledFromXML = getXmlBindings(bindingFilesObject);
          StringWriter sw = new StringWriter();
          StreamResult newResult = new StreamResult(sw);
          jsonMarshaller.marshal(unmarshalledFromXML, newResult);
          StreamSource newSource = new StreamSource(new StringReader(sw.toString()));
          props.put(JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY, newSource);
        }
      }
    }
    return props;
  }