/** * Hook to throw exception from the middle of a contructor chained call * to this */ private static Marshaller assertionFailed( String message ) throws JAXBException { throw new JAXBException( message ); } }
/** * Create new {@link javax.xml.transform.sax.SAXSource} for a given entity input stream. * * @param spf SAX parser factory to be used to create the SAX source. * @param entityStream entity input stream. * @return new {@link javax.xml.transform.sax.SAXSource} representing the entity input stream. * @throws JAXBException in case SAX source creation fails. */ protected static SAXSource getSAXSource(SAXParserFactory spf, InputStream entityStream) throws JAXBException { try { return new SAXSource(spf.newSAXParser().getXMLReader(), new InputSource(entityStream)); } catch (Exception ex) { throw new JAXBException("Error creating SAXSource", ex); } }
private XMLStreamWriter getXMLStreamWriter(Writer writer) throws JAXBException { try { return Stax2JettisonFactory.createWriter(writer, jsonConfig); } catch (IOException ex) { throw new JAXBException(ex); } }
private static Properties loadJAXBProperties(URL url) throws JAXBException { try { Properties props; logger.log(Level.FINE, "loading props from {0}", url); props = new Properties(); InputStream is = url.openStream(); props.load(is); is.close(); return props; } catch (IOException ioe) { logger.log(Level.FINE, "Unable to load " + url.toString(), ioe); throw new JAXBException(ioe.toString(), ioe); } }
/** * Hook to throw exception from the middle of a contructor chained call * to this */ private static Unmarshaller assertionFailed() throws JAXBException { throw new JAXBException( Messages.format( Messages.RESULT_NULL_CONTEXT ) ); } }
/** * If a key is present in the map, remove the value and return it. */ private static <T> T getPropertyValue(Map<String, Object> properties, String keyName, Class<T> type ) throws JAXBException { Object o = properties.get(keyName); if(o==null) return null; properties.remove(keyName); if(!type.isInstance(o)) throw new JAXBException(Messages.INVALID_PROPERTY_VALUE.format(keyName,o)); else return type.cast(o); }
private Authorizations unmarshallAuthorizations() throws JAXBException { try { final XMLStreamReader xsr = XmlUtils.createSafeReader(new StreamSource(authorizationsFile)); final Unmarshaller unmarshaller = JAXB_AUTHORIZATIONS_CONTEXT.createUnmarshaller(); unmarshaller.setSchema(authorizationsSchema); final JAXBElement<Authorizations> element = unmarshaller.unmarshal(xsr, Authorizations.class); return element.getValue(); } catch (XMLStreamException e) { logger.error("Encountered an error reading authorizations file: ", e); throw new JAXBException("Error reading authorizations file", e); } }
private Tenants unmarshallTenants() throws JAXBException { final Unmarshaller unmarshaller = JAXB_TENANTS_CONTEXT.createUnmarshaller(); unmarshaller.setSchema(tenantsSchema); try { final XMLStreamReader xsr = XmlUtils.createSafeReader(new StreamSource(tenantsFile)); final JAXBElement<Tenants> element = unmarshaller.unmarshal(xsr, Tenants.class); return element.getValue(); } catch (XMLStreamException e) { throw new JAXBException("Error unmarshalling tenants", e); } }
public static ClusterNodeInformation unmarshal(final InputStream is) throws JAXBException { try { final Unmarshaller unmarshaller = JAXB_CONTEXT.createUnmarshaller(); final XMLStreamReader xsr = XmlUtils.createSafeReader(is); return (ClusterNodeInformation) unmarshaller.unmarshal(xsr); } catch (XMLStreamException e) { throw new JAXBException("Error unmarshalling the cluster node information", e); } } }
private static Object instantiateProviderIfNecessary(final Class<?> implClass) throws JAXBException { try { if (JAXBContextFactory.class.isAssignableFrom(implClass)) { return AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() { @Override public Object run() throws Exception { return implClass.newInstance(); } }); } return null; } catch (PrivilegedActionException x) { Throwable e = (x.getCause() == null) ? x : x.getCause(); throw new JAXBException(Messages.format(Messages.COULD_NOT_INSTANTIATE, implClass, e), e); } }
/** * Gets the {@link JaxBeanInfo} object that can handle * the given JAXB-bound class. * * @param fatal * if true, the failure to look up will throw an exception. * Otherwise it will just return null. */ public final <T> JaxBeanInfo<T> getBeanInfo(Class<T> clazz,boolean fatal) throws JAXBException { JaxBeanInfo<T> bi = getBeanInfo(clazz); if(bi!=null) return bi; if(fatal) throw new JAXBException(clazz.getName()+" is not known to this context"); return null; }
public void marshal(Object jaxbElement, File output) throws JAXBException { checkNotNull(jaxbElement, "jaxbElement", output, "output" ); try { OutputStream os = new BufferedOutputStream(new FileOutputStream(output)); try { marshal( jaxbElement, new StreamResult(os) ); } finally { os.close(); } } catch (IOException e) { throw new JAXBException(e); } }
/** * first factoryId should be the preferred one, * more of those can be provided to support backwards compatibility */ private static String classNameFromPackageProperties(URL packagePropertiesUrl, String ... factoryIds) throws JAXBException { logger.log(Level.FINE, "Trying to locate {0}", packagePropertiesUrl.toString()); Properties props = loadJAXBProperties(packagePropertiesUrl); for(String factoryId : factoryIds) { if (props.containsKey(factoryId)) { return props.getProperty(factoryId); } } //Factory key not found String propertiesUrl = packagePropertiesUrl.toExternalForm(); String packageName = propertiesUrl.substring(0, propertiesUrl.indexOf("/jaxb.properties")); throw new JAXBException(Messages.format(Messages.MISSING_PROPERTY, packageName, factoryIds[0])); }
/** * Gets the {@link JaxBeanInfo} object that can handle * the given JAXB-bound object. * * @param fatal * if true, the failure to look up will throw an exception. * Otherwise it will just return null. */ public final JaxBeanInfo getBeanInfo(Object o,boolean fatal) throws JAXBException { JaxBeanInfo bi = getBeanInfo(o); if(bi!=null) return bi; if(fatal) { if(o instanceof Document) throw new JAXBException(Messages.ELEMENT_NEEDED_BUT_FOUND_DOCUMENT.format(o.getClass())); throw new JAXBException(Messages.UNKNOWN_CLASS.format(o.getClass())); } return null; }
@Override public final void writeCollection(Class<?> elementType, Collection<?> t, MediaType mediaType, Charset c, Marshaller m, OutputStream entityStream) throws JAXBException, IOException { final OutputStreamWriter osw = new OutputStreamWriter(entityStream, c); JettisonConfig origJsonConfig = JettisonConfig.DEFAULT; if (m instanceof JettisonConfigured) { origJsonConfig = ((JettisonConfigured) m).getJSONConfiguration(); } final JettisonConfig unwrappingJsonConfig = JettisonConfig.createJSONConfiguration(origJsonConfig); final XMLStreamWriter jxsw = Stax2JettisonFactory.createWriter(osw, unwrappingJsonConfig); final String invisibleRootName = getRootElementName(elementType); try { jxsw.writeStartDocument(); jxsw.writeStartElement(invisibleRootName); for (Object o : t) { m.marshal(o, jxsw); } jxsw.writeEndElement(); jxsw.writeEndDocument(); jxsw.flush(); } catch (XMLStreamException ex) { Logger.getLogger(JettisonListElementProvider.class.getName()).log(Level.SEVERE, null, ex); throw new JAXBException(ex.getMessage(), ex); } }
private static Object tryUsingRI(Marshaller m) throws JAXBException { try { // Try RI suitable one m.setProperty("com.sun.xml.bind.namespacePrefixMapper", new NamespacePrefixMapper() ); log.info("Using NamespacePrefixMapper, which is suitable for the JAXB RI"); prefixMapper = new NamespacePrefixMapper(); return prefixMapper; } catch (java.lang.NoClassDefFoundError notRIEither) { notRIEither.printStackTrace(); log.error("JAXB: neither Reference Implementation nor Java 6 implementation present?", notRIEither); throw new JAXBException("JAXB: neither Reference Implementation nor Java 6 implementation present?"); } catch (javax.xml.bind.PropertyException notRIEither) { notRIEither.printStackTrace(); log.error("JAXB: neither Reference Implementation nor Java 6 implementation present?", notRIEither); throw new JAXBException("JAXB: neither Reference Implementation nor Java 6 implementation present?"); } }
private static Object tryRIforRelationshipsPart(Marshaller m) throws JAXBException { try { // Try RI suitable one m.setProperty("com.sun.xml.bind.namespacePrefixMapper", new NamespacePrefixMapperRelationshipsPart() ); log.info("Using NamespacePrefixMapperRelationshipsPart, which is suitable for the JAXB RI"); prefixMapperRels = new NamespacePrefixMapperRelationshipsPart(); return prefixMapperRels; } catch (java.lang.NoClassDefFoundError notRIEither) { notRIEither.printStackTrace(); log.error("JAXB: neither Reference Implementation nor Java 6 implementation present?", notRIEither); throw new JAXBException("JAXB: neither Reference Implementation nor Java 6 implementation present?"); } catch (javax.xml.bind.PropertyException notRIEither) { notRIEither.printStackTrace(); log.error("JAXB: neither Reference Implementation nor Java 6 implementation present?", notRIEither); throw new JAXBException("JAXB: neither Reference Implementation nor Java 6 implementation present?"); } }
static JAXBContext newInstance(Class[] classes, Map properties, Class spFactory) throws JAXBException { try { ModuleUtil.delegateAddOpensToImplModule(classes, spFactory); Method m = spFactory.getMethod("createContext", Class[].class, Map.class); Object obj = instantiateProviderIfNecessary(spFactory); Object context = m.invoke(obj, classes, properties); if (!(context instanceof JAXBContext)) { // the cast would fail, so generate an exception with a nice message throw handleClassCastException(context.getClass(), JAXBContext.class); } return (JAXBContext) context; } catch (NoSuchMethodException | IllegalAccessException e) { throw new JAXBException(e); } catch (InvocationTargetException e) { // throw if it is exception not to be wrapped // otherwise, wrap with a JAXBException Throwable x = handleInvocationTargetException(e); throw new JAXBException(x); } }
/** * Determine if two types (JAXBContext in this case) will generate a ClassCastException. * * For example, (targetType)originalType * * @param originalType * The Class object of the type being cast * @param targetType * The Class object of the type that is being cast to * @return JAXBException to be thrown. */ private static JAXBException handleClassCastException(Class originalType, Class targetType) { final URL targetTypeURL = which(targetType); return new JAXBException(Messages.format(Messages.ILLEGAL_CAST, // we don't care where the impl class is, we want to know where JAXBContext lives in the impl // class' ClassLoader getClassClassLoader(originalType).getResource("javax/xml/bind/JAXBContext.class"), targetTypeURL)); }
/** * Create an instance of a class using the thread context ClassLoader */ static JAXBContext newInstance(Class[] classes, Map properties, String className) throws JAXBException { Class spi; try { spi = ServiceLoaderUtil.safeLoadClass(className, PLATFORM_DEFAULT_FACTORY_CLASS, getContextClassLoader()); } catch (ClassNotFoundException e) { throw new JAXBException(Messages.format(Messages.DEFAULT_PROVIDER_NOT_FOUND), e); } if (logger.isLoggable(Level.FINE)) { // extra check to avoid costly which operation if not logged logger.log(Level.FINE, "loaded {0} from {1}", new Object[]{className, which(spi)}); } return newInstance(classes, properties, spi); }