/** * Delegates the marshalling to the wrapped marshaller. */ @Override public void marshal(final Object object, final OutputStream output) throws JAXBException { final FilterVersion version = getFilterVersion(); if (version != null) try { marshal(object, OutputFactory.createXMLStreamWriter(output, getEncoding()), version); } catch (XMLStreamException e) { throw new JAXBException(e); } else { // Marshalling to the default GML version. final Context context = begin(); try { marshaller.marshal(toImplementation(object), output); } finally { context.finish(); } } }
/** * Converts the given arbitrary object to an implementation having JAXB annotations. * If the given object is not recognized or is already an instance of the expected class, * then it is returned unchanged. */ private Object toImplementation(final Object value) { specificBitMasks = value.getClass().isAnnotationPresent(UseLegacyMetadata.class) ? Context.LEGACY_METADATA : 0; final UnaryOperator<Object>[] converters = getRootAdapters(); if (converters != null) { for (final UnaryOperator<Object> t : converters) { final Object c = t.apply(value); if (c != null) return c; } } return value; }
/** * Creates a pooled marshaller wrapping the given one. * Callers shall invoke {@link #reset(Pooled)} after construction for completing the initialization. * * @param marshaller the marshaller to use for the actual work. * @param template the {@link PooledTemplate} from which to get the initial values. * @throws JAXBException if an error occurred while setting a property. */ PooledMarshaller(final Marshaller marshaller, final Pooled template) throws JAXBException { super(template); this.marshaller = marshaller; initialize(template); }
/** * Delegates the marshalling to the wrapped marshaller. */ @Override public void marshal(final Object object, XMLStreamWriter output) throws JAXBException { final FilterVersion version = getFilterVersion(); if (version != null) { output = new FilteredStreamWriter(output, version); } final Context context = begin(); try { marshaller.marshal(toImplementation(object), output); } finally { context.finish(); } }
/** * Delegates to the wrapped marshaller. The initial state will be saved * if it was not already done, for future restoration by {@link #reset(Pooled)}. */ @Override public void setAttachmentMarshaller(final AttachmentMarshaller am) { if (!isPropertySaved(AttachmentMarshaller.class)) { saveProperty(AttachmentMarshaller.class, marshaller.getAttachmentMarshaller()); } marshaller.setAttachmentMarshaller(am); }
/** * Delegates the marshalling to the wrapped marshaller. */ @Override public void marshal(Object object, final OutputStream output) throws JAXBException { object = toImplementation(object); // Must be call before getTransformVersion() final TransformVersion version = getTransformVersion(); if (version != null) try { marshal(object, OutputFactory.createXMLEventWriter(output, getEncoding()), version); } catch (XMLStreamException e) { throw new JAXBException(e); } else { // Marshalling to the default GML version. final Context context = begin(); try { marshaller.marshal(object, output); } finally { context.finish(); } } }
/** * Delegates the marshalling to the wrapped marshaller. */ @Override public Node getNode(Object object) throws JAXBException { object = toImplementation(object); // Must be call before getTransformVersion() final TransformVersion version = getTransformVersion(); if (version != null) { // This exception is thrown by javax.xml.bind.helpers.AbstractMarshallerImpl anyway. throw new UnsupportedOperationException(); } else { final Context context = begin(); try { return marshaller.getNode(object); } finally { context.finish(); } } }
/** * Marshals to the given output with on-the-fly substitution of namespaces. * This method is invoked only when the user asked to marshal to a different GML version * than the one supported natively by SIS, i.e. when {@link #getFilterVersion()} returns * a non-null value. * * @param object the object to marshall. * @param output the writer created by SIS (<b>not</b> the writer given by the user). * @param version identifies the namespace substitutions to perform. */ private void marshal(final Object object, XMLStreamWriter output, final FilterVersion version) throws XMLStreamException, JAXBException { output = new FilteredStreamWriter(output, version); final Context context = begin(); try { marshaller.marshal(toImplementation(object), output); } finally { context.finish(); } output.close(); // Despite its name, this method does not close the underlying output stream. }
/** * Returns a JAXB marshaller from the pool. If there is no marshaller currently available * in the pool, then this method will {@linkplain #createMarshaller() create} a new one. * * <p>This method shall be used as below:</p> * * {@preformat java * Marshaller marshaller = pool.acquireMarshaller(); * marshaller.marchall(...); * pool.recycle(marshaller); * } * * Note that {@link #recycle(Marshaller)} shall not be invoked in case of exception, * since the marshaller may be in an invalid state. * * @return a marshaller configured for formatting OGC/ISO XML. * @throws JAXBException if an error occurred while creating and configuring a marshaller. */ public Marshaller acquireMarshaller() throws JAXBException { Marshaller marshaller = marshallers.poll(); if (marshaller == null) { marshaller = new PooledMarshaller(createMarshaller(), template); } return marshaller; }
/** * Marshals to the given output with on-the-fly substitution of namespaces. * This method is invoked when the user asked to marshal to a different GML or metadata version than the * one supported natively by SIS, i.e. when {@link #getTransformVersion()} returns a non-null value. * * @param object the object to marshal. * @param output the writer created by SIS (<b>not</b> the writer given by the user). * @param version identifies the namespace substitutions to perform. */ private void marshal(Object object, XMLEventWriter output, final TransformVersion version) throws XMLStreamException, JAXBException { output = new TransformingWriter(output, version); final Context context = begin(); try { marshaller.marshal(object, output); } finally { context.finish(); } output.close(); // Despite its name, this method does not close the underlying output stream. }
/** * Delegates the marshalling to the wrapped marshaller. */ @Override public void marshal(Object object, final File output) throws JAXBException { object = toImplementation(object); // Must be call before getTransformVersion() final TransformVersion version = getTransformVersion(); if (version != null) try { try (OutputStream s = new BufferedOutputStream(new FileOutputStream(output))) { marshal(object, OutputFactory.createXMLEventWriter(s, getEncoding()), version); } } catch (IOException | XMLStreamException e) { throw new JAXBException(e); } else { // Marshalling to the default GML version. final Context context = begin(); try { marshaller.marshal(object, output); } finally { context.finish(); } } }
/** * Delegates to the wrapped marshaller. The initial state will be saved * if it was not already done, for future restoration by {@link #reset(Pooled)}. */ @Override public void setListener(final Listener listener) { if (!isPropertySaved(Listener.class)) { saveProperty(Listener.class, marshaller.getListener()); } marshaller.setListener(listener); }
/** * Delegates the marshalling to the wrapped marshaller. */ @Override public Node getNode(final Object object) throws JAXBException { final FilterVersion version = getFilterVersion(); if (version != null) { // This exception is thrown by javax.xml.bind.helpers.AbstractMarshallerImpl anyway. throw new UnsupportedOperationException(); } else { final Context context = begin(); try { return marshaller.getNode(toImplementation(object)); } finally { context.finish(); } } }
/** * Delegates the marshalling to the wrapped marshaller. */ @Override public void marshal(Object object, XMLEventWriter output) throws JAXBException { object = toImplementation(object); // Must be call before getTransformVersion() final TransformVersion version = getTransformVersion(); if (version != null) { output = new TransformingWriter(output, version); } final Context context = begin(); try { marshaller.marshal(object, output); } finally { context.finish(); } }
/** * Returns a JAXB marshaller from the pool. If there is no marshaller currently available * in the pool, then this method will {@linkplain #createMarshaller() create} a new one. * * <p>This method shall be used as below:</p> * * {@preformat java * Marshaller marshaller = pool.acquireMarshaller(); * marshaller.marchall(...); * pool.recycle(marshaller); * } * * Note that {@link #recycle(Marshaller)} shall not be invoked in case of exception, * since the marshaller may be in an invalid state. * * @return a marshaller configured for formatting OGC/ISO XML. * @throws JAXBException if an error occurred while creating and configuring a marshaller. */ public Marshaller acquireMarshaller() throws JAXBException { Marshaller marshaller = marshallers.poll(); if (marshaller == null) { marshaller = new PooledMarshaller(createMarshaller(), template); } return marshaller; }
/** * Delegates the marshalling to the wrapped marshaller. */ @Override public void marshal(final Object object, final Node output) throws JAXBException { final FilterVersion version = getFilterVersion(); if (version != null) try { marshal(object, OutputFactory.createXMLStreamWriter(output), version); } catch (XMLStreamException e) { throw new JAXBException(e); } else { // Marshalling to the default GML version. final Context context = begin(); try { marshaller.marshal(toImplementation(object), output); } finally { context.finish(); } } }
/** * Delegates the marshalling to the wrapped marshaller. */ @Override public void marshal(Object object, final ContentHandler output) throws JAXBException { object = toImplementation(object); // Must be call before getTransformVersion() final TransformVersion version = getTransformVersion(); if (version != null) try { marshal(object, OutputFactory.createXMLEventWriter(output), version); } catch (XMLStreamException e) { throw new JAXBException(e); } else { // Marshalling to the default GML version. final Context context = begin(); try { marshaller.marshal(object, output); } finally { context.finish(); } } }
/** * Delegates to the wrapped marshaller. The initial state will be saved * if it was not already done, for future restoration by {@link #reset(Pooled)}. */ @Override public void setAttachmentMarshaller(final AttachmentMarshaller am) { if (!isPropertySaved(AttachmentMarshaller.class)) { saveProperty(AttachmentMarshaller.class, marshaller.getAttachmentMarshaller()); } marshaller.setAttachmentMarshaller(am); }
/** * Creates a pooled marshaller wrapping the given one. * Callers shall invoke {@link #reset(Pooled)} after construction for completing the initialization. * * @param marshaller the marshaller to use for the actual work. * @param template the {@link PooledTemplate} from which to get the initial values. * @throws JAXBException if an error occurred while setting a property. */ PooledMarshaller(final Marshaller marshaller, final Pooled template) throws JAXBException { super(template); this.marshaller = marshaller; initialize(template); }
/** * Converts the given arbitrary object to an implementation having JAXB annotations. * If the given object is not recognized or is already an instance of the expected class, * then it is returned unchanged. */ private Object toImplementation(final Object value) throws JAXBException { final TypeRegistration[] converters = getRootAdapters(); if (converters != null) { for (final TypeRegistration t : converters) { final Object c = t.toImplementation(value); if (c != null) return c; } } return value; }