|
1 /* |
|
2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. |
|
8 * |
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
|
13 * accompanied this code). |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License version |
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 * |
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 * or visit www.oracle.com if you need additional information or have any |
|
21 * questions. |
|
22 */ |
|
23 |
|
24 package propertiesparser.parser; |
|
25 |
|
26 import propertiesparser.parser.MessageType.CompoundType; |
|
27 import propertiesparser.parser.MessageType.OrType; |
|
28 import propertiesparser.parser.MessageType.SimpleType; |
|
29 import propertiesparser.parser.MessageType.UnionType; |
|
30 |
|
31 import java.util.ArrayList; |
|
32 import java.util.List; |
|
33 |
|
34 /** |
|
35 * An object to represent the comment that may precede the property |
|
36 * specification in a Message. |
|
37 * The comment is modelled as a list of fields, where the fields correspond |
|
38 * to the placeholder values (e.g. {0}, {1}, etc) within the message value. |
|
39 */ |
|
40 public final class MessageInfo { |
|
41 |
|
42 /** The fields of the Info object. */ |
|
43 List<MessageType> types = new ArrayList<>(); |
|
44 |
|
45 MessageInfo(String text) throws IllegalArgumentException { |
|
46 if (text != null) { |
|
47 if (!text.startsWith("# ")) |
|
48 throw new IllegalArgumentException(); |
|
49 String[] segs = text.substring(2).split(", "); |
|
50 types = new ArrayList<>(); |
|
51 for (String seg : segs) { |
|
52 types.add(parseType(seg)); |
|
53 } |
|
54 } |
|
55 } |
|
56 |
|
57 public List<MessageType> getTypes() { |
|
58 return types; |
|
59 } |
|
60 |
|
61 boolean isEmpty() { |
|
62 return types.isEmpty(); |
|
63 } |
|
64 |
|
65 @Override |
|
66 public String toString() { |
|
67 return types.toString(); |
|
68 } |
|
69 |
|
70 /** |
|
71 * Split the type comment into multiple alternatives (separated by 'or') - then parse each of them |
|
72 * individually and form an 'or' type. |
|
73 */ |
|
74 MessageType parseType(String text) { |
|
75 int commentStart = text.indexOf("("); |
|
76 if (commentStart != -1) { |
|
77 //remove optional comment |
|
78 text = text.substring(0, commentStart); |
|
79 } |
|
80 text = text.substring(text.indexOf(": ") + 2); |
|
81 String[] alternatives = text.split(" " + OrType.OR_NAME + " "); |
|
82 MessageType[] types = new MessageType[alternatives.length]; |
|
83 for (int i = 0 ; i < alternatives.length ; i++) { |
|
84 types[i] = parseAlternative(alternatives[i].trim()); |
|
85 } |
|
86 return types.length > 1 ? |
|
87 new OrType(types) : types[0]; |
|
88 } |
|
89 |
|
90 /** |
|
91 * Parse a subset of the type comment; valid matches are simple types, compound types, |
|
92 * union types and custom types. |
|
93 */ |
|
94 MessageType parseAlternative(String text) { |
|
95 //try with custom types |
|
96 if (text.charAt(0) == '\'') { |
|
97 int end = text.indexOf('\'', 1); |
|
98 return new MessageType.CustomType(text.substring(1, end)); |
|
99 } |
|
100 //try with simple types |
|
101 for (SimpleType st : SimpleType.values()) { |
|
102 if (text.equals(st.kindName())) { |
|
103 return st; |
|
104 } |
|
105 } |
|
106 //try with compound types |
|
107 for (CompoundType.Kind ck : CompoundType.Kind.values()) { |
|
108 if (text.startsWith(ck.kindName)) { |
|
109 MessageType elemtype = parseAlternative(text.substring(ck.kindName.length() + 1).trim()); |
|
110 return new CompoundType(ck, elemtype); |
|
111 } |
|
112 } |
|
113 //try with union types |
|
114 for (UnionType.Kind uk : UnionType.Kind.values()) { |
|
115 if (text.startsWith(uk.kindName)) { |
|
116 return new UnionType(uk); |
|
117 } |
|
118 } |
|
119 //no match - report a warning |
|
120 System.err.println("WARNING - unrecognized type: " + text); |
|
121 return SimpleType.UNKNOWN; |
|
122 } |
|
123 |
|
124 /** Dummy message info to be used when no resource key comment is available. */ |
|
125 static final MessageInfo dummyInfo = new MessageInfo(null); |
|
126 } |