001/**
002 * Copyright (c) 2011, The University of Southampton and the individual contributors.
003 * All rights reserved.
004 *
005 * Redistribution and use in source and binary forms, with or without modification,
006 * are permitted provided that the following conditions are met:
007 *
008 *   *  Redistributions of source code must retain the above copyright notice,
009 *      this list of conditions and the following disclaimer.
010 *
011 *   *  Redistributions in binary form must reproduce the above copyright notice,
012 *      this list of conditions and the following disclaimer in the documentation
013 *      and/or other materials provided with the distribution.
014 *
015 *   *  Neither the name of the University of Southampton nor the names of its
016 *      contributors may be used to endorse or promote products derived from this
017 *      software without specific prior written permission.
018 *
019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029 */
030package org.openimaj.citation.annotation.mock;
031
032import java.lang.reflect.InvocationHandler;
033import java.lang.reflect.Method;
034import java.lang.reflect.Proxy;
035import java.util.ArrayList;
036import java.util.Arrays;
037import java.util.List;
038import java.util.Map;
039import java.util.Map.Entry;
040
041import org.jbibtex.BibTeXEntry;
042import org.jbibtex.Key;
043import org.jbibtex.StringValue;
044import org.jbibtex.Value;
045import org.openimaj.citation.annotation.Reference;
046import org.openimaj.citation.annotation.ReferenceType;
047
048/**
049 * A Mocked version of a {@link Reference} 
050 * 
051 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
052 */
053public class MockReference implements InvocationHandler {
054        private String[] author;
055        private String title;
056        private ReferenceType type;
057        private String year;
058        private String journal = "";
059        private String booktitle = "";
060        private String[] pages = {};
061        private String chapter = "";
062    private String edition = "";
063    private String url = "";
064    private String note = "";
065    private String[] editor = {};
066    private String institution = "";
067    private String month = "";
068    private String number = "";
069    private String organization = "";
070    private String publisher = "";
071    private String school = "";
072    private String series = "";
073    private String volume = "";
074    private String [] customData = {};
075    
076    /**
077     * Construct from a BibTeXEntry
078     * @param entry the BibTeXEntry
079     */
080    public MockReference(BibTeXEntry entry) {
081        type = ReferenceType.getReferenceType(entry.getType().getValue());
082        
083        Map<Key, Value> fields = entry.getFields();
084        for (Entry<Key, Value> e : fields.entrySet()) {
085                String ks = e.getKey().getValue();
086                Value v = e.getValue();
087                
088                if (ks.equalsIgnoreCase("author"))
089                        author = ((StringValue) v).getString().split(" and ");
090                else if (ks.equalsIgnoreCase("title"))
091                        title = ((StringValue) v).getString();
092                else if (ks.equalsIgnoreCase("year"))
093                        year = ((StringValue) v).getString();
094                else if (ks.equalsIgnoreCase("journal"))
095                        journal = ((StringValue) v).getString();
096                else if (ks.equalsIgnoreCase("booktitle"))
097                        booktitle = ((StringValue) v).getString();
098                else if (ks.equalsIgnoreCase("pages"))
099                        pages = ((StringValue) v).getString().split(",|-|--");
100                else if (ks.equalsIgnoreCase("chapter"))
101                        chapter = ((StringValue) v).getString();
102                else if (ks.equalsIgnoreCase("edition"))
103                        edition = ((StringValue) v).getString();
104                else if (ks.equalsIgnoreCase("url"))
105                        url = ((StringValue) v).getString();
106                else if (ks.equalsIgnoreCase("note"))
107                        note = ((StringValue) v).getString();
108                else if (ks.equalsIgnoreCase("editor"))
109                        editor = ((StringValue) v).getString().split(" and ");
110                else if (ks.equalsIgnoreCase("institution"))
111                        institution = ((StringValue) v).getString();
112                else if (ks.equalsIgnoreCase("month"))
113                        month = ((StringValue) v).getString();
114                else if (ks.equalsIgnoreCase("number"))
115                        number = ((StringValue) v).getString();
116                else if (ks.equalsIgnoreCase("organization"))
117                        organization = ((StringValue) v).getString();
118                else if (ks.equalsIgnoreCase("publisher"))
119                        publisher = ((StringValue) v).getString();
120                else if (ks.equalsIgnoreCase("school"))
121                        school = ((StringValue) v).getString();
122                else if (ks.equalsIgnoreCase("series"))
123                        series = ((StringValue) v).getString();
124                else if (ks.equalsIgnoreCase("volume"))
125                        volume = ((StringValue) v).getString();
126                else {
127                        List<String> data = new ArrayList<String>();
128                        data.addAll(Arrays.asList(customData));
129                        
130                        if (v instanceof StringValue) {
131                                data.add(ks);
132                                data.add(((StringValue)v).getString());
133                        }
134                        
135                        customData = data.toArray(customData);
136                }
137        }       
138    }
139    
140    /**
141     * The name(s) of the author(s)
142     * 
143     * @return The name(s) of the author(s)
144     */
145    public String[] author() {
146        return author;
147    }
148
149    /**
150     * The title of the work
151     * 
152     * @return The title of the work
153     */
154    public String title() {
155        return title;
156    }
157
158    /**
159     * The type of publication
160     * 
161     * @return The type of publication
162     * @see ReferenceType
163     */
164    public ReferenceType type() {
165        return type;
166    }
167
168    /**
169     * The year of publication (or, if unpublished, the year of creation)
170     * 
171     * @return The year of publication (or, if unpublished, the year of creation)
172     */
173    public String year() {
174        return year;
175    }
176
177    /**
178     * The journal or magazine the work was published in
179     * 
180     * @return The journal or magazine the work was published in
181     */
182    public String journal() {
183        return journal;
184    }
185    
186    /**
187     * The title of the book, if only part of it is being cited
188     * 
189     * @return The title of the book, if only part of it is being cited
190     */
191    public String booktitle() {
192        return booktitle;
193    }
194
195    /**
196     * Page numbers 
197     * 
198     * @return Page numbers
199     */
200    public String[] pages() {
201        return pages;
202    }
203
204    /**
205     * The chapter number
206     * 
207     * @return The chapter number
208     */
209    public String chapter() {
210        return chapter;
211    }
212    
213    /**
214     * The edition of a book, long form (such as "first" or "second")
215     * 
216     * @return  The edition of a book, long form (such as "first" or "second")
217     */
218    public String edition() {
219        return edition;
220    }
221    
222    /**
223     * An optional URL reference where the publication can be found.
224     * 
225     * @return A URL where the reference can be found. 
226     */
227    public String url() {
228        return url;
229    }
230
231    /**
232     * Miscellaneous extra information
233     * 
234     * @return Miscellaneous extra information
235     */
236    public String note() {
237        return note;
238    }
239    
240    /**
241     * The name(s) of the editor(s)
242     * 
243     * @return The name(s) of the editor(s)
244     */
245    public String[] editor() {
246        return editor;
247    }
248    
249    /**
250     * The institution that was involved in the publishing, but not necessarily the publisher
251     * 
252     * @return The institution that was involved in the publishing, but not necessarily the publisher
253     */
254    public String institution() {
255        return institution;
256    }
257    
258    /**
259     * The month of publication (or, if unpublished, the month of creation)
260     * 
261     * @return The month of publication (or, if unpublished, the month of creation)
262     */
263    public String month() {
264        return month;
265    }
266    
267    /**
268     * The "(issue) number" of a journal, magazine, or tech-report, if applicable. (Most publications have a "volume", but no "number" field.)
269     * 
270     * @return The "(issue) number" of a journal, magazine, or tech-report, if applicable. (Most publications have a "volume", but no "number" field.)
271     */
272    public String number() {
273        return number;
274    }
275    
276    /**
277     * The conference sponsor
278     * 
279     * @return The conference sponsor
280     */
281    public String organization() {
282        return organization;
283    }
284    
285    /**
286     * The publisher's name
287     * 
288     * @return The publisher's name
289     */
290    public String publisher() {
291        return publisher;
292    }
293    
294    /**
295     * The school where the thesis was written
296     * 
297     * @return The school where the thesis was written
298     */
299    public String school() {
300        return school;
301    }
302    
303    /**
304     * The series of books the book was published in (e.g. "The Hardy Boys" or "Lecture Notes in Computer Science")
305     * 
306     * @return The series of books the book was published in (e.g. "The Hardy Boys" or "Lecture Notes in Computer Science")
307     */
308    public String series() {
309        return series;
310    }
311    
312    /**
313     * The volume of a journal or multi-volume book
314     * 
315     * @return The volume of a journal or multi-volume book
316     */
317    public String volume() {
318        return volume;
319    }
320    
321    /**
322     * A list of custom key value data pairs.
323     * 
324     * @return A list of custom key value data pairs.
325     */
326    public String [] customData() {
327        return customData;
328    }
329
330        @Override
331        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
332                Method newmethod = getClass().getMethod(method.getName(), method.getParameterTypes());
333                
334                return newmethod.invoke(this, args);
335        }
336        
337        /**
338         * @return the {@link MockReference} as a {@link Reference} instance
339         */
340        public Reference asReference() {
341                return (Reference) Proxy.newProxyInstance(MockReference.class.getClassLoader(), new Class<?>[]{Reference.class}, this);
342        }
343        
344        /**
345         * Make a {@link Reference} from a {@link BibTeXEntry}.
346         * @param entry the {@link BibTeXEntry}.
347         * @return the {@link Reference}
348         */
349        public static Reference makeReference(BibTeXEntry entry) {
350                return new MockReference(entry).asReference();
351        }
352}