View Javadoc
1   package org.eclipse.aether.internal.test.util;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   * 
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   * 
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.ArrayList;
23  import java.util.Collections;
24  import java.util.LinkedHashMap;
25  import java.util.List;
26  import java.util.Map;
27  import java.util.regex.Matcher;
28  import java.util.regex.Pattern;
29  
30  /**
31   * A definition of a dependency node via a single line of text.
32   * 
33   * @see DependencyGraphParser
34   */
35  class NodeDefinition
36  {
37  
38      static final String ID = "\\(([-_a-zA-Z0-9]+)\\)";
39  
40      static final String IDREF = "\\^([-_a-zA-Z0-9]+)";
41  
42      static final String COORDS = "([^: \\(]+):([^: ]+)(?::([^: ]*)(?::([^: ]+))?)?:([^: \\[\\(<]+)";
43  
44      private static final String COORDS_NC = NodeDefinition.COORDS.replaceAll( "\\((?=\\[)", "(?:" );
45  
46      private static final String RANGE_NC = "[\\(\\[][^\\(\\)\\[\\]]+[\\)\\]]";
47  
48      static final String RANGE = "(" + RANGE_NC + ")";
49  
50      static final String SCOPE = "(?:scope\\s*=\\s*)?((?!optional)[-_a-zA-Z0-9]+)(?:<([-_a-zA-Z0-9]+))?";
51  
52      static final String OPTIONAL = "(!?optional)";
53  
54      static final String RELOCATIONS = "relocations\\s*=\\s*(" + COORDS_NC + "(?:\\s*,\\s*" + COORDS_NC + ")*)";
55  
56      static final String KEY_VAL = "(?:[-_a-zA-Z0-9]+)\\s*:\\s*(?:[-_a-zA-Z0-9]*)";
57  
58      static final String PROPS = "props\\s*=\\s*(" + KEY_VAL + "(?:\\s*,\\s*" + KEY_VAL + ")*)";
59  
60      static final String COORDSX = "(" + COORDS_NC + ")" + RANGE + "?(?:<((?:" + RANGE_NC + ")|\\S+))?";
61  
62      static final String NODE = COORDSX + "(?:\\s+" + PROPS + ")?" + "(?:\\s+" + SCOPE + ")?" + "(?:\\s+" + OPTIONAL
63          + ")?" + "(?:\\s+" + RELOCATIONS + ")?" + "(?:\\s+" + ID + ")?";
64  
65      static final String LINE = "(?:" + IDREF + ")|(?:" + NODE + ")";
66  
67      private static final Pattern PATTERN = Pattern.compile( LINE );
68  
69      private final String def;
70  
71      String coords;
72  
73      Map<String, String> properties;
74  
75      String range;
76  
77      String premanagedVersion;
78  
79      String scope;
80  
81      String premanagedScope;
82  
83      Boolean optional;
84  
85      List<String> relocations;
86  
87      String id;
88  
89      String reference;
90  
91      NodeDefinition( String definition )
92      {
93          def = definition.trim();
94  
95          Matcher m = PATTERN.matcher( def );
96          if ( !m.matches() )
97          {
98              throw new IllegalArgumentException( "bad syntax: " + def );
99          }
100 
101         reference = m.group( 1 );
102         if ( reference != null )
103         {
104             return;
105         }
106 
107         coords = m.group( 2 );
108         range = m.group( 3 );
109         premanagedVersion = m.group( 4 );
110 
111         String props = m.group( 5 );
112         if ( props != null )
113         {
114             properties = new LinkedHashMap<>();
115             for ( String prop : props.split( "\\s*,\\s*" ) )
116             {
117                 int sep = prop.indexOf( ':' );
118                 String key = prop.substring( 0, sep );
119                 String val = prop.substring( sep + 1 );
120                 properties.put( key, val );
121             }
122         }
123 
124         scope = m.group( 6 );
125         premanagedScope = m.group( 7 );
126         optional = ( m.group( 8 ) != null ) ? !m.group( 8 ).startsWith( "!" ) : Boolean.FALSE;
127 
128         String relocs = m.group( 9 );
129         if ( relocs != null )
130         {
131             relocations = new ArrayList<>();
132             Collections.addAll( relocations, relocs.split( "\\s*,\\s*" ) );
133         }
134 
135         id = m.group( 10 );
136     }
137 
138     @Override
139     public String toString()
140     {
141         return def;
142     }
143 
144 }