1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package net.sourceforge.jarduino.message;
18
19 import java.util.HashMap;
20 import java.util.Map;
21
22 import net.sourceforge.jarduino.ArduinoException;
23 import net.sourceforge.jarduino.message.ArduinoParser.ArduinoParserException;
24
25
26
27
28 public class ArduinoComments {
29
30
31
32 static final String MARKER = "CM_";
33
34
35
36
37 private Map<ArduinoNamedObject, String> theComments;
38
39
40
41
42 ArduinoComments() {
43 theComments = new HashMap<>();
44 }
45
46
47
48
49
50
51 public String getCommentForObject(final ArduinoNamedObject pObject) {
52 return theComments.get(pObject);
53 }
54
55
56
57
58
59
60 void storeCommentForObject(final ArduinoNamedObject pObject,
61 final String pComment) {
62 theComments.put(pObject, pComment);
63 }
64
65
66
67
68
69
70
71 public static void parseComment(final ArduinoSystem pSystem,
72 final String pCommentDef) throws ArduinoException {
73
74 final String myMarker = ArduinoParser.nextToken(pCommentDef);
75 String myDef = ArduinoParser.stripToken(pCommentDef, myMarker);
76 if (!myMarker.equals(MARKER)) {
77 throw new ArduinoException("Invalid marker", pCommentDef);
78 }
79
80
81 final int myIndex = myDef.indexOf(ArduinoChar.QUOTE);
82 if (myIndex == -1) {
83 throw new ArduinoException("Missing " + ArduinoChar.QUOTE + " separator", pCommentDef);
84 }
85 String myId = myDef.substring(0, myIndex);
86 myDef = myDef.substring(myIndex);
87
88
89 final int myLen = myDef.length();
90 if (myDef.length() == 0 || myDef.charAt(myLen - 1) != ArduinoChar.SEMICOLON) {
91 throw new ArduinoException("Missing terminator", pCommentDef);
92 }
93 myDef = myDef.substring(0, myLen - 1);
94
95
96 try {
97
98 final String myComment = ArduinoParser.nextQuotedToken(myDef);
99
100
101 final String myType = ArduinoParser.nextToken(myId);
102 myId = ArduinoParser.stripToken(myId, myType);
103
104
105 final ArduinoNamedObject myTarget = determineTarget(pSystem, myType, myId);
106
107
108 pSystem.storeCommentForObject(myTarget, myComment);
109
110
111 } catch (ArduinoParserException e) {
112 throw new ArduinoException(e.getMessage() + ArduinoChar.COLON + e.getDetail(), pCommentDef);
113 }
114 }
115
116
117
118
119
120
121
122
123
124 private static ArduinoNamedObject determineTarget(final ArduinoSystem pSystem,
125 final String pType,
126 final String pId) throws ArduinoParserException {
127
128 if (pType.length() == 0) {
129 return pSystem;
130 }
131
132
133 if (pType.equals(ArduinoNode.MARKER)) {
134 return pSystem.findNodeByName(pId);
135 }
136
137
138 final String myId = ArduinoParser.nextToken(pId);
139 final String mySignal = ArduinoParser.stripToken(pId, myId);
140 final ArduinoMessage myMessage = pSystem.findMessageById(myId);
141
142
143 if (pType.equals(ArduinoMessage.MARKER)) {
144 return myMessage;
145 }
146
147
148 if (pType.equals(ArduinoSignal.MARKER)) {
149 return myMessage.findSignalByName(mySignal);
150 }
151
152
153 throw new ArduinoParserException("Invalid comment type", pType);
154 }
155 }