/modules/library/main/src/main/java/org/geotools/feature/type/DescriptorValidator.java

https://github.com/geotools/geotools · Java · 303 lines · 5 code · 3 blank · 295 comment · 0 complexity · 4f15de36a971c8da8e5e329709ebd9c7 MD5 · raw file

  1. /*
  2. * GeoTools - The Open Source Java GIS Toolkit
  3. * http://geotools.org
  4. *
  5. * (C) 2002-2008, Open Source Geospatial Foundation (OSGeo)
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation;
  10. * version 2.1 of the License.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. */
  17. package org.geotools.feature.type;
  18. class DescriptorValidator {
  19. private DescriptorValidator() {
  20. // no-op
  21. }
  22. // public static void validate(AttributeDescriptor schema, List<Attribute> content) {
  23. // if (schema == null) {
  24. // throw new NullPointerException("schema");
  25. // }
  26. // if (content == null) {
  27. // throw new NullPointerException("content");
  28. // }
  29. //
  30. // List<AttributeType> allowedTypes =
  31. // Descriptors.types(schema.getType());
  32. //
  33. // int index = 0;
  34. // for (Iterator itr = content.iterator(); itr.hasNext();) {
  35. // Attribute att = (Attribute) itr.next();
  36. // // att shall not be null
  37. // checkAttIsNotNull(index, att);
  38. // // and has to be of one of the allowed types
  39. // checkAttIsOfAllowedType(allowedTypes, index, att);
  40. // index++;
  41. // }
  42. //
  43. // if (schema.getType() instanceof SequenceType) {
  44. // validateSequence(schema, content);
  45. // }
  46. // else if (schema.getType() instanceof ChoiceType) {
  47. // validateChoice(schema,content);
  48. // }
  49. // else if (schema.getType() instanceof ComplexType) {
  50. // validateAll(schema, content);
  51. // }
  52. // else {
  53. // validateNode(schema, content);
  54. // }
  55. //
  56. // }
  57. //
  58. // /**
  59. // * Validates the sequence of Attributes in <code>content</code> until the
  60. // * <code>schema.sequence()</code> gets exhausted, and returns the list of
  61. // * remaining attributes.
  62. // *
  63. // */
  64. // private static void validateSequence(AttributeDescriptor schema,
  65. // List<Attribute> content) {
  66. // // empty sequences are allowed, in such a case, content should be empty
  67. // if (((List)((SequenceType)schema.getType()).attributes()).isEmpty()) {
  68. // if (content.size() > 0) {
  69. // throw new IllegalArgumentException(
  70. // "Sequence is empty, content not allowed");
  71. // }
  72. // return;
  73. // }
  74. //
  75. // final List<AttributeDescriptor> descriptors =
  76. // (List) ((SequenceType)schema.getType()).attributes();
  77. // final int min = schema.getMinOccurs();
  78. // final int max = schema.getMaxOccurs();
  79. //
  80. // List<Attribute> remaining = processSequence(descriptors, content, min,
  81. // max);
  82. //
  83. // if (remaining.size() > 0) {
  84. // throw new IllegalArgumentException(
  85. // "Extra content found beyond the specified in the schema: "
  86. // + remaining);
  87. // }
  88. // }
  89. //
  90. // private static void validateChoice(AttributeDescriptor choice,
  91. // List<Attribute> content) {
  92. //
  93. // ChoiceType type = (ChoiceType) choice.getType();
  94. // // empty sequences are allowed, in such a case, content should be empty
  95. // if (type.attributes().isEmpty()) {
  96. // if (content.size() > 0) {
  97. // throw new IllegalArgumentException(
  98. // "Choice is empty, content not allowed");
  99. // }
  100. // return;
  101. // }
  102. //
  103. // final Set<Descriptor> descriptors = (Set) type.attributes();
  104. // final int min = choice.getMinOccurs();
  105. // final int max = choice.getMaxOccurs();
  106. //
  107. // List<Attribute> remaining = processChoice(descriptors, content, min,
  108. // max);
  109. //
  110. // if (remaining.size() > 0) {
  111. // throw new IllegalArgumentException(
  112. // "Extra content found beyond the specified in the schema: "
  113. // + remaining);
  114. // }
  115. // }
  116. //
  117. // private static void validateAll(AttributeDescriptor all, List<Attribute> content)
  118. // throws NullPointerException, IllegalArgumentException {
  119. // if (content == null) {
  120. // throw new NullPointerException("content");
  121. // }
  122. //
  123. // ComplexType ctype = (ComplexType)all.getType();
  124. // List<AttributeType> usedTypes = new ArrayList<AttributeType>();
  125. // for (Iterator itr = content.iterator(); itr.hasNext();) {
  126. // Attribute att = (Attribute) itr.next();
  127. // AttributeType<?> type = att.getType();
  128. //
  129. // // cannot be more than one instance of its type
  130. // // (shortcut to multiplicity rangecheck)
  131. // if (usedTypes.contains(type)) {
  132. // throw new IllegalArgumentException("Attribute of type "
  133. // + type.getName() + " encountered more than once.");
  134. // }
  135. // usedTypes.add(type);
  136. // }
  137. // // and the multiplicity specified in each AttributeDescriptor respected
  138. // for (Iterator itr = ctype.attributes().iterator(); itr.hasNext();) {
  139. // AttributeDescriptor node = (AttributeDescriptor) itr.next();
  140. // int min = node.getMinOccurs();
  141. // int max = node.getMaxOccurs();
  142. // AttributeType<?> expectedType = node.getType();
  143. // if (max == 0 && usedTypes.contains(expectedType)) {
  144. // throw new IllegalArgumentException(
  145. // expectedType.getName()
  146. // + " was fund, thus it is not allowed since maxOccurs is set to 0");
  147. // }
  148. // if (min == 1 && !usedTypes.contains(expectedType)) {
  149. // throw new IllegalArgumentException(
  150. // expectedType.getName()
  151. // + " was not fund, thus it have to since minOccurs is set to 1");
  152. // }
  153. // }
  154. //
  155. // }
  156. //
  157. // private static List<Attribute> processDescriptor(AttributeDescriptor schema,
  158. // List<Attribute> content) {
  159. // final int min = schema.getMinOccurs();
  160. // final int max = schema.getMaxOccurs();
  161. //
  162. // if (schema.getType() instanceof ChoiceType) {
  163. // Set<AttributeDescriptor> options =
  164. // (Set) ((ChoiceType)schema.getType()).attributes();
  165. // return processChoice(options, content, min, max);
  166. // }
  167. // else if (schema.getType() instanceof SequenceType) {
  168. // List<AttributeDescriptor> sequence =
  169. // (List) ((SequenceType) schema.getType()).attributes();
  170. // return processSequence(sequence, content, min, max);
  171. // }
  172. // else {
  173. // AttributeType type = schema.getType();
  174. // return processType(content, type, min, max);
  175. // }
  176. // }
  177. //
  178. // private static List<Attribute> processSequence(
  179. // List<AttributeDescriptor> sequence, List<Attribute> content,
  180. // int min, int max) {
  181. //
  182. // int count = 0;
  183. // List<Attribute> remaining = content;
  184. //
  185. // while (true) {
  186. // for (Iterator itr = sequence.iterator(); itr.hasNext();) {
  187. // AttributeDescriptor desc = (AttributeDescriptor) itr.next();
  188. // remaining = processDescriptor(desc, remaining);
  189. // }
  190. // if (count < max) {
  191. // count++;
  192. // } else {
  193. // break;
  194. // }
  195. // if (count == max || remaining.size() == 0) {
  196. // break;
  197. // }
  198. // }
  199. //
  200. // return remaining;
  201. // }
  202. //
  203. // private static List<Attribute> processChoice(
  204. // Set<Descriptor> allowableContent, List<Attribute> content, int min,
  205. // int max) {
  206. // throw new UnsupportedOperationException("not yet implemented");
  207. // /*
  208. // //the choice itself is nullified
  209. // if (min == 0 && max == 0) {
  210. // return content;
  211. // }
  212. //
  213. // int count = 0;
  214. // Map<AttributeType, Descriptor>m;
  215. // for (Attribute att : content){
  216. // AttributeType type = att.getType();
  217. //
  218. // if (allowableContent.contains(type)) {
  219. // if (count < max) {
  220. // count++;
  221. // } else {
  222. // break;
  223. // }
  224. // } else {
  225. // break;
  226. // }
  227. // }
  228. // if (count < min) {
  229. // throw new IllegalArgumentException("Expected at least " + min
  230. // + " occurrences of " + allowableContent);
  231. // }
  232. //
  233. // if (count == 0)
  234. // return content;
  235. //
  236. // return content.subList(count, content.size());
  237. // */
  238. // }
  239. //
  240. // /**
  241. // * process a minimun of <code>min</code> and a maximun of <code>max</code>
  242. // * consecutive occurrencies of Attributes of type <code>expectedType</code>
  243. // * and return the remaining attributes. Only fails if first attribute is not
  244. // * of the expected type or minOccurs has not been satisfied. Never exceeds
  245. // * maxOccurs.
  246. // *
  247. // */
  248. // private static List<Attribute> processType(List<Attribute> content,
  249. // AttributeType expectedType, int min, int max) {
  250. // int count = 0;
  251. //
  252. // for (Iterator itr = content.iterator(); itr.hasNext();) {
  253. // Attribute att = (Attribute) itr.next();
  254. // AttributeType attType = att.getType();
  255. // if (attType.equals(expectedType)) {
  256. // count++;
  257. // if (count == max) {
  258. // break;
  259. // }
  260. // } else {
  261. // break;
  262. // }
  263. // }
  264. // if (count < min) {
  265. // throw new IllegalArgumentException("got " + count
  266. // + " occurrences of " + expectedType.getName()
  267. // + ". Expected at least " + min);
  268. // }
  269. // if (count == 0)
  270. // return content;
  271. // return content.subList(count, content.size());
  272. // }
  273. //
  274. // private static void validateNode(AttributeDescriptor schema,
  275. // List<Attribute> content) {
  276. // // no-op
  277. // }
  278. //
  279. // /**
  280. // */
  281. // private static void checkAttIsOfAllowedType(
  282. // List<AttributeType> allowedTypes, int index, Attribute att)
  283. // throws IllegalArgumentException {
  284. // AttributeType<?> type = att.getType();
  285. // if (!allowedTypes.contains(type)) {
  286. // throw new IllegalArgumentException("Attribute of type "
  287. // + type.getName() + " found at index " + index
  288. // + " but this type is not allowed by this descriptor");
  289. // }
  290. // }
  291. //
  292. // private static void checkAttIsNotNull(int index, Attribute att) {
  293. // if (att == null) {
  294. // throw new NullPointerException(
  295. // "Attribute at index "
  296. // + index
  297. // + " is null. Attributes can't be null. Do you mean Attribute.get() == null?");
  298. // }
  299. // }
  300. }