001    /*
002     * mesopotamia @mesopotamia.version@
003     * Multilingual parser and repository. 
004     * Copyright (C) 2005  Hammurapi Group
005     *
006     * This program is free software; you can redistribute it and/or
007     * modify it under the terms of the GNU Lesser General Public
008     * License as published by the Free Software Foundation; either
009     * version 2 of the License, or (at your option) any later version.
010     *
011     * This program is distributed in the hope that it will be useful,
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014     * Lesser General Public License for more details.
015     *
016     * You should have received a copy of the GNU Lesser General Public
017     * License along with this library; if not, write to the Free Software
018     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
019     *
020     * URL: http://http://www.hammurapi.biz
021     * e-Mail: support@hammurapi.biz
022     */
023    package org.mesopotamia;
024    
025    import java.awt.event.WindowEvent;
026    import java.io.IOException;
027    import java.io.Writer;
028    import java.lang.ref.Reference;
029    import java.lang.ref.SoftReference;
030    import java.sql.SQLException;
031    import java.util.ArrayList;
032    import java.util.Collection;
033    import java.util.Collections;
034    import java.util.HashMap;
035    import java.util.Iterator;
036    import java.util.List;
037    import java.util.Map;
038    
039    import javax.swing.WindowConstants;
040    import javax.swing.tree.TreeNode;
041    
042    import org.mesopotamia.sql.LoadError;
043    import org.mesopotamia.sql.MesopotamiaEngine;
044    import org.w3c.dom.Element;
045    
046    import biz.hammurapi.convert.ConverterClosure;
047    import biz.hammurapi.convert.ConvertingService;
048    import biz.hammurapi.legacy.review.Signed;
049    import biz.hammurapi.swing.Browser;
050    import biz.hammurapi.swing.Visualizable;
051    import biz.hammurapi.util.Attributable;
052    import biz.hammurapi.util.CollectionVisitable;
053    import biz.hammurapi.util.ConvertingCollection;
054    import biz.hammurapi.util.VisitableBase;
055    import biz.hammurapi.util.Visitor;
056    import biz.hammurapi.xml.dom.DomSerializable;
057    
058    
059    /**
060     * Represents source unit, e.g. java source file
061     * @author Pavel Vlasov
062     * @revision $Revision: 1.1 $
063     */
064    public class AstSourceUnit extends SourceUnit {
065            
066            private Collection<Object> languageElements;
067            
068            /**
069             * Instances of SourceUnit shall be created only by
070             * repository
071             * @throws SQLException 
072             */
073            protected AstSourceUnit(
074                            org.mesopotamia.sql.SourceUnit dbData, 
075                            final Scan scan, 
076                            RepositoryLanguage repoLanguage, 
077                            Collection<Number> loadLevels) throws MesopotamiaException {
078                    super(dbData, scan, repoLanguage, loadLevels);
079                    
080                    SyntaxTree st = getSyntaxTree();
081                    if (st==null) {
082                            languageElements = EMPTY_UNMODIFIABLE_LIST;
083                    } else {
084                            ConvertingCollection rootLanguageElements = new ConvertingCollection(
085                                                                                    st.getRoots(),
086                                                                                    new ConverterClosure() {
087                            
088                                                                                            public Object convert(Object source) {
089                                                                                                    NodeData mn = (NodeData) source;
090                                                                                                    LanguageElementHandle handle = new LanguageElementHandle(mn.getSourceUnitId(), mn.getId(), null, null);
091                                                                                                    return scan.getLanguageElement(handle);
092                                                                                            }
093                                                                                            
094                                                                                    },
095                                                                                    null);
096                            
097                            languageElements = Collections.unmodifiableCollection(rootLanguageElements);
098                    }
099            }
100            
101            /**
102             * Helper method for ANTLR-based languages. Returns data for level "ast" cast to SyntaxTree.
103             * @return
104             */
105            public SyntaxTree getSyntaxTree() {
106                    return (SyntaxTree) getLevelData("ast");
107            }
108    
109            protected void acceptChildren(Visitor visitor) {
110                    new CollectionVisitable(languageElements, false).accept(visitor);               
111            }
112            
113            /**
114             * @return Root language elements
115             */
116            public Collection<Object> getLanguageElements() {
117                    return languageElements;
118            }
119    
120            public void toDom(Element holder) {
121                    super.toDom(holder);
122            
123                    Element le=holder.getOwnerDocument().createElement("language-elements");
124                    holder.appendChild(le);
125                    DomSerializable ds = (DomSerializable) ConvertingService.convert(languageElements, DomSerializable.class);
126                    ds.toDom(le);
127                    
128            }
129            
130            public Token getToken(int position) {
131                    return (Token) ((ArrayList<Token>) getLevelData("token")).get(position);
132            }
133                    
134    }