public void testRead() throws Exception {
    InputStream carStream =
        getClass()
            .getClassLoader()
            .getResourceAsStream("org/eclipse/persistence/testing/oxm/inheritance/car.xml");
    Object car = unmarshaller.unmarshal(carStream);
    assertTrue("Wrong object returned for subclass", car instanceof Car);

    InputStream vehicleStream =
        getClass()
            .getClassLoader()
            .getResourceAsStream("org/eclipse/persistence/testing/oxm/inheritance/vehicle.xml");
    Object vehicle = unmarshaller.unmarshal(vehicleStream);
    assertTrue("Wrong object returned for superclass", vehicle.getClass().equals(Vehicle.class));
  }
 public void testReadMailingAddress() {
   try {
     InputStream inputStream = ClassLoader.getSystemResourceAsStream(XML_RESOURCE_MAILING);
     MailingAddress testObject = (MailingAddress) unmarshaller.unmarshal(inputStream);
   } catch (ClassCastException e) {
     fail();
   }
 }
 private XMLUnmarshaller getXmlUnmarshaller(Object options) {
   XMLUnmarshaller xmlUnmarshaller = getXmlUnmarshaller().clone();
   if (null == options) {
     return xmlUnmarshaller;
   }
   try {
     DataObject optionsDO = (DataObject) options;
     if (optionsDO.isSet(SDOConstants.ATTACHMENT_UNMARSHALLER_OPTION)) {
       xmlUnmarshaller.setAttachmentUnmarshaller(
           (XMLAttachmentUnmarshaller) optionsDO.get(SDOConstants.ATTACHMENT_UNMARSHALLER_OPTION));
     }
     return xmlUnmarshaller;
   } catch (ClassCastException ccException) {
     throw SDOException.optionsMustBeADataObject(
         ccException, SDOConstants.ORACLE_SDO_URL, SDOConstants.XMLHELPER_LOAD_OPTIONS);
   }
 }
 public void testReadBillingAddress() {
   try {
     InputStream inputStream = ClassLoader.getSystemResourceAsStream(XML_RESOURCE_BILLING);
     Object testObject = unmarshaller.unmarshal(inputStream);
     fail();
   } catch (XMLMarshalException e) {
     // PASS - an XMLMarshalException should be caught
   }
 }
  public XMLUnmarshaller getXmlUnmarshaller() {
    XMLUnmarshaller unmarshaller = xmlUnmarshallerMap.get(Thread.currentThread());

    if (null == unmarshaller) {
      unmarshaller = getXmlContext().createUnmarshaller();
      unmarshaller.getProperties().put(SDOConstants.SDO_HELPER_CONTEXT, aHelperContext);
      unmarshaller.setUnmappedContentHandlerClass(SDOUnmappedContentHandler.class);
      unmarshaller.setUnmarshalListener(new SDOUnmarshalListener(aHelperContext));
      unmarshaller.setResultAlwaysXMLRoot(true);
      xmlUnmarshallerMap.put(Thread.currentThread(), unmarshaller);
    }

    XMLContext context = getXmlContext();
    if (unmarshaller.getXMLContext() != context) {
      unmarshaller.setXMLContext(context);
    }
    return unmarshaller;
  }
  public Object valueFromRow(
      AbstractRecord row,
      JoinedAttributeManager joinManager,
      ObjectBuildingQuery query,
      AbstractSession executionSession) {
    ContainerPolicy cp = this.getContainerPolicy();

    Object fieldValue = row.getValues(this.getField());
    if (fieldValue == null) {
      if (reuseContainer) {
        Object currentObject = ((XMLRecord) row).getCurrentObject();
        Object container = getAttributeAccessor().getAttributeValueFromObject(currentObject);
        return container != null ? container : cp.containerInstance();
      } else {
        return cp.containerInstance();
      }
    }

    Vector fieldValues = this.getDescriptor().buildDirectValuesFromFieldValue(fieldValue);
    if (fieldValues == null) {
      if (reuseContainer) {
        Object currentObject = ((XMLRecord) row).getCurrentObject();
        Object container = getAttributeAccessor().getAttributeValueFromObject(currentObject);
        return container != null ? container : cp.containerInstance();
      } else {
        return cp.containerInstance();
      }
    }

    Object result = null;
    if (reuseContainer) {
      Object currentObject = ((XMLRecord) row).getCurrentObject();
      Object container = getAttributeAccessor().getAttributeValueFromObject(currentObject);
      result = container != null ? container : cp.containerInstance();
    } else {
      result = cp.containerInstance(fieldValues.size());
    }

    for (Enumeration stream = fieldValues.elements(); stream.hasMoreElements(); ) {
      Object element = stream.nextElement();

      // PERF: Direct variable access.
      // Object value = row.get(field);
      // Object fieldValue = null;
      XMLUnmarshaller unmarshaller = ((XMLRecord) row).getUnmarshaller();
      if (element instanceof String) {
        if (this.isSwaRef() && (unmarshaller.getAttachmentUnmarshaller() != null)) {
          fieldValue =
              unmarshaller.getAttachmentUnmarshaller().getAttachmentAsDataHandler((String) element);
        } else if (!this.isSwaRef()) {
          // should be base64
          byte[] bytes =
              ((XMLConversionManager)
                      executionSession.getDatasourcePlatform().getConversionManager())
                  .convertSchemaBase64ToByteArray(element);
          fieldValue = bytes;
        }
      } else {
        // this was an element, so do the XOP/SWAREF/Inline binary cases for an element
        XMLRecord record = (XMLRecord) element;
        record.setSession(executionSession);

        if ((unmarshaller.getAttachmentUnmarshaller() != null)
            && unmarshaller.getAttachmentUnmarshaller().isXOPPackage()
            && !this.isSwaRef()
            && !this.shouldInlineBinaryData()) {
          // look for the include element:
          String xpath = XMLConstants.EMPTY_STRING;

          //  need a prefix for XOP
          String prefix = null;
          NamespaceResolver descriptorResolver =
              ((XMLDescriptor) getDescriptor()).getNamespaceResolver();
          if (descriptorResolver != null) {
            prefix = descriptorResolver.resolveNamespaceURI(XMLConstants.XOP_URL);
          }
          if (prefix == null) {
            prefix = XMLConstants.XOP_PREFIX;
          }
          NamespaceResolver tempResolver = new NamespaceResolver();
          tempResolver.put(prefix, XMLConstants.XOP_URL);
          xpath = prefix + XMLConstants.COLON + INCLUDE + "/@href";
          XMLField field = new XMLField(xpath);
          field.setNamespaceResolver(tempResolver);
          String includeValue = (String) record.get(field);
          if (element != null && includeValue != null) {
            if ((getAttributeElementClass() == ClassConstants.ABYTE)
                || (getAttributeElementClass() == ClassConstants.APBYTE)) {
              fieldValue =
                  unmarshaller.getAttachmentUnmarshaller().getAttachmentAsByteArray(includeValue);
            } else {
              fieldValue =
                  unmarshaller.getAttachmentUnmarshaller().getAttachmentAsDataHandler(includeValue);
            }
          } else {
            // If we didn't find the Include element, check for inline
            fieldValue = record.get(XMLConstants.TEXT);
            // should be a base64 string
            fieldValue =
                ((XMLConversionManager)
                        executionSession.getDatasourcePlatform().getConversionManager())
                    .convertSchemaBase64ToByteArray(fieldValue);
          }
        } else if ((unmarshaller.getAttachmentUnmarshaller() != null) && isSwaRef()) {
          String refValue = (String) record.get(XMLConstants.TEXT);
          if (refValue != null) {
            fieldValue =
                unmarshaller.getAttachmentUnmarshaller().getAttachmentAsDataHandler(refValue);
          }
        } else {
          fieldValue = record.get(XMLConstants.TEXT);
          // should be a base64 string
          fieldValue =
              ((XMLConversionManager)
                      executionSession.getDatasourcePlatform().getConversionManager())
                  .convertSchemaBase64ToByteArray(fieldValue);
        }
      }
      Object attributeValue = fieldValue;
      if (getValueConverter() != null) {
        if (getValueConverter() instanceof XMLConverter) {
          attributeValue =
              ((XMLConverter) getValueConverter())
                  .convertDataValueToObjectValue(fieldValue, executionSession, unmarshaller);
        } else {
          attributeValue =
              getValueConverter().convertDataValueToObjectValue(fieldValue, executionSession);
        }
      }

      cp.addInto(attributeValue, result, query.getSession());
    }
    return result;
  }
 @Test
 public void checkWSDL() throws WSDLException {
   DBWS_BUILDER_XML_USERNAME =
       "******"1.0\" encoding=\"UTF-8\"?>"
           + "<dbws-builder xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">"
           + "<properties>"
           + "<property name=\"projectName\">simpletable2</property>"
           + "<property name=\"logLevel\">off</property>"
           + "<property name=\"username\">";
   DBWS_BUILDER_XML_PASSWORD = "******"password\">";
   DBWS_BUILDER_XML_URL = "</property><property name=\"url\">";
   DBWS_BUILDER_XML_DRIVER = "</property><property name=\"driver\">";
   DBWS_BUILDER_XML_PLATFORM = "</property><property name=\"platformClassname\">";
   DBWS_BUILDER_XML_MAIN =
       "</property>"
           + "</properties>"
           + "<table "
           + "schemaPattern=\"%\" "
           + "tableNamePattern=\"simpletable2\" "
           + ">"
           + "<sql "
           + "name=\"findByName\" "
           + "isCollection=\"true\" "
           + "returnType=\"simpletable2Type\" "
           + ">"
           + "<text><![CDATA[select * from SIMPLETABLE2 where NAME like ?]]></text>"
           + "<binding name=\"NAME\" type=\"xsd:string\"/>"
           + "</sql>"
           + "</table>"
           + "</dbws-builder>";
   String username = System.getProperty(DATABASE_USERNAME_KEY, DEFAULT_DATABASE_USERNAME);
   String password = System.getProperty(DATABASE_PASSWORD_KEY, DEFAULT_DATABASE_PASSWORD);
   String url = System.getProperty(DATABASE_URL_KEY, DEFAULT_DATABASE_URL);
   String builderString =
       DBWS_BUILDER_XML_USERNAME
           + username
           + DBWS_BUILDER_XML_PASSWORD
           + password
           + DBWS_BUILDER_XML_URL
           + url
           + DBWS_BUILDER_XML_DRIVER
           + DATABASE_DRIVER
           + DBWS_BUILDER_XML_PLATFORM
           + DATABASE_PLATFORM
           + DBWS_BUILDER_XML_MAIN;
   XMLContext context = new XMLContext(new DBWSBuilderModelProject());
   XMLUnmarshaller unmarshaller = context.createUnmarshaller();
   DBWSBuilderModel builderModel =
       (DBWSBuilderModel) unmarshaller.unmarshal(new StringReader(builderString));
   DBWSBuilder builder = new DBWSBuilder();
   builder.quiet = true;
   // builder.setLogLevel(SessionLog.FINE_LABEL);
   builder.setLogLevel(SessionLog.OFF_LABEL);
   builder.setPlatformClassname(DATABASE_PLATFORM);
   builder.properties = builderModel.properties;
   builder.getProperties().put(SESSIONS_FILENAME_KEY, NO_SESSIONS_FILENAME);
   builder.operations = builderModel.operations;
   builder.setPackager(
       new JSR109WebServicePackager(null, "WebServiceTestPackager", noArchive) {
         @Override
         public void start() {
           // do nothing - don't have to verify existence of 'stageDir' when
           // all the streams are in-memory
         }
       });
   ByteArrayOutputStream dbwsServiceStream = new ByteArrayOutputStream();
   ByteArrayOutputStream wsdlStream = new ByteArrayOutputStream();
   builder.build(
       __nullStream,
       __nullStream,
       dbwsServiceStream,
       __nullStream,
       __nullStream,
       __nullStream,
       __nullStream,
       wsdlStream,
       __nullStream,
       __nullStream,
       __nullStream,
       __nullStream,
       null);
   // verify that the generated WSDL has the correct response type
   // for the nested sql operation 'findByName'
   try {
     StringWriter sw = new StringWriter();
     StreamSource wsdlStreamSource = new StreamSource(new StringReader(wsdlStream.toString()));
     Transformer t =
         TransformerFactory.newInstance()
             .newTransformer(new StreamSource(new StringReader(MATCH_SCHEMA)));
     StreamResult streamResult = new StreamResult(sw);
     t.transform(wsdlStreamSource, streamResult);
     sw.toString();
     SchemaModelProject schemaProject = new SchemaModelProject();
     XMLContext xmlContext2 = new XMLContext(schemaProject);
     unmarshaller = xmlContext2.createUnmarshaller();
     Schema schema = (Schema) unmarshaller.unmarshal(new StringReader(sw.toString()));
     ComplexType findByNameResponseType =
         (ComplexType) schema.getTopLevelComplexTypes().get(FINDBYNAME_RESPONSETYPE);
     Element result = (Element) findByNameResponseType.getSequence().getElements().get(0);
     Element unnamed = (Element) result.getComplexType().getSequence().getElements().get(0);
     assertTrue(
         "wrong refType for " + FINDBYNAME_RESPONSETYPE, TABLE_ALIAS.equals(unnamed.getRef()));
   } catch (Exception e) {
     fail(e.getMessage());
   }
 }
  public XMLDocument load(Source source, String locationURI, Object options) throws IOException {
    // get XMLUnmarshaller once - as we may create a new instance if this helper isDirty=true
    XMLUnmarshaller anXMLUnmarshaller = getXmlUnmarshaller(options);
    Object unmarshalledObject = null;
    if (options == null) {
      try {
        unmarshalledObject = anXMLUnmarshaller.unmarshal(source);
      } catch (XMLMarshalException xmlException) {
        handleXMLMarshalException(xmlException);
      }
    } else {
      try {
        DataObject optionsDataObject = (DataObject) options;
        try {
          SDOType theType = (SDOType) optionsDataObject.get(SDOConstants.TYPE_LOAD_OPTION);
          try {
            if (theType != null) {
              unmarshalledObject = anXMLUnmarshaller.unmarshal(source, theType.getImplClass());
            } else {
              unmarshalledObject = anXMLUnmarshaller.unmarshal(source);
            }
          } catch (XMLMarshalException xmlException) {
            handleXMLMarshalException(xmlException);
          }
        } catch (ClassCastException ccException) {
          throw SDOException.typePropertyMustBeAType(ccException);
        }
      } catch (ClassCastException ccException) {
        throw SDOException.optionsMustBeADataObject(
            ccException, SDOConstants.ORACLE_SDO_URL, SDOConstants.XMLHELPER_LOAD_OPTIONS);
      }
    }

    if (unmarshalledObject instanceof XMLRoot) {
      XMLRoot xmlRoot = (XMLRoot) unmarshalledObject;
      XMLDocument xmlDocument =
          createDocument(
              (DataObject) ((XMLRoot) unmarshalledObject).getObject(),
              ((XMLRoot) unmarshalledObject).getNamespaceURI(),
              ((XMLRoot) unmarshalledObject).getLocalName());
      if (xmlRoot.getEncoding() != null) {
        xmlDocument.setEncoding(xmlRoot.getEncoding());
      }
      if (xmlRoot.getXMLVersion() != null) {
        xmlDocument.setXMLVersion(xmlRoot.getXMLVersion());
      }
      xmlDocument.setSchemaLocation(xmlRoot.getSchemaLocation());
      xmlDocument.setNoNamespaceSchemaLocation(xmlRoot.getNoNamespaceSchemaLocation());
      return xmlDocument;
    } else if (unmarshalledObject instanceof DataObject) {
      DataObject unmarshalledDataObject = (DataObject) unmarshalledObject;
      String localName =
          ((SDOType) ((DataObject) unmarshalledObject).getType())
              .getXmlDescriptor()
              .getDefaultRootElement();
      if (localName == null) {
        localName = ((SDOType) ((DataObject) unmarshalledObject).getType()).getXsdLocalName();
      }
      return createDocument(
          unmarshalledDataObject, unmarshalledDataObject.getType().getURI(), localName);
    } else if (unmarshalledObject instanceof XMLDocument) {
      return (XMLDocument) unmarshalledObject;
    }
    return null;
  }