public void testCreateContext() throws Exception { try { JAXBContextFactory.createContext(new Class[] {InvalidChild.class}, null); } catch (javax.xml.bind.JAXBException e) { JAXBException moxyException = (JAXBException) e.getCause(); assertEquals(JAXBException.SUBCLASS_CANNOT_HAVE_XMLVALUE, moxyException.getErrorCode()); return; } fail(); }
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; }
public void testCreateConcreteClassWithMultiArgConstructor() throws JAXBException { try { Class[] classes = new Class[1]; classes[0] = ConcreteClassWithMultiArgConstructor.class; JAXBContextFactory.createContext(classes, null); } catch (JAXBException e) { org.eclipse.persistence.exceptions.JAXBException je = (org.eclipse.persistence.exceptions.JAXBException) e.getLinkedException(); assertEquals( org.eclipse.persistence.exceptions.JAXBException.FACTORY_METHOD_OR_ZERO_ARG_CONST_REQ, je.getErrorCode()); return; } fail(); }
/** * Handle xml-any-attribute. * * @param xmlAnyAttribute * @param oldProperty * @param tInfo * @param javaType * @return */ private Property processXmlAnyAttribute( XmlAnyAttribute xmlAnyAttribute, Property oldProperty, TypeInfo tInfo, JavaType javaType) { // if oldProperty is already an Any (via @XmlAnyAttribute annotation) // there's nothing to do if (oldProperty.isAnyAttribute()) { return oldProperty; } // type has to be a java.util.Map if (!oldProperty.getType().getName().equals("java.util.Map")) { throw org.eclipse.persistence.exceptions.JAXBException.anyAttributeOnNonMap( oldProperty.getPropertyName()); } // reset any existing values resetProperty(oldProperty, tInfo); oldProperty.setIsAnyAttribute(true); tInfo.setAnyAttributePropertyName(oldProperty.getPropertyName()); return oldProperty; }
/** * XmlElement override will completely replace the existing values. * * @param xmlElement * @param oldProperty * @param typeInfo * @param nsInfo * @return */ private Property processXmlElement( XmlElement xmlElement, Property oldProperty, TypeInfo typeInfo, NamespaceInfo nsInfo) { // reset any existing values resetProperty(oldProperty, typeInfo); if (xmlElement.getXmlMap() != null) { processXmlMap(xmlElement.getXmlMap(), oldProperty); } // handle xml-id if (xmlElement.isXmlId()) { typeInfo.setIDProperty(oldProperty); } else if (oldProperty.isXmlId()) { // account for XmlID un-set via XML if (typeInfo.getIDProperty() != null && typeInfo.getIDProperty().getPropertyName().equals(oldProperty.getPropertyName())) { typeInfo.setIDProperty(null); } } oldProperty.setIsXmlId(xmlElement.isXmlId()); // handle xml-idref oldProperty.setIsXmlIdRef(xmlElement.isXmlIdref()); // set required oldProperty.setIsRequired(xmlElement.isRequired()); // set xml-inline-binary-data oldProperty.setisInlineBinaryData(xmlElement.isXmlInlineBinaryData()); // set nillable oldProperty.setNillable(xmlElement.isNillable()); // set defaultValue if (xmlElement.getDefaultValue().equals("\u0000")) { oldProperty.setDefaultValue(null); } else { oldProperty.setDefaultValue(xmlElement.getDefaultValue()); } // set schema name QName qName; String name = xmlElement.getName(); if (name.equals("##default")) { name = oldProperty.getPropertyName(); } if (xmlElement.getNamespace().equals("##default")) { if (nsInfo.isElementFormQualified()) { qName = new QName(nsInfo.getNamespace(), name); } else { qName = new QName(name); } } else { qName = new QName(xmlElement.getNamespace(), name); } oldProperty.setSchemaName(qName); // set type if (xmlElement.getType().equals("javax.xml.bind.annotation.XmlElement.DEFAULT")) { // if xmlElement has no type, and the property type was set via // @XmlElement, reset it to the original value if (oldProperty.isXmlElementType()) { oldProperty.setType(oldProperty.getOriginalType()); } } else { if (xmlElement.getXmlMap() != null) { getLogger() .logWarning( JAXBMetadataLogger.INVALID_TYPE_ON_MAP, new Object[] {xmlElement.getName()}); } else { oldProperty.setType(jModelInput.getJavaModel().getClass(xmlElement.getType())); } } // handle XmlJavaTypeAdapter if (xmlElement.getXmlJavaTypeAdapter() != null) { oldProperty.setXmlJavaTypeAdapter(xmlElement.getXmlJavaTypeAdapter()); } // handle XmlElementWrapper if (xmlElement.getXmlElementWrapper() != null) { oldProperty.setXmlElementWrapper(xmlElement.getXmlElementWrapper()); } // for primitives we always set required, a.k.a. minOccurs="1" if (!oldProperty.isRequired()) { JavaClass ptype = oldProperty.getActualType(); oldProperty.setIsRequired( ptype.isPrimitive() || ptype.isArray() && ptype.getComponentType().isPrimitive()); } // handle xml-list if (xmlElement.isSetXmlList()) { // Make sure XmlList annotation is on a collection or array if (!aProcessor.isCollectionType(oldProperty) && !oldProperty.getType().isArray()) { throw JAXBException.invalidList(oldProperty.getPropertyName()); } oldProperty.setIsXmlList(xmlElement.isXmlList()); } // handle xml-mime-type if (xmlElement.getXmlMimeType() != null) { oldProperty.setMimeType(xmlElement.getXmlMimeType()); } // handle xml-attachment-ref if (xmlElement.isXmlAttachmentRef()) { oldProperty.setIsSwaAttachmentRef(true); oldProperty.setSchemaType(XMLConstants.SWA_REF_QNAME); } // handle xml-schema-type if (xmlElement.getXmlSchemaType() != null) { oldProperty.setSchemaType( new QName( xmlElement.getXmlSchemaType().getNamespace(), xmlElement.getXmlSchemaType().getName())); } return oldProperty; }
/** * Set a property on the JAXBMarshaller. Attempting to set any unsupported property will result in * a javax.xml.bind.PropertyException * * @see org.eclipse.persistence.jaxb.MarshallerProperties */ public void setProperty(String key, Object value) throws PropertyException { try { if (key == null) { throw new IllegalArgumentException(); } else if (Constants.JAXB_FRAGMENT.equals(key)) { if (value == null) { throw new PropertyException(key, Constants.EMPTY_STRING); } Boolean fragment = (Boolean) value; xmlMarshaller.setFragment(fragment.booleanValue()); } else if (JAXB_FORMATTED_OUTPUT.equals(key)) { if (value == null) { throw new PropertyException(key, Constants.EMPTY_STRING); } Boolean formattedOutput = (Boolean) value; xmlMarshaller.setFormattedOutput(formattedOutput.booleanValue()); } else if (JAXB_ENCODING.equals(key)) { xmlMarshaller.setEncoding((String) value); } else if (JAXB_SCHEMA_LOCATION.equals(key)) { xmlMarshaller.setSchemaLocation((String) value); } else if (JAXB_NO_NAMESPACE_SCHEMA_LOCATION.equals(key)) { xmlMarshaller.setNoNamespaceSchemaLocation((String) value); } else if (MarshallerProperties.NAMESPACE_PREFIX_MAPPER.equals(key)) { if (value == null) { xmlMarshaller.setNamespacePrefixMapper(null); } else if (value instanceof Map) { NamespacePrefixMapper namespacePrefixMapper = new MapNamespacePrefixMapper((Map) value); xmlMarshaller.setNamespacePrefixMapper(namespacePrefixMapper); } else { xmlMarshaller.setNamespacePrefixMapper((NamespacePrefixMapper) value); } } else if (SUN_NAMESPACE_PREFIX_MAPPER.equals(key) || SUN_JSE_NAMESPACE_PREFIX_MAPPER.equals(key)) { if (value == null) { xmlMarshaller.setNamespacePrefixMapper(null); } else { xmlMarshaller.setNamespacePrefixMapper(new NamespacePrefixMapperWrapper(value)); } } else if (MarshallerProperties.INDENT_STRING.equals(key) || SUN_INDENT_STRING.equals(key) || SUN_JSE_INDENT_STRING.equals(key)) { xmlMarshaller.setIndentString((String) value); } else if (MarshallerProperties.JSON_MARSHAL_EMPTY_COLLECTIONS.equals(key)) { xmlMarshaller.setMarshalEmptyCollections((Boolean) value); } else if (MarshallerProperties.JSON_REDUCE_ANY_ARRAYS.equals(key)) { xmlMarshaller.setReduceAnyArrays((Boolean) value); } else if (MarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME.equals(key)) { xmlMarshaller.setWrapperAsCollectionName((Boolean) value); } else if (MarshallerProperties.CHARACTER_ESCAPE_HANDLER.equals(key)) { xmlMarshaller.setCharacterEscapeHandler((CharacterEscapeHandler) value); } else if (SUN_CHARACTER_ESCAPE_HANDLER.equals(key) || SUN_JSE_CHARACTER_ESCAPE_HANDLER.equals(key)) { if (value == null) { xmlMarshaller.setCharacterEscapeHandler(null); } else { xmlMarshaller.setCharacterEscapeHandler(new CharacterEscapeHandlerWrapper(value)); } } else if (XML_DECLARATION.equals(key)) { if (value == null) { throw new PropertyException(key, Constants.EMPTY_STRING); } Boolean fragment = !(Boolean) value; xmlMarshaller.setFragment(fragment.booleanValue()); } else if (XML_HEADERS.equals(key)) { xmlMarshaller.setXmlHeader((String) value); } else if (OBJECT_IDENTITY_CYCLE_DETECTION.equals(key)) { if (value == null) { throw new PropertyException(key, Constants.EMPTY_STRING); } xmlMarshaller.setEqualUsingIdenity(((Boolean) value).booleanValue()); } else if (MarshallerProperties.MEDIA_TYPE.equals(key)) { MediaType mType = null; if (value instanceof MediaType) { mType = (MediaType) value; } else if (value instanceof String) { mType = MediaType.getMediaType((String) value); } if (mType == null) { throw new PropertyException(key, Constants.EMPTY_STRING); } xmlMarshaller.setMediaType(mType); } else if (MarshallerProperties.JSON_ATTRIBUTE_PREFIX.equals(key)) { xmlMarshaller.setAttributePrefix((String) value); } else if (MarshallerProperties.JSON_INCLUDE_ROOT.equals(key)) { if (value == null) { throw new PropertyException(key, Constants.EMPTY_STRING); } xmlMarshaller.setIncludeRoot((Boolean) value); } else if (MarshallerProperties.JSON_VALUE_WRAPPER.equals(key)) { if (value == null || (((String) value).length() == 0)) { throw new PropertyException(key, Constants.EMPTY_STRING); } xmlMarshaller.setValueWrapper((String) value); } else if (MarshallerProperties.JSON_NAMESPACE_SEPARATOR.equals(key)) { if (value == null) { throw new PropertyException(key, Constants.EMPTY_STRING); } xmlMarshaller.setNamespaceSeparator((Character) value); } else if (MarshallerProperties.OBJECT_GRAPH.equals(key)) { if (value == null) { xmlMarshaller.setMarshalAttributeGroup(null); } else if (value instanceof ObjectGraphImpl) { xmlMarshaller.setMarshalAttributeGroup(((ObjectGraphImpl) value).getAttributeGroup()); } else if (value.getClass() == ClassConstants.STRING) { xmlMarshaller.setMarshalAttributeGroup(value); } else { throw org.eclipse.persistence.exceptions.JAXBException.invalidValueForObjectGraph(value); } } else { throw new PropertyException(key, value); } } catch (ClassCastException exception) { throw new PropertyException(key, exception); } }