001: /*
002: * XML 2 Java Binding (X2JB) - the excellent Java tool.
003: * Copyright 2007, by Richard Opalka.
004: *
005: * This is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU Lesser General Public License as
007: * published by the Free Software Foundation; either version 2.1 of
008: * the License, or (at your option) any later version.
009: *
010: * This software is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this software; if not see the FSF site:
017: * http://www.fsf.org/ and search for the LGPL License document there.
018: */
019: package org.x2jb.bind;
020:
021: import java.lang.reflect.Method;
022: import java.lang.reflect.Array;
023: import java.text.MessageFormat;
024: import java.util.List;
025: import java.util.ArrayList;
026: import java.util.Collections;
027: import org.x2jb.bind.Messages;
028: import org.x2jb.bind.Messages.BundleKey;
029: import org.x2jb.bind.provider.BindingDefinition;
030: import org.w3c.dom.Attr;
031: import org.w3c.dom.Node;
032: import org.w3c.dom.Element;
033: import org.w3c.dom.NodeList;
034:
035: /**
036: * XML 2 Java helper class
037: *
038: * @author <a href="mailto:richard_opalka@yahoo.com">Richard Opalka</a>
039: * @version 1.0
040: */
041: final class Helper {
042:
043: static final String EMPTY_STRING = "";
044: static final String POINT_STRING = ".";
045: static final String STAR_STRING = "*";
046:
047: /**
048: * Constructor
049: */
050: private Helper() {
051: // no instances
052: }
053:
054: static void ensureIfaceIsMappedToElement(Class iface,
055: BindingDefinition binding) throws BindingException {
056: if (!binding.isElementNode()) {
057: // iface cannot be mapped to attribute
058: throw new BindingException(MessageFormat.format(Messages
059: .get(BundleKey.IFACE_MAPPED_TO_ATTRIBUTE),
060: new Object[] { iface.getName() }));
061: }
062: }
063:
064: static String getNamespace(Element element) {
065: return (element.getNamespaceURI() == null) ? EMPTY_STRING
066: : element.getNamespaceURI();
067: }
068:
069: static String getName(BindingDefinition binding)
070: throws BindingException {
071: String nodeName = binding.getNodeName();
072: return (nodeName == null) ? EMPTY_STRING : nodeName;
073: }
074:
075: static String getAbsNode(String namespace, String localPart) {
076: return namespace.equals(EMPTY_STRING) ? localPart : "{"
077: + namespace + "}" + localPart;
078: }
079:
080: static String getNamespace(BindingDefinition binding,
081: String parentNamespace) throws BindingException {
082: String currentNamespace = (binding.getNodeNamespace() == null) ? EMPTY_STRING
083: : binding.getNodeNamespace();
084: return EMPTY_STRING.equals(currentNamespace) ? parentNamespace
085: : currentNamespace;
086: }
087:
088: static String getNamespace(Element element, String parentNamespace)
089: throws BindingException {
090: String currentNamespace = (getNamespace(element) == null) ? EMPTY_STRING
091: : getNamespace(element);
092: return EMPTY_STRING.equals(currentNamespace) ? parentNamespace
093: : currentNamespace;
094: }
095:
096: static boolean mandatoryAttributeIsMissing(String attrValue,
097: BindingDefinition binding) throws BindingException {
098: return ((attrValue == null) || (attrValue.equals(EMPTY_STRING)))
099: && (binding.isNodeMandatory());
100: }
101:
102: static boolean attributeValueIsDefined(String attributeValue) {
103: return (attributeValue != null)
104: && (!attributeValue.equals(EMPTY_STRING));
105: }
106:
107: static Object bindAttribute(Class returnType, Attr attribute,
108: BindingDefinition binding) throws BindingException {
109: return HandlersRepository.bindAttributeToObject(returnType,
110: attribute, binding.getTypeHandler());
111: }
112:
113: static Object bindElement(Class returnType, Element element,
114: BindingDefinition binding) throws BindingException {
115: return HandlersRepository.bindElementToObject(returnType,
116: element, binding.getTypeHandler());
117: }
118:
119: static Object bindElements(Class returnType, Element[] elements,
120: BindingDefinition binding) throws BindingException {
121: return HandlersRepository.bindElementsToObjectArray(returnType,
122: elements, binding.getTypeHandler());
123: }
124:
125: static Object defaultValue(Class returnType,
126: BindingDefinition binding) throws BindingException {
127: return HandlersRepository.getDefaultValue(returnType, binding);
128: }
129:
130: static Object createEmptyArray(Class returnType)
131: throws BindingException {
132: return Array.newInstance(returnType, 0);
133: }
134:
135: static List getElements(Element e, String elementNS,
136: String elementName) {
137: if (elementName.equals(POINT_STRING)) {
138: List retVal = new ArrayList();
139: retVal.add(e);
140: return retVal;
141: }
142: if (!e.hasChildNodes())
143: return Collections.EMPTY_LIST;
144: NodeList childNodes = e.getChildNodes();
145: List retVal = new ArrayList();
146: for (int i = 0; i < childNodes.getLength(); i++) {
147: Node child = childNodes.item(i);
148: if (child.getNodeType() == Node.ELEMENT_NODE) {
149: Element candidate = (Element) child;
150: if (elementName.equals(STAR_STRING)) {
151: retVal.add(candidate);
152: } else {
153: if (elementNS.length() > 0) {
154: // namespace was specified
155: if (!candidate.getLocalName().equals(
156: elementName))
157: continue;
158: if (!candidate.getNamespaceURI().equals(
159: elementNS))
160: continue;
161: retVal.add(candidate);
162: } else {
163: // namespace wasn't specified
164: if (!candidate.getTagName().equals(elementName))
165: continue;
166: if (candidate.getNamespaceURI() != null)
167: continue;
168: retVal.add(candidate);
169: }
170: }
171: }
172: }
173: return retVal;
174: }
175:
176: static BindingDefinition getBinding(Method method)
177: throws BindingException {
178: return FactoryProvider.INSTANCE.getBinding(method);
179: }
180:
181: }
|