1 /*******************************************************************************
\r
2 * Copyright (c) 2013 Association for Decentralized Information Management in
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * Semantum Oy - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.sysdyn.unitParser.nodes;
\r
14 import java.io.StringReader;
\r
15 import java.util.HashMap;
\r
17 import org.simantics.sysdyn.unitParser.ParseException;
\r
18 import org.simantics.sysdyn.unitParser.UnitCheckingException;
\r
19 import org.simantics.sysdyn.unitParser.UnitCheckingNode;
\r
20 import org.simantics.sysdyn.unitParser.UnitParser;
\r
21 import org.simantics.sysdyn.unitParser.nodes.UnitResult.UnitType;
\r
24 * See UnitCheckingNodeFactory for mapping
\r
25 * @author Teemu Lempinen
\r
28 public class ComponentReferenceFull extends UnitCheckingNode {
\r
30 public ComponentReferenceFull(int id) {
\r
34 protected UnitResult parseUnits(String units) {
\r
35 StringReader sr = new StringReader(units);
\r
36 UnitParser parser = new UnitParser(sr);
\r
38 UnitCheckingNode node = (UnitCheckingNode) parser.expr();
\r
39 return node.getUnits(null);
\r
40 } catch (ParseException e) {
\r
41 e.printStackTrace();
\r
42 } catch (UnitCheckingException e) {
\r
43 e.printStackTrace();
\r
49 public UnitResult getUnits(HashMap<String, String> units) throws UnitCheckingException {
\r
50 String node = printNode();
\r
52 if("dmnl".equals(node)) {
\r
53 UnitResult result = new UnitResult();
\r
54 result.setUnitType(UnitType.DMNL);
\r
59 if(!units.containsKey(node) || units.get(node) == null)
\r
60 throw new UnitCheckingException("No units defined for " + node);
\r
62 return parseUnits(units.get(node));
\r
65 UnitResult result = new UnitResult();
\r
66 result.addDivident(node);
\r
67 result.append(node);
\r
73 public String printNode() {
\r
74 StringBuilder sb = new StringBuilder();
\r
75 for(int i = 0; i < jjtGetNumChildren(); i++) {
\r
76 UnitCheckingNode node = (UnitCheckingNode) jjtGetChild(i);
\r
77 sb.append(node.printNode());
\r
79 return sb.toString();
\r