View Javadoc

1   /*******************************************************************************
2    * Copyright (c) 2009 Ordina and committers to Mod4j
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the Eclipse Public License v1.0
5    * which accompanies this distribution, and is available at
6    * http://www.eclipse.org/legal/epl-v10.html
7    *
8    * Contributors:
9    *     Ordina - initial implementation
10   *******************************************************************************/
11  package org.mod4j.crossx.broker;
12  
13  import java.io.PrintWriter;
14  import java.util.ArrayList;
15  import java.util.Collections;
16  import java.util.List;
17  
18  import org.mod4j.crossx.mm.crossx.Symbol;
19  import org.mod4j.crossx.mm.crossx.ModelInfo;
20  
21  // import crossx.util.EclipseUtil;
22  
23  /**
24   * This class is a singleton with static members only.
25   * It keeps all cross reference information for DSL models for one location
26   * The location is identified with the 'name'
27   * @author Jos Warmer
28   * 
29   */
30  public class CrossxLocation {
31  
32      /**
33       * The list of all ModelInfo objects in this location.
34       */
35      private List<ModelInfo> information = new ArrayList<ModelInfo>();
36  
37      private String name;
38  
39      /**
40       *  Create a new CrossxLocation for location named 'theName'
41       * @param theName
42       */
43      public CrossxLocation(String theName) {
44          this.name = theName;
45      }
46  
47      /**
48       * Get the name of this location
49       * @return
50       */
51      public String getName() {
52          return name;
53      }
54  
55      public List<ModelInfo> getAll() {
56          return Collections.unmodifiableList(information);
57      }
58  
59      /**
60       * Add modelinfo to this location, remove previous modelinfo is this exists from the same resource
61       * 
62       * @param modelinfo
63       */
64      public void addModelInfo(ModelInfo modelinfo) {
65          ModelInfo existing = findModelInfo(modelinfo, information);
66          if (existing != null) {
67              information.remove(existing);
68          }
69          information.add(modelinfo);
70      }
71  
72      public void removeModelInfo(String resource) {
73          ModelInfo existing = findModelInfo(resource, information);
74          if (existing != null) {
75              information.remove(existing);
76          }
77      }
78  
79      /**
80       * Find the existing ModelInfo that has the same resource as modelinfo
81       * 
82       * @param modelinfo
83       * @param modelInfoList
84       * @return
85       */
86      private ModelInfo findModelInfo(ModelInfo modelinfo, List<ModelInfo> modelInfoList) {
87          for (ModelInfo existing : modelInfoList) {
88              if (modelinfo.getResource().equals(existing.getResource())) {
89                  return existing;
90              }
91          }
92          return null;
93      }
94      private ModelInfo findModelInfo(String resource, List<ModelInfo> modelInfoList) {
95          for (ModelInfo existing : modelInfoList) {
96              if (resource.equals(existing.getResource())) {
97                  return existing;
98              }
99          }
100         return null;
101     }
102 
103     /**
104      * Find the symbol with name 'name' and type 'type'.
105      * 
106      * @param name
107      * @param symbolType
108      * @return The name of the resource if the element is found, null if it isn't found
109      */
110     public Symbol lookup(String model, String name, String symbolType) {
111         if (information == null) {
112             System.err.println("CrossxLocation::lookup information = null");
113         }
114         for (ModelInfo modelinfo : information) {
115             if (modelinfo.getModelname().equals(model)) {
116                 for (Symbol symbol : modelinfo.getSymbols()) {
117                     if (symbol.getName().equals(name) && symbol.getType().equals(symbolType)) {
118                         return symbol;
119                     }
120                 }
121             }
122         }
123         return null;
124     }
125 
126     /**
127      * Find all names of symbols of type 'elemType'.
128      * 
129      * @param symbolType
130      * @return The list of names (String) of all found symbols. If there is no such symbol, an empty list.
131      */
132     public List<Symbol> findAll(String symbolType) {
133         List<Symbol> result = new ArrayList<Symbol>();
134         for (ModelInfo modelinfo : information) {
135             for (Symbol symbol : modelinfo.getSymbols()) {
136                 String value = symbol.getType();
137                 if ((value != null) && value.equals(symbolType)) {
138                     result.add(symbol);
139                 }
140             }
141         }
142         return result;
143     }
144 
145     /**
146      * Find all symbols of type 'symbolType'.
147      * 
148      * @param elemType
149      * @return The list of names (String) of all found symbols. If there is no such symbol, an empty list.
150      */
151     public List<Symbol> findAllSymbols(String symbolType) {
152         List<Symbol> result = new ArrayList<Symbol>();
153         for (ModelInfo modelinfo : information) {
154             for (Symbol symbol : modelinfo.getSymbols()) {
155                 String value = symbol.getType();
156                 if ((value != null) && value.equals(symbolType)) {
157                     result.add(symbol);
158                 }
159             }
160         }
161         return result;
162     }
163 
164     /**
165      * Find all symbols of type 'elemType'.
166      * 
167      * @param elemType
168      * @return The list of names (String) of all found symbols. If there is no such symbol, an empty list.
169      */
170     public List<Symbol> findAllFromModel(String modelname, String elemType) {
171         List<Symbol> result = new ArrayList<Symbol>();
172         for (ModelInfo modelinfo : information) {
173             if (modelinfo.getModelname().equals(modelname)) {
174                 for (Symbol symbol : modelinfo.getSymbols()) {
175                     String value = symbol.getType();
176                     if ((value != null) && value.equals(elemType)) {
177                         result.add(symbol);
178                     }
179                 }
180             }
181         }
182         return result;
183     }
184 
185     public List<Symbol> findAllSymbolsFromModel(String modelname, String elemType) {
186         List<Symbol> result = new ArrayList<Symbol>();
187         for (ModelInfo modelinfo : information) {
188             if (modelinfo.getModelname().equals(modelname)) {
189                 for (Symbol symbol : modelinfo.getSymbols()) {
190                     String value = symbol.getType();
191                     if ((value != null) && value.equals(elemType)) {
192                         result.add(symbol);
193                     }
194                 }
195             }
196         }
197         return result;
198     }
199 
200     private PrintWriter pr = null;
201 
202     /**
203      * Sets 'pw' as the output writer for this object.
204      * Should use proper logging for this ...
205      * @param pw
206      */
207     public void setPrintWriter(PrintWriter pw) {
208         pr = pw;
209     }
210 
211     /**
212      * Print to the error output or the given printwriter
213      * 
214      * @param text
215      */
216     private void print(String text) {
217         if (pr == null) {
218             System.err.println(text);
219         } else {
220             pr.println(text);
221             pr.flush();
222         }
223     }
224 }