/hazelcast/src/main/java/com/hazelcast/config/AbstractXmlConfigHelper.java

https://bitbucket.org/gabral6_gmailcom/hazelcast · Java · 171 lines · 130 code · 26 blank · 15 comment · 22 complexity · 9adef96bfc883c3bf4443257fd21732a MD5 · raw file

  1. /*
  2. * Copyright (c) 2008-2013, Hazelcast, Inc. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.hazelcast.config;
  17. import org.w3c.dom.Node;
  18. import org.w3c.dom.NodeList;
  19. import java.util.Iterator;
  20. import java.util.NoSuchElementException;
  21. public abstract class AbstractXmlConfigHelper {
  22. public static class IterableNodeList implements Iterable<Node> {
  23. private final NodeList parent;
  24. private final int maximum;
  25. private final short nodeType;
  26. public IterableNodeList(final Node node) {
  27. this(node.getChildNodes());
  28. }
  29. public IterableNodeList(final NodeList list) {
  30. this(list, (short) 0);
  31. }
  32. public IterableNodeList(final Node node, short nodeType) {
  33. this(node.getChildNodes(), nodeType);
  34. }
  35. public IterableNodeList(final NodeList parent, short nodeType) {
  36. this.parent = parent;
  37. this.nodeType = nodeType;
  38. this.maximum = parent.getLength();
  39. }
  40. public Iterator<Node> iterator() {
  41. return new Iterator<Node>() {
  42. private int index = 0;
  43. private Node next;
  44. private boolean findNext() {
  45. next = null;
  46. for (; index < maximum; index++) {
  47. final Node item = parent.item(index);
  48. if (nodeType == 0 || item.getNodeType() == nodeType) {
  49. next = item;
  50. return true;
  51. }
  52. }
  53. return false;
  54. }
  55. public boolean hasNext() {
  56. return findNext();
  57. }
  58. public Node next() {
  59. if (findNext()) {
  60. index++;
  61. return next;
  62. }
  63. throw new NoSuchElementException();
  64. }
  65. public void remove() {
  66. throw new UnsupportedOperationException();
  67. }
  68. };
  69. }
  70. }
  71. protected String xmlToJavaName(final String name) {
  72. final StringBuilder builder = new StringBuilder();
  73. final char[] charArray = name.toCharArray();
  74. boolean dash = false;
  75. final StringBuilder token = new StringBuilder();
  76. for (int i = 0; i < charArray.length; i++) {
  77. if (charArray[i] == '-') {
  78. appendToken(builder, token);
  79. dash = true;
  80. continue;
  81. }
  82. token.append(dash ? Character.toUpperCase(charArray[i]) : charArray[i]);
  83. dash = false;
  84. }
  85. appendToken(builder, token);
  86. return builder.toString();
  87. }
  88. protected void appendToken(final StringBuilder builder, final StringBuilder token) {
  89. String string = token.toString();
  90. if ("Jvm".equals(string)) {
  91. string = "JVM";
  92. }
  93. builder.append(string);
  94. token.setLength(0);
  95. }
  96. protected String getTextContent(final Node node) {
  97. return getTextContent2(node);
  98. }
  99. protected String getTextContent2(final Node node) {
  100. final Node child = node.getFirstChild();
  101. if (child != null) {
  102. final Node next = child.getNextSibling();
  103. if (next == null) {
  104. return hasTextContent(child) ? child.getNodeValue() : "";
  105. }
  106. final StringBuffer buf = new StringBuffer();
  107. getTextContent2(node, buf);
  108. return buf.toString();
  109. }
  110. return "";
  111. }
  112. protected void getTextContent2(final Node node, final StringBuffer buf) {
  113. Node child = node.getFirstChild();
  114. while (child != null) {
  115. if (hasTextContent(child)) {
  116. getTextContent2(child, buf);
  117. }
  118. child = child.getNextSibling();
  119. }
  120. }
  121. protected String getValue(org.w3c.dom.Node node) {
  122. return node != null ? getTextContent(node).trim() : "";
  123. }
  124. protected final boolean hasTextContent(final Node child) {
  125. final short nodeType = child.getNodeType();
  126. final boolean result = nodeType != Node.COMMENT_NODE &&
  127. nodeType != Node.PROCESSING_INSTRUCTION_NODE;
  128. return result;
  129. }
  130. public final String cleanNodeName(final Node node) {
  131. return cleanNodeName(node.getNodeName());
  132. }
  133. public final static String cleanNodeName(final String nodeName) {
  134. String name = nodeName;
  135. if (name != null) {
  136. name = nodeName.replaceAll("\\w+:", "").toLowerCase();
  137. }
  138. return name;
  139. }
  140. protected boolean checkTrue(final String value) {
  141. return "true".equalsIgnoreCase(value) ||
  142. "yes".equalsIgnoreCase(value) ||
  143. "on".equalsIgnoreCase(value);
  144. }
  145. }