001    package ezvcard.io;
002    
003    import ezvcard.VCard;
004    import ezvcard.VCardException;
005    import ezvcard.property.VCardProperty;
006    
007    /*
008     Copyright (c) 2013, Michael Angstadt
009     All rights reserved.
010    
011     Redistribution and use in source and binary forms, with or without
012     modification, are permitted provided that the following conditions are met: 
013    
014     1. Redistributions of source code must retain the above copyright notice, this
015     list of conditions and the following disclaimer. 
016     2. Redistributions in binary form must reproduce the above copyright notice,
017     this list of conditions and the following disclaimer in the documentation
018     and/or other materials provided with the distribution. 
019    
020     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
021     ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
022     WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
023     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
024     ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
025     (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
026     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
027     ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
028     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
029     SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
030    
031     The views and conclusions contained in the software and documentation are those
032     of the authors and should not be interpreted as representing official policies, 
033     either expressed or implied, of the FreeBSD Project.
034     */
035    
036    /**
037     * Thrown during the marshalling/unmarshalling of a property to signal to the
038     * marshaller that the property's value is a nested (2.1 style) or embedded (3.0
039     * style) vCard.
040     * @author Michael Angstadt
041     */
042    @SuppressWarnings("serial")
043    public class EmbeddedVCardException extends VCardException {
044            private final VCard vcard;
045            private final InjectionCallback callback;
046    
047            /**
048             * Thrown to unmarshal a nested or embedded vCard.
049             * @param callback injects the unmarshalled vCard into the property object
050             */
051            public EmbeddedVCardException(InjectionCallback callback) {
052                    this.callback = callback;
053                    this.vcard = null;
054            }
055    
056            /**
057             * Thrown to marshal a nested or embedded vCard.
058             * @param vcard the vCard to marshal
059             */
060            public EmbeddedVCardException(VCard vcard) {
061                    this.callback = null;
062                    this.vcard = vcard;
063            }
064    
065            /**
066             * Gets the vCard to marshal.
067             * @return the vCard to marshal
068             */
069            public VCard getVCard() {
070                    return vcard;
071            }
072    
073            /**
074             * Injects the unmarshalled vCard into the property object that threw this
075             * exception.
076             * @param vcard the vCard to inject
077             */
078            public void injectVCard(VCard vcard) {
079                    if (callback == null) {
080                            return;
081                    }
082    
083                    callback.injectVCard(vcard);
084            }
085    
086            /**
087             * Gets the property object that threw the exception.
088             * @return the property object
089             */
090            public VCardProperty getProperty() {
091                    if (callback == null) {
092                            return null;
093                    }
094    
095                    return callback.getProperty();
096            }
097    
098            /**
099             * Injects an unmarshalled vCard into the property object.
100             */
101            public static interface InjectionCallback {
102                    /**
103                     * Injects an unmarshalled vCard into the property object.
104                     * @param vcard the vCard to inject
105                     */
106                    void injectVCard(VCard vcard);
107    
108                    /**
109                     * Gets the property object that threw the
110                     * {@link EmbeddedVCardException}.
111                     * @return the property object
112                     */
113                    VCardProperty getProperty();
114            }
115    }