PageRenderTime 63ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/codegen/src/main/java/com/paypal/selion/reader/AbstractYamlReader.java

https://gitlab.com/CORP-RESELLER/SeLion
Java | 152 lines | 96 code | 31 blank | 25 comment | 10 complexity | a709a4a531edc53effc0866766c9da2a MD5 | raw file
  1. /*-------------------------------------------------------------------------------------------------------------------*\
  2. | Copyright (C) 2014 PayPal |
  3. | |
  4. | Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance |
  5. | with the License. |
  6. | |
  7. | You may obtain a copy of the License at |
  8. | |
  9. | http://www.apache.org/licenses/LICENSE-2.0 |
  10. | |
  11. | Unless required by applicable law or agreed to in writing, software distributed under the License is distributed |
  12. | on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for |
  13. | the specific language governing permissions and limitations under the License. |
  14. \*-------------------------------------------------------------------------------------------------------------------*/
  15. package com.paypal.selion.reader;
  16. import java.io.IOException;
  17. import java.util.ArrayList;
  18. import java.util.List;
  19. import java.util.Map;
  20. import java.util.Map.Entry;
  21. import org.yaml.snakeyaml.Yaml;
  22. import com.paypal.selion.elements.HtmlSeLionElementList;
  23. import com.paypal.selion.platform.web.HtmlContainerElement;
  24. import com.paypal.selion.plugins.TestPlatform;
  25. /**
  26. * This is an abstract representation of a typical yaml reader.
  27. */
  28. //TODO Merge this with "clients" version of a class by the same name.. Move merged result to "common"
  29. public abstract class AbstractYamlReader {
  30. private Yaml yaml;
  31. private boolean processed;
  32. private String baseClassName;
  33. private TestPlatform platform;
  34. public static final String KEY = "Key";
  35. public static final String DELIMITER = "#";
  36. private final List<String> allkeys = new ArrayList<>();
  37. final Yaml getYaml() {
  38. if (this.yaml == null) {
  39. this.yaml = new Yaml();
  40. }
  41. return this.yaml;
  42. }
  43. final void setBaseClassName(String name) {
  44. baseClassName = name;
  45. }
  46. public String getBaseClassName() {
  47. return baseClassName;
  48. }
  49. final void setPlatform(TestPlatform platform){
  50. this.platform = platform;
  51. }
  52. public TestPlatform getPlatform(){
  53. return this.platform;
  54. }
  55. final void appendKey(String key) {
  56. allkeys.add(key);
  57. }
  58. public final List<String> getAllKeys() {
  59. List<String> objects = new ArrayList<>();
  60. objects.addAll(this.allkeys);
  61. return objects;
  62. }
  63. boolean isProcessed() {
  64. return processed;
  65. }
  66. final void setProcessed(boolean flag) {
  67. processed = flag;
  68. }
  69. protected List<String> parseKeysForContainer(String fileName, List<Object> allElements) {
  70. List<String> elementKeys = new ArrayList<String>();
  71. for (Object element : allElements) {
  72. @SuppressWarnings("unchecked")
  73. Map<String, Object> elementMap = (Map<String, Object>) element;
  74. try {
  75. String elementKey = ((String) elementMap.get(KEY)).trim();
  76. if ("".equals(elementKey)) {
  77. continue;
  78. }
  79. if (!(HtmlSeLionElementList.isValid(elementKey))) {
  80. throw new IllegalArgumentException(String.format("Detected an invalid key [%s] in data file %s",
  81. elementKey, fileName));
  82. }
  83. if (HtmlSeLionElementList.CONTAINER.looksLike(elementKey)) {
  84. throw new IllegalArgumentException("Cannot define a Container within a Container.");
  85. }
  86. elementKeys.add(elementKey);
  87. } catch (NullPointerException e) {// NOSONAR
  88. // Gobbling the exception but doing nothing with it.
  89. }
  90. }
  91. return elementKeys;
  92. }
  93. protected List<String> parseKeysForContainer(String fileName, Map<String, HtmlContainerElement> allElements) {
  94. List<String> elementKeys = new ArrayList<String>();
  95. for (Entry<String, HtmlContainerElement> element : allElements.entrySet()) {
  96. try {
  97. String elementKey = element.getKey().trim();
  98. if (("").equals(elementKey)) {
  99. continue;
  100. }
  101. if (!(HtmlSeLionElementList.isValidHtmlElement(elementKey))) {
  102. throw new IllegalArgumentException(String.format("Detected an invalid key [%s] in data file %s",
  103. elementKey, fileName));
  104. }
  105. if (HtmlSeLionElementList.CONTAINER.looksLike(elementKey)) {
  106. throw new IllegalArgumentException("Cannot define a Container within a Container.");
  107. }
  108. elementKeys.add(elementKey);
  109. } catch (NullPointerException e) {// NOSONAR
  110. // Gobbling the exception but doing nothing with it.
  111. }
  112. }
  113. return elementKeys;
  114. }
  115. /**
  116. * This method processes the contents of a data source (yaml file for e.g.,).
  117. * @param resource - A {@link FileSystemResource} that represents a particular data source.
  118. * @throws IOException
  119. */
  120. public abstract void processPage(FileSystemResource resource) throws IOException;
  121. }