|
12 | 12 | package com.maxprograms.xml; |
13 | 13 |
|
14 | 14 | import java.io.Serializable; |
| 15 | +import java.util.List; |
| 16 | +import java.util.Stack; |
| 17 | +import java.util.StringTokenizer; |
| 18 | +import java.util.Vector; |
15 | 19 |
|
16 | 20 | public class ContentModel implements Serializable { |
17 | 21 |
|
18 | 22 | public static final String EMPTY = "EMPTY"; |
19 | 23 | public static final String ANY = "ANY"; |
20 | 24 | public static final String MIXED = "Mixed"; |
21 | 25 | public static final String PCDATA = "#PCDATA"; |
| 26 | + public static final String CHILDREN = "Children"; |
22 | 27 |
|
23 | 28 | // cardinality |
24 | 29 | public static final int NONE = 0; |
25 | 30 | public static final int OPTIONAL = 1; // ? |
26 | 31 | public static final int ZEROMANY = 2; // * |
27 | 32 | public static final int ONEMANY = 3; // + |
28 | 33 |
|
29 | | - public ContentModel() { |
| 34 | + private List<ContentParticle> content; |
| 35 | + private String type = EMPTY; |
| 36 | + |
| 37 | + private ContentModel(List<ContentParticle> content, String type) { |
| 38 | + this.content = content; |
| 39 | + this.type = type; |
| 40 | + } |
| 41 | + |
| 42 | + public static ContentModel parse(String modelString) { |
| 43 | + String string = modelString.replaceAll("\\s+", ""); |
| 44 | + validateParentheses(string); |
| 45 | + List<ContentParticle> particles = new Vector<>(); |
| 46 | + String type = CHILDREN; |
| 47 | + |
| 48 | + // Handle EMPTY and ANY |
| 49 | + if (string.equals(EMPTY)) { |
| 50 | + type = EMPTY; |
| 51 | + return new ContentModel(particles, type); |
| 52 | + } |
| 53 | + if (string.equals(ANY)) { |
| 54 | + type = ANY; |
| 55 | + return new ContentModel(particles, type); |
| 56 | + } |
| 57 | + |
| 58 | + // Handle pure PCDATA |
| 59 | + if (string.equals("(#PCDATA)")) { |
| 60 | + particles.add(new DTDPCData()); |
| 61 | + return new ContentModel(particles, MIXED); |
| 62 | + } |
| 63 | + |
| 64 | + // Handle mixed content |
| 65 | + if (string.startsWith("(#PCDATA")) { |
| 66 | + type = MIXED; |
| 67 | + if (!string.endsWith(")*")) { |
| 68 | + throw new IllegalArgumentException("Invalid mixed content model: " + string); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + // Handle element content (sequence/choice/groups) |
| 73 | + StringTokenizer st = new StringTokenizer(string, "()|,?*+", true); |
| 74 | + Stack<List<Object>> stack = new Stack<>(); |
| 75 | + List<Object> current = new Vector<>(); |
| 76 | + |
| 77 | + while (st.hasMoreTokens()) { |
| 78 | + String token = st.nextToken().trim(); |
| 79 | + validateToken(token); |
| 80 | + |
| 81 | + if (token.equals("(")) { |
| 82 | + stack.push(current); |
| 83 | + current = new Vector<>(); |
| 84 | + } else if (token.equals(")")) { |
| 85 | + ContentParticle groupParticle = processGroup(current); |
| 86 | + current = stack.pop(); |
| 87 | + current.add(groupParticle); |
| 88 | + } else if ("*".equals(token) || "+".equals(token) || "?".equals(token)) { |
| 89 | + if (current.isEmpty()) { |
| 90 | + throw new IllegalArgumentException( |
| 91 | + "Cardinality operator '" + token + "' must follow a valid particle."); |
| 92 | + } |
| 93 | + Object lastObject = current.get(current.size() - 1); |
| 94 | + if (!(lastObject instanceof ContentParticle)) { |
| 95 | + throw new IllegalArgumentException( |
| 96 | + "Cardinality operator '" + token + "' must follow a valid particle."); |
| 97 | + } |
| 98 | + int cardinality = "?".equals(token) ? OPTIONAL : ("*".equals(token) ? ZEROMANY : ONEMANY); |
| 99 | + ((ContentParticle) lastObject).setCardinality(cardinality); |
| 100 | + } else if ("|".equals(token) || ",".equals(token)) { |
| 101 | + current.add(token); |
| 102 | + } else if (PCDATA.equals(token)) { |
| 103 | + current.add(new DTDPCData()); |
| 104 | + } else { |
| 105 | + current.add(new DTDName(token)); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + for (Object obj : current) { |
| 110 | + if (!(obj instanceof ContentParticle)) { |
| 111 | + throw new IllegalArgumentException("Invalid content model: " + string); |
| 112 | + } |
| 113 | + particles.add((ContentParticle) obj); |
| 114 | + } |
| 115 | + |
| 116 | + return new ContentModel(particles, type); |
| 117 | + } |
| 118 | + |
| 119 | + private static ContentParticle processGroup(List<Object> group) { |
| 120 | + if (group.isEmpty()) { |
| 121 | + throw new IllegalArgumentException("Empty group found in content model."); |
| 122 | + } |
| 123 | + if (group.size() == 1) { |
| 124 | + Object obj = group.get(0); |
| 125 | + if (obj instanceof ContentParticle) { |
| 126 | + return (ContentParticle) obj; |
| 127 | + } else if (obj instanceof String) { |
| 128 | + return new DTDName((String) obj); |
| 129 | + } |
| 130 | + } |
| 131 | + String sep = null; |
| 132 | + for (Object obj : group) { |
| 133 | + if (obj instanceof String) { |
| 134 | + String token = (String) obj; |
| 135 | + if ("|".equals(token) || ",".equals(token)) { |
| 136 | + sep = token; |
| 137 | + break; |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + if (sep == null) { |
| 142 | + throw new IllegalArgumentException("No separator found in group: " + group); |
| 143 | + } |
| 144 | + ContentParticle result = "|".equals(sep) ? new DTDChoice() : new DTDSecuence(); |
| 145 | + for (Object obj : group) { |
| 146 | + if ("|".equals(obj) || ",".equals(obj)) { |
| 147 | + continue; |
| 148 | + } |
| 149 | + if (obj instanceof ContentParticle) { |
| 150 | + result.addParticle((ContentParticle) obj); |
| 151 | + } else if (obj instanceof String) { |
| 152 | + result.addParticle(new DTDName((String) obj)); |
| 153 | + } |
| 154 | + } |
| 155 | + return result; |
| 156 | + } |
| 157 | + |
| 158 | + private static void validateParentheses(String string) { |
| 159 | + int balance = 0; |
| 160 | + for (char c : string.toCharArray()) { |
| 161 | + if (c == '(') |
| 162 | + balance++; |
| 163 | + else if (c == ')') |
| 164 | + balance--; |
| 165 | + if (balance < 0) { |
| 166 | + throw new IllegalArgumentException("Unbalanced parentheses in content model: " + string); |
| 167 | + } |
| 168 | + } |
| 169 | + if (balance != 0) { |
| 170 | + throw new IllegalArgumentException("Unbalanced parentheses in content model: " + string); |
| 171 | + } |
30 | 172 | } |
31 | 173 |
|
32 | | - public static ContentModel parse(String string) { |
33 | | - // TODO |
34 | | - return new ContentModel(); |
| 174 | + private static void validateToken(String token) { |
| 175 | + if (!token.matches("[a-zA-Z0-9#|,?*+()]+")) { |
| 176 | + throw new IllegalArgumentException("Invalid token in content model: " + token); |
| 177 | + } |
35 | 178 | } |
36 | 179 |
|
| 180 | + @Override |
| 181 | + public String toString() { |
| 182 | + StringBuilder sb = new StringBuilder(); |
| 183 | + String separator = type.equals("|") ? "|" : ","; |
| 184 | + for (int i = 0; i < content.size(); i++) { |
| 185 | + ContentParticle particle = content.get(i); |
| 186 | + sb.append(particle.toString()); |
| 187 | + if (i < content.size() - 1) { |
| 188 | + sb.append(separator); |
| 189 | + } |
| 190 | + } |
| 191 | + return sb.toString(); |
| 192 | + } |
| 193 | + |
| 194 | + public List<ContentParticle> getContent() { |
| 195 | + return content; |
| 196 | + } |
| 197 | + |
| 198 | + public String getType() { |
| 199 | + return type; |
| 200 | + } |
37 | 201 | } |
0 commit comments