1 /** 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package org.apache.geronimo.st.core.jaxb; 19 20 import java.util.ArrayList; 21 import java.util.HashMap; 22 import java.util.Map; 23 24 import org.w3c.dom.Attr; 25 import org.w3c.dom.Element; 26 import org.w3c.dom.NamedNodeMap; 27 import org.w3c.dom.Node; 28 import org.w3c.dom.NodeList; 29 30 import com.sun.xml.bind.marshaller.NamespacePrefixMapper; 31 32 /** 33 * @version $Rev: 815171 $ $Date: 2009-09-15 14:55:16 +0800 (Tue, 15 Sep 2009) $ 34 */ 35 public class NamespacePrefix extends NamespacePrefixMapper{ 36 37 private static Map<String, String> prefixMap = new HashMap<String, String>(); 38 39 static { 40 prefixMap.put("http://geronimo.apache.org/xml/ns/deployment-1.2", "dep"); 41 prefixMap.put("http://geronimo.apache.org/xml/ns/j2ee/application-2.0", "app"); 42 prefixMap.put("http://geronimo.apache.org/xml/ns/j2ee/application-client-2.0", "client"); 43 prefixMap.put("http://geronimo.apache.org/xml/ns/j2ee/connector-1.2", "conn"); 44 prefixMap.put("http://openejb.apache.org/xml/ns/openejb-jar-2.2", "ejb"); 45 prefixMap.put("http://java.sun.com/xml/ns/persistence", "pers"); 46 prefixMap.put("http://openejb.apache.org/xml/ns/pkgen-2.1", "pkgen"); 47 prefixMap.put("http://geronimo.apache.org/xml/ns/naming-1.2", "name"); 48 prefixMap.put("http://geronimo.apache.org/xml/ns/security-2.0", "sec"); 49 prefixMap.put("http://geronimo.apache.org/xml/ns/j2ee/web-2.0.1", "web"); 50 prefixMap.put("http://geronimo.apache.org/xml/ns/loginconfig-2.0", "log"); 51 } 52 53 public static void processPrefix( Node parent ) { 54 NodeList nl = parent.getChildNodes(); 55 56 if ( parent instanceof Element ) { 57 updatePrefix( (Element)parent ); 58 } 59 60 for ( int i = 0; i <= nl.getLength(); i ++ ) { 61 Node node = nl.item(i); 62 if ( node instanceof Element ) { 63 processPrefix( node ); 64 } 65 } 66 67 } 68 69 private static void updatePrefix( Element element ) { 70 NamedNodeMap mnm = element.getAttributes(); 71 72 ArrayList<Attr> attributes = new ArrayList<Attr>(); 73 for ( int j = 0; j <= mnm.getLength(); j ++ ) { 74 Attr attr = (Attr)mnm.item(j); 75 if ( attr != null && attr.getOwnerElement() != null && getPrefix( attr.getNodeValue() ) != null ) { 76 attributes.add((Attr)attr.cloneNode(false)); 77 } 78 } 79 for ( int j = 0; j < attributes.size(); j ++ ) { 80 Attr tempAttr = attributes.get(j); 81 Attr attr = element.getAttributeNode(tempAttr.getName()); 82 Element owner = (Element)attr.getOwnerElement(); 83 owner.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:" + getPrefix( attr.getNodeValue() ), attr.getNodeValue()); 84 owner.removeAttributeNode(attr); 85 } 86 String prefix = getPrefix( element.getNamespaceURI() ); 87 if ( prefix != null ) { 88 element.setPrefix( prefix ); 89 } 90 } 91 92 private static String getPrefix(String namespaceURI) { 93 if (prefixMap.containsKey(namespaceURI)) 94 return prefixMap.get(namespaceURI); 95 return null; 96 } 97 98 @Override 99 public String getPreferredPrefix(String namespaceUri, String suggestion, boolean requirePrefix) { 100 101 if (prefixMap.containsKey(namespaceUri)) 102 return prefixMap.get(namespaceUri); 103 104 return suggestion; 105 } 106 107 }