001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.tools.util;
019:
020: import java.io.File;
021: import java.io.FileOutputStream;
022:
023: import org.w3c.dom.Document;
024: import org.w3c.dom.Element;
025: import org.w3c.dom.Node;
026: import org.w3c.dom.NodeList;
027:
028: import org.apache.cxf.helpers.DOMUtils;
029: import org.apache.cxf.helpers.FileUtils;
030: import org.apache.cxf.helpers.XMLUtils;
031: import org.apache.cxf.tools.common.ToolConstants;
032:
033: public final class JAXBUtils {
034: private JAXBUtils() {
035: }
036:
037: private static Node innerJaxbBinding(Element schema) {
038: String schemaNamespace = schema.getNamespaceURI();
039: NodeList annoList = schema.getElementsByTagNameNS(
040: schemaNamespace, "annotation");
041: Element annotation = null;
042: if (annoList.getLength() > 0) {
043: annotation = (Element) annoList.item(0);
044: } else {
045: annotation = schema.getOwnerDocument().createElementNS(
046: schemaNamespace, "annotation");
047: }
048:
049: NodeList appList = annotation.getElementsByTagNameNS(
050: schemaNamespace, "appinfo");
051: Element appInfo = null;
052: if (appList.getLength() > 0) {
053: appInfo = (Element) appList.item(0);
054: } else {
055: appInfo = schema.getOwnerDocument().createElementNS(
056: schemaNamespace, "appinfo");
057: annotation.appendChild(appInfo);
058: }
059:
060: Element jaxbBindings = null;
061: NodeList jaxbList = schema.getElementsByTagNameNS(
062: ToolConstants.NS_JAXB_BINDINGS, "schemaBindings");
063: if (jaxbList.getLength() > 0) {
064: jaxbBindings = (Element) jaxbList.item(0);
065: } else {
066: jaxbBindings = schema.getOwnerDocument().createElementNS(
067: ToolConstants.NS_JAXB_BINDINGS, "schemaBindings");
068: appInfo.appendChild(jaxbBindings);
069: }
070: return jaxbBindings;
071:
072: }
073:
074: public static Node innerJaxbPackageBinding(Element schema,
075: String packagevalue) {
076: Document doc = schema.getOwnerDocument();
077:
078: if (!XMLUtils.hasAttribute(schema,
079: ToolConstants.NS_JAXB_BINDINGS)) {
080: schema.setAttributeNS(ToolConstants.NS_JAXB_BINDINGS,
081: "version", "2.0");
082: }
083:
084: Node schemaBindings = innerJaxbBinding(schema);
085:
086: NodeList pkgList = doc.getElementsByTagNameNS(
087: ToolConstants.NS_JAXB_BINDINGS, "package");
088: Element packagename = null;
089: if (pkgList.getLength() > 0) {
090: packagename = (Element) pkgList.item(0);
091: } else {
092: packagename = doc.createElementNS(
093: ToolConstants.NS_JAXB_BINDINGS, "package");
094: }
095: packagename.setAttribute("name", packagevalue);
096:
097: schemaBindings.appendChild(packagename);
098:
099: return schemaBindings.getParentNode().getParentNode();
100: }
101:
102: /**
103: * Create the jaxb binding file to customize namespace to package mapping
104: *
105: * @param namespace
106: * @param pkgName
107: * @return file
108: */
109: public static File getPackageMappingSchemaBindingFile(
110: String namespace, String pkgName) {
111: Document doc = DOMUtils.createDocument();
112: Element rootElement = doc.createElement("schema");
113: rootElement.setAttribute("xmlns", ToolConstants.SCHEMA_URI);
114: rootElement.setAttribute("xmlns:jaxb",
115: ToolConstants.NS_JAXB_BINDINGS);
116: rootElement.setAttribute("jaxb:version", "2.0");
117: rootElement.setAttribute("targetNamespace", namespace);
118: Element annoElement = doc.createElement("annotation");
119: Element appInfo = doc.createElement("appinfo");
120: Element schemaBindings = doc
121: .createElement("jaxb:schemaBindings");
122: Element pkgElement = doc.createElement("jaxb:package");
123: pkgElement.setAttribute("name", pkgName);
124: annoElement.appendChild(appInfo);
125: appInfo.appendChild(schemaBindings);
126: schemaBindings.appendChild(pkgElement);
127: rootElement.appendChild(annoElement);
128: File tmpFile = null;
129: try {
130: tmpFile = FileUtils.createTempFile("customzied", ".xsd");
131: FileOutputStream fout = new FileOutputStream(tmpFile);
132: DOMUtils.writeXml(rootElement, fout);
133: fout.close();
134: } catch (Exception e) {
135: e.printStackTrace();
136: }
137: return tmpFile;
138: }
139: }
|