Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.hazelcast.internal.config.DomConfigHelper Maven / Gradle / Ivy
/*
* Copyright (c) 2008-2020, Hazelcast, Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.hazelcast.internal.config;
import com.hazelcast.config.InvalidConfigurationException;
import com.hazelcast.core.HazelcastException;
import com.hazelcast.config.AbstractXmlConfigHelper;
import com.hazelcast.internal.util.StringUtil;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import java.util.Iterator;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Properties;
import static com.hazelcast.internal.util.StringUtil.isNullOrEmpty;
import static java.lang.Boolean.parseBoolean;
import static java.lang.Double.parseDouble;
import static java.lang.String.format;
/**
* Helper class for accessing and extracting values from W3C DOM {@link Node}
* instances
*
* @see AbstractDomConfigProcessor
* @see AbstractXmlConfigHelper
*/
public final class DomConfigHelper {
private DomConfigHelper() {
}
public static void fillProperties(final Node node, Map properties, boolean domLevel3) {
if (properties == null) {
return;
}
for (Node n : childElements(node)) {
if (n.getNodeType() == Node.TEXT_NODE || n.getNodeType() == Node.COMMENT_NODE) {
continue;
}
final String propertyName = getTextContent(n.getAttributes().getNamedItem("name"), domLevel3).trim();
final String value = getTextContent(n, domLevel3).trim();
properties.put(propertyName, value);
}
}
public static void fillProperties(final Node node, Properties properties, boolean domLevel3) {
if (properties == null) {
return;
}
for (Node n : childElements(node)) {
final String propertyName = getTextContent(n.getAttributes().getNamedItem("name"), domLevel3).trim();
final String value = getTextContent(n, domLevel3).trim();
properties.setProperty(propertyName, value);
}
}
public static Iterable childElements(Node node) {
return new IterableNodeList(node, Node.ELEMENT_NODE, null);
}
public static Iterable childElementsWithName(Node node, String nodeName) {
return new IterableNodeList(node, Node.ELEMENT_NODE, nodeName);
}
public static Node childElementWithName(Node node, String nodeName) {
Iterator it = childElementsWithName(node, nodeName).iterator();
return it.hasNext() ? it.next() : null;
}
public static Node firstChildElement(Node node) {
Iterator it = new IterableNodeList(node, Node.ELEMENT_NODE, null).iterator();
return it.hasNext() ? it.next() : null;
}
public static Iterable asElementIterable(NodeList list) {
return new IterableNodeList(list, Node.ELEMENT_NODE, null);
}
public static String cleanNodeName(final Node node) {
final String nodeName = node.getLocalName();
if (nodeName == null) {
throw new HazelcastException("Local node name is null for " + node);
}
return StringUtil.lowerCaseInternal(nodeName);
}
public static String getTextContent(final Node node, boolean domLevel3) {
if (node != null) {
final String text;
if (domLevel3) {
text = node.getTextContent();
} else {
text = getTextContentOld(node);
}
return text != null ? text.trim() : "";
}
return "";
}
private static String getTextContentOld(final Node node) {
final Node child = node.getFirstChild();
if (child != null) {
final Node next = child.getNextSibling();
if (next == null) {
return hasTextContent(child) ? child.getNodeValue() : "";
}
final StringBuilder buf = new StringBuilder();
appendTextContents(node, buf);
return buf.toString();
}
return "";
}
private static void appendTextContents(final Node node, final StringBuilder buf) {
Node child = node.getFirstChild();
while (child != null) {
if (hasTextContent(child)) {
buf.append(child.getNodeValue());
}
child = child.getNextSibling();
}
}
private static boolean hasTextContent(final Node node) {
final short nodeType = node.getNodeType();
return nodeType != Node.COMMENT_NODE && nodeType != Node.PROCESSING_INSTRUCTION_NODE;
}
public static boolean getBooleanValue(final String value) {
return parseBoolean(StringUtil.lowerCaseInternal(value));
}
public static int getIntegerValue(final String parameterName, final String value) {
try {
return Integer.parseInt(value);
} catch (final NumberFormatException e) {
throw new InvalidConfigurationException(format("Invalid integer value for parameter %s: %s", parameterName, value));
}
}
public static int getIntegerValue(final String parameterName, final String value, int defaultValue) {
if (isNullOrEmpty(value)) {
return defaultValue;
}
return getIntegerValue(parameterName, value);
}
public static long getLongValue(final String parameterName, final String value) {
try {
return Long.parseLong(value);
} catch (final Exception e) {
throw new InvalidConfigurationException(
format("Invalid long integer value for parameter %s: %s", parameterName, value));
}
}
public static long getLongValue(final String parameterName, final String value, long defaultValue) {
if (isNullOrEmpty(value)) {
return defaultValue;
}
return getLongValue(parameterName, value);
}
public static double getDoubleValue(final String parameterName, final String value) {
try {
return parseDouble(value);
} catch (final Exception e) {
throw new InvalidConfigurationException(
format("Invalid long integer value for parameter %s: %s", parameterName, value));
}
}
public static double getDoubleValue(final String parameterName, final String value, double defaultValue) {
if (isNullOrEmpty(value)) {
return defaultValue;
}
return getDoubleValue(parameterName, value);
}
public static String getAttribute(Node node, String attName, boolean domLevel3) {
final Node attNode = node.getAttributes().getNamedItem(attName);
if (attNode == null) {
return null;
}
return getTextContent(attNode, domLevel3);
}
private static class IterableNodeList implements Iterable {
private final NodeList wrapped;
private final int maximum;
private final short nodeType;
private final String nodeName;
IterableNodeList(Node parent, short nodeType, String nodeName) {
this(parent.getChildNodes(), nodeType, nodeName);
}
IterableNodeList(NodeList wrapped, short nodeType, String nodeName) {
this.wrapped = wrapped;
this.nodeType = nodeType;
this.maximum = wrapped.getLength();
this.nodeName = nodeName;
}
@Override
public Iterator iterator() {
return new Iterator() {
private int index;
private Node next;
public boolean hasNext() {
next = null;
for (; index < maximum; index++) {
final Node item = wrapped.item(index);
if ((nodeType == 0 || item.getNodeType() == nodeType)
&& (nodeName == null || nodeName.equals(cleanNodeName(item)))) {
next = item;
return true;
}
}
return false;
}
public Node next() {
if (hasNext()) {
index++;
return next;
}
throw new NoSuchElementException();
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
};
}
}
}