001    // This file is part of AceWiki.
002    // Copyright 2008-2012, AceWiki developers.
003    // 
004    // AceWiki is free software: you can redistribute it and/or modify it under the terms of the GNU
005    // Lesser General Public License as published by the Free Software Foundation, either version 3 of
006    // the License, or (at your option) any later version.
007    // 
008    // AceWiki is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
009    // even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
010    // Lesser General Public License for more details.
011    // 
012    // You should have received a copy of the GNU Lesser General Public License along with AceWiki. If
013    // not, see http://www.gnu.org/licenses/.
014    
015    package ch.uzh.ifi.attempto.chartparser;
016    
017    import java.util.HashMap;
018    import java.util.Map;
019    
020    /**
021     * This class represents a grammar annotation object.
022     * 
023     * @author Tobias Kuhn
024     */
025    public class Annotation {
026            
027            private Map<String, Object> items = new HashMap<String, Object>();
028            
029            /**
030             * Creates a new annotation object.
031             */
032            public Annotation() {
033            }
034            
035            /**
036             * Sets an annotation item.
037             * 
038             * @param name The name of the annotation item.
039             * @param value The value of the annotation item.
040             */
041            public void setItem(String name, Object value) {
042                    items.put(name, value);
043            }
044            
045            /**
046             * Returns the value of an annotation item.
047             * 
048             * @param name The name of the annotation item.
049             * @return The value.
050             */
051            public Object getItem(String name) {
052                    return items.get(name);
053            }
054            
055            /**
056             * Creates a deep copy of this annotation object.
057             * 
058             * @return A deep copy.
059             */
060            public Annotation deepCopy() {
061                    return deepCopy(new HashMap<Integer, StringObject>());
062            }
063            
064            /**
065             * Creates a deep copy of this annotation object using the given string objects. This method is
066             * usually called form another deepCopy-method.
067             * 
068             * @param stringObjs The string objects to be used.
069             * @return A deep copy.
070             */
071            Annotation deepCopy(HashMap<Integer, StringObject> stringObjs) {
072                    Annotation a = new Annotation();
073                    for (String n : items.keySet()) {
074                            a.setItem(n, copyStructure(items.get(n), stringObjs));
075                    }
076                    return a;
077            }
078            
079            private Object copyStructure(Object structure, HashMap<Integer, StringObject> stringObjs) {
080                    if (structure instanceof Object[]) {
081                            Object[] array = (Object[]) structure;
082                            Object[] arrayC = new Object[array.length];
083                            for (int i=0 ; i < array.length ; i++) {
084                                    Object o = array[i];
085                                    arrayC[i] = copyStructure(o, stringObjs);
086                            }
087                            return arrayC;
088                    } else if (structure instanceof String) {
089                            return structure;
090                    } else if (structure instanceof StringRef) {
091                            StringRef s = (StringRef) structure;
092                            StringObject so = stringObjs.get(s.getID());
093                            if (so == null) {
094                                    StringRef sr = new StringRef(s.getString());
095                                    stringObjs.put(s.getID(), sr.getStringObject());
096                                    return sr;
097                            } else {
098                                    return so.newStringRef();
099                            }
100                    }
101                    return null;
102            }
103    
104    }