PageRenderTime 22ms CodeModel.GetById 1ms app.highlight 16ms RepoModel.GetById 1ms app.codeStats 0ms

/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
 17package com.hazelcast.config;
 18
 19import org.w3c.dom.Node;
 20import org.w3c.dom.NodeList;
 21
 22import java.util.Iterator;
 23import java.util.NoSuchElementException;
 24
 25public abstract class AbstractXmlConfigHelper {
 26
 27    public static class IterableNodeList implements Iterable<Node> {
 28
 29        private final NodeList parent;
 30        private final int maximum;
 31        private final short nodeType;
 32
 33        public IterableNodeList(final Node node) {
 34            this(node.getChildNodes());
 35        }
 36
 37        public IterableNodeList(final NodeList list) {
 38            this(list, (short) 0);
 39        }
 40
 41        public IterableNodeList(final Node node, short nodeType) {
 42            this(node.getChildNodes(), nodeType);
 43        }
 44
 45        public IterableNodeList(final NodeList parent, short nodeType) {
 46            this.parent = parent;
 47            this.nodeType = nodeType;
 48            this.maximum = parent.getLength();
 49        }
 50
 51        public Iterator<Node> iterator() {
 52            return new Iterator<Node>() {
 53
 54                private int index = 0;
 55                private Node next;
 56
 57                private boolean findNext() {
 58                    next = null;
 59                    for (; index < maximum; index++) {
 60                        final Node item = parent.item(index);
 61                        if (nodeType == 0 || item.getNodeType() == nodeType) {
 62                            next = item;
 63                            return true;
 64                        }
 65                    }
 66                    return false;
 67                }
 68
 69                public boolean hasNext() {
 70                    return findNext();
 71                }
 72
 73                public Node next() {
 74                    if (findNext()) {
 75                        index++;
 76                        return next;
 77                    }
 78                    throw new NoSuchElementException();
 79                }
 80
 81                public void remove() {
 82                    throw new UnsupportedOperationException();
 83                }
 84            };
 85        }
 86    }
 87
 88    protected String xmlToJavaName(final String name) {
 89        final StringBuilder builder = new StringBuilder();
 90        final char[] charArray = name.toCharArray();
 91        boolean dash = false;
 92        final StringBuilder token = new StringBuilder();
 93        for (int i = 0; i < charArray.length; i++) {
 94            if (charArray[i] == '-') {
 95                appendToken(builder, token);
 96                dash = true;
 97                continue;
 98            }
 99            token.append(dash ? Character.toUpperCase(charArray[i]) : charArray[i]);
100            dash = false;
101        }
102        appendToken(builder, token);
103        return builder.toString();
104    }
105
106    protected void appendToken(final StringBuilder builder, final StringBuilder token) {
107        String string = token.toString();
108        if ("Jvm".equals(string)) {
109            string = "JVM";
110        }
111        builder.append(string);
112        token.setLength(0);
113    }
114
115    protected String getTextContent(final Node node) {
116        return getTextContent2(node);
117    }
118
119    protected String getTextContent2(final Node node) {
120        final Node child = node.getFirstChild();
121        if (child != null) {
122            final Node next = child.getNextSibling();
123            if (next == null) {
124                return hasTextContent(child) ? child.getNodeValue() : "";
125            }
126            final StringBuffer buf = new StringBuffer();
127            getTextContent2(node, buf);
128            return buf.toString();
129        }
130        return "";
131    }
132
133    protected void getTextContent2(final Node node, final StringBuffer buf) {
134        Node child = node.getFirstChild();
135        while (child != null) {
136            if (hasTextContent(child)) {
137                getTextContent2(child, buf);
138            }
139            child = child.getNextSibling();
140        }
141    }
142
143    protected String getValue(org.w3c.dom.Node node) {
144        return node != null ? getTextContent(node).trim() : "";
145    }
146
147    protected final boolean hasTextContent(final Node child) {
148        final short nodeType = child.getNodeType();
149        final boolean result = nodeType != Node.COMMENT_NODE &&
150                nodeType != Node.PROCESSING_INSTRUCTION_NODE;
151        return result;
152    }
153
154    public final String cleanNodeName(final Node node) {
155        return cleanNodeName(node.getNodeName());
156    }
157
158    public final static String cleanNodeName(final String nodeName) {
159        String name = nodeName;
160        if (name != null) {
161            name = nodeName.replaceAll("\\w+:", "").toLowerCase();
162        }
163        return name;
164    }
165
166    protected boolean checkTrue(final String value) {
167        return "true".equalsIgnoreCase(value) ||
168                "yes".equalsIgnoreCase(value) ||
169                "on".equalsIgnoreCase(value);
170    }
171}