Skip to main content
summaryrefslogtreecommitdiffstats
blob: a07a4a3ad272a982b1784213deb567bf9263ba8c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*******************************************************************************
 * Copyright (c) 2004, 2007 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.framework.core.dsl.integration.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Stack;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.eclipse.osee.framework.core.data.IOseeBranch;
import org.eclipse.osee.framework.core.model.IBasicArtifact;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.framework.jdk.core.type.OseeStateException;
import org.eclipse.osee.framework.jdk.core.util.Conditions;
import org.eclipse.osee.framework.jdk.core.util.Strings;

/**
 * @author Roberto E. Escobar
 */
public class OseeDslSegmentParser {

   public String getStartTag(IBasicArtifact<?> artifact) throws OseeCoreException {
      return getSegmentTag("start", artifact);
   }

   public String getEndTag(IBasicArtifact<?> artifact) throws OseeCoreException {
      return getSegmentTag("end", artifact);
   }

   private static String getSegmentTag(String tagPrefix, IBasicArtifact<?> artifact) throws OseeCoreException {
      Conditions.checkNotNull(artifact, "artifact");
      IOseeBranch branch = artifact.getBranch();
      Conditions.checkNotNull(branch, "branch");
      return String.format("//@%s_artifact branch/%s/artifact/%s/ (%s:%s)", tagPrefix, branch.getGuid(),
         artifact.getGuid(), branch.getName(), artifact.getName());
   }

   public Collection<OseeDslSegment> getSegments(String source) throws OseeCoreException {
      return getSegments(getTagLocations(source));
   }

   public Collection<OseeDslSegment> getSegments(Collection<TagLocation> tagLocations) throws OseeCoreException {
      Conditions.checkNotNull(tagLocations, "tagLocations");
      Collection<OseeDslSegment> segments = new ArrayList<OseeDslSegment>();
      Stack<TagLocation> segmentStack = new Stack<TagLocation>();
      for (TagLocation segment : tagLocations) {
         if (segment.isStartTag()) {
            segmentStack.push(segment);
         } else {
            if (matches(segmentStack.peek(), segment)) {
               TagLocation startSeg = segmentStack.pop();
               processData(segments, startSeg, segment);
            }
         }
      }
      if (!segmentStack.isEmpty()) {
         throw new OseeStateException("Found unmatched segments");
      }
      return segments;
   }

   private void processData(Collection<OseeDslSegment> segments, TagLocation startSeg, TagLocation stopSeg) {
      String branchGuid = startSeg.getBranchGuid();
      String artifactGuid = startSeg.getArtifactGuid();
      int startAt = startSeg.end();
      int endAt = stopSeg.start();
      segments.add(new OseeDslSegment(branchGuid, artifactGuid, startAt, endAt));
   }

   private boolean matches(TagLocation seg1, TagLocation seg2) {
      return seg1.getBranchGuid().equals(seg2.getBranchGuid()) && seg1.getArtifactGuid().equals(seg2.getArtifactGuid());
   }

   public Collection<TagLocation> getTagLocations(String source) throws OseeCoreException {
      Conditions.checkNotNull(source, "artifact source data");
      Collection<TagLocation> segments = new ArrayList<TagLocation>();
      Pattern pattern = Pattern.compile("\\s?//@(.*?)_artifact\\s+branch/(.*?)/artifact/(.*?)/\\s+\\(.*?\\)");
      Matcher matcher = pattern.matcher(source);

      String branchGuid = null;
      String artifactGuid = null;
      String tag = null;
      int tagStart = -1;
      int tagEnd = -1;
      while (matcher.find()) {
         tagStart = matcher.start();
         tagEnd = matcher.end();

         tag = matcher.group(1);
         branchGuid = matcher.group(2);
         artifactGuid = matcher.group(3);
         if (Strings.isValid(tag) && Strings.isValid(branchGuid) && Strings.isValid(artifactGuid)) {
            boolean isStartTag = tag.equalsIgnoreCase("start");
            segments.add(new TagLocation(isStartTag, branchGuid, artifactGuid, tagStart, tagEnd));
         }
      }
      return segments;
   }

   public static final class TagLocation {

      private final boolean isStartTag;
      private final String branchGuid;
      private final String artifactGuid;
      private final int start;
      private final int end;

      public TagLocation(boolean isStartTag, String branchGuid, String artifactGuid, int start, int end) {
         super();
         this.isStartTag = isStartTag;
         this.branchGuid = branchGuid;
         this.artifactGuid = artifactGuid;
         this.start = start;
         this.end = end;
      }

      public boolean isStartTag() {
         return isStartTag;
      }

      public String getBranchGuid() {
         return branchGuid;
      }

      public String getArtifactGuid() {
         return artifactGuid;
      }

      public int start() {
         return start;
      }

      public int end() {
         return end;
      }

      @Override
      public String toString() {
         return "OseeDslSegment [isStartTag=" + isStartTag + ", branchGuid=" + branchGuid + ", artifactGuid=" + artifactGuid + ", start=" + start + ", end=" + end + "]";
      }

   }

   public static final class OseeDslSegment {

      private final String branchGuid;
      private final String artifactGuid;
      private final int start;
      private final int end;

      public OseeDslSegment(String branchGuid, String artifactGuid, int start, int end) {
         super();
         this.branchGuid = branchGuid;
         this.artifactGuid = artifactGuid;
         this.start = start;
         this.end = end;
      }

      public String getBranchGuid() {
         return branchGuid;
      }

      public String getArtifactGuid() {
         return artifactGuid;
      }

      public int start() {
         return start;
      }

      public int end() {
         return end;
      }

      @Override
      public int hashCode() {
         final int prime = 31;
         int result = 1;
         result = prime * result + (artifactGuid == null ? 0 : artifactGuid.hashCode());
         result = prime * result + (branchGuid == null ? 0 : branchGuid.hashCode());
         result = prime * result + end;
         result = prime * result + start;
         return result;
      }

      @Override
      public boolean equals(Object obj) {
         if (this == obj) {
            return true;
         }
         if (obj == null) {
            return false;
         }
         if (getClass() != obj.getClass()) {
            return false;
         }
         OseeDslSegment other = (OseeDslSegment) obj;
         if (artifactGuid == null) {
            if (other.artifactGuid != null) {
               return false;
            }
         } else if (!artifactGuid.equals(other.artifactGuid)) {
            return false;
         }
         if (branchGuid == null) {
            if (other.branchGuid != null) {
               return false;
            }
         } else if (!branchGuid.equals(other.branchGuid)) {
            return false;
         }
         if (end != other.end) {
            return false;
         }
         if (start != other.start) {
            return false;
         }
         return true;
      }

      @Override
      public String toString() {
         return "OseeDslSegment [branchGuid=" + branchGuid + ", artifactGuid=" + artifactGuid + ", start=" + start + ", end=" + end + "]";
      }

   }
}

Back to the top