001: /*
002: * Copyright 2005 jWic Group (http://www.jwic.de)
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: *
016: * de.jwic.util.XMLTool
017: * Created on 14.04.2005
018: * $Id: XMLTool.java,v 1.2 2006/07/24 14:05:29 lordsam Exp $
019: */
020: package de.jwic.util;
021:
022: import java.util.Iterator;
023: import java.util.Properties;
024:
025: import org.w3c.dom.Element;
026: import org.w3c.dom.Node;
027: import org.w3c.dom.NodeList;
028:
029: /**
030: * Provides static methods used to read xml data.
031: *
032: * @author Florian Lippisch
033: * @version $Revision: 1.2 $
034: */
035: public class XMLTool {
036:
037: /**
038: * Creates a utf8 byte array from the input int number.
039: * @param input input int number.
040: * @return a utf8 byte array.
041: */
042: public static final byte[] toUtf8(int input) {
043: byte output[];
044: if (input <= 0x7F) {
045: output = new byte[1];
046: int x = input;
047: x = (x & 0x7F);
048: output[0] = (byte) x;
049: } else if (input >= 0x800) {
050: output = new byte[3];
051: int x = input;
052: int y = input;
053: int z = input;
054: x = ((x & 0xF000) >>> 12) | 0xE0;
055: y = ((y & 0xFC0) >>> 6) | 0x80;
056: z = (z & 0x3F) | 0x80;
057: output[0] = (byte) x;
058: output[1] = (byte) y;
059: output[2] = (byte) z;
060: } else {
061: output = new byte[2];
062: int x = input;
063: int y = input;
064: x = ((x & 0x7C0) >>> 6) | 0xC0;
065: y = (y & 0x3F) | 0x80;
066: output[0] = (byte) x;
067: output[1] = (byte) y;
068: }
069: return output;
070: }
071:
072: /**
073: * Creates a utf8 byte array from the input string.
074: * @param input input string.
075: * @return a utf8 byte array.
076: */
077: public static final String toUtf8(String input) {
078: StringBuffer output = new StringBuffer(input.length());
079: for (int i = 0; i < input.length(); i++) {
080: output.append(new String(toUtf8(input.charAt(i))));
081: }
082: return output.toString();
083: }
084:
085: /**
086: * Returns a <code>Properties</code> object from an <code>Node</code>. The
087: * node must have child elements as follows:
088: * <p><pre>
089: * <prop key="propertyKey">value</prop>
090: * </pre></p>
091: * @param node
092: * @return
093: */
094: public static Properties getProperties(Node node) {
095:
096: Properties p = new Properties();
097: NodeList nl = node.getChildNodes();
098:
099: for (int i = 0; i < nl.getLength(); i++) {
100: Node n = nl.item(i);
101: if (n.getNodeName().equals("prop")) {
102: Element e = (Element) n;
103: p.setProperty(e.getAttribute("key"), getText(n));
104: }
105: }
106:
107: return p;
108: }
109:
110: /**
111: * Returns a <code>Properties</code> object from an <code>org.dom4j.Element</code>. The
112: * node must have child elements as follows:
113: * <p><pre>
114: * <prop key="propertyKey">value</prop>
115: * </pre></p>
116: * @param node
117: * @return
118: */
119: public static Properties getProperties(
120: org.dom4j.Element dom4jElement) {
121:
122: Properties p = new Properties();
123: for (Iterator it = dom4jElement.elementIterator(); it.hasNext();) {
124: org.dom4j.Element n = (org.dom4j.Element) it.next();
125: if (n.getName().equals("prop")) {
126: p.setProperty(n.attribute("key").getValue(), n
127: .getText());
128: }
129: }
130:
131: return p;
132: }
133:
134: /**
135: * Returns the text (#text child) value of an element.
136: * @return java.lang.String
137: * @param node org.w3c.dom.Node
138: */
139: public static String getText(Node node) {
140:
141: StringBuffer sb = new StringBuffer();
142: NodeList children = node.getChildNodes();
143: if (children != null) {
144: int len = children.getLength();
145: for (int i = 0; i < len; i++) {
146: sb.append(children.item(i).getNodeValue());
147: }
148: }
149:
150: return sb.toString();
151: }
152:
153: }
|