1 package yarfraw.utils; 2 3 import java.util.Collections; 4 import java.util.HashMap; 5 import java.util.Map; 6 7 import javax.xml.bind.JAXBContext; 8 import javax.xml.bind.JAXBException; 9 import javax.xml.bind.Marshaller; 10 11 import com.sun.xml.bind.marshaller.NamespacePrefixMapper; 12 13 import yarfraw.core.datamodel.FeedFormat; 14 15 public class JAXBUtils { 16 public static final String PREFIX_MAPPER_PROPERTY_NAME = "com.sun.xml.bind.namespacePrefixMapper"; 17 private static JAXBContext RSS20_CONTEXT = null; 18 private static JAXBContext RSS10_CONTEXT = null; 19 private static JAXBContext ATOM10_CONTEXT = null; 20 private static JAXBContext ATOM03_CONTEXT = null; 21 22 23 private static Map<String, String> _extensionPrefixMap = null; 24 private static Map<String, String> _rss10PrefixMap = null; 25 private static Map<String, String> _rss20PrefixMap = null; 26 private static Map<String, String> _atom10PrefixMap = null; 27 private static Map<String, String> _atom03PrefixMap = null; 28 29 /*** 30 * Gets the namespace prefix map for the marshaller. 31 * @param format a {@link FeedFormat} 32 * @return a {@link NamespacePrefixMapper} object for the {@link Marshaller} 33 */ 34 public static NamespacePrefixMapper getNamespacePrefixMapper(FeedFormat format) { 35 if(format == FeedFormat.ATOM10){ 36 return ATOM10_PREFIX_MAPPER; 37 }else if(format == FeedFormat.RSS10){ 38 return RSS10_PREFIX_MAPPER; 39 }else if(format == FeedFormat.RSS20){ 40 return RSS20_PREFIX_MAPPER; 41 }else if(format == FeedFormat.ATOM03){ 42 return ATOM03_PREFIX_MAPPER; 43 }else{ 44 throw new UnsupportedOperationException("Unknown format: "+format); 45 } 46 } 47 48 private static NamespacePrefixMapper ATOM03_PREFIX_MAPPER = new NamespacePrefixMapper(){ 49 50 @Override 51 public String getPreferredPrefix(String namespaceUri, String suggestion, boolean requirePrefix) { 52 Map<String, String> prefixMap = getAtom03PrefixMap(); 53 return prefixMap.get(namespaceUri); 54 } 55 56 }; 57 58 private static NamespacePrefixMapper ATOM10_PREFIX_MAPPER = new NamespacePrefixMapper(){ 59 60 @Override 61 public String getPreferredPrefix(String namespaceUri, String suggestion, boolean requirePrefix) { 62 Map<String, String> prefixMap = getAtom10PrefixMap(); 63 return prefixMap.get(namespaceUri); 64 } 65 66 }; 67 68 private static NamespacePrefixMapper RSS10_PREFIX_MAPPER = new NamespacePrefixMapper(){ 69 70 @Override 71 public String getPreferredPrefix(String namespaceUri, String suggestion, boolean requirePrefix) { 72 Map<String, String> prefixMap = getRss10PrefixMap(); 73 return prefixMap.get(namespaceUri); 74 } 75 76 }; 77 78 private static NamespacePrefixMapper RSS20_PREFIX_MAPPER = new NamespacePrefixMapper(){ 79 80 @Override 81 public String getPreferredPrefix(String namespaceUri, String suggestion, boolean requirePrefix) { 82 Map<String, String> prefixMap = getRss20PrefixMap(); 83 return prefixMap.get(namespaceUri); 84 } 85 86 }; 87 88 private synchronized static Map<String, String> getRss10PrefixMap(){ 89 if(_rss10PrefixMap == null){ 90 _rss10PrefixMap = new HashMap<String, String>(); 91 _rss10PrefixMap.put("http://purl.org/rss/1.0/", ""); //rss 1.0 elements go to default 92 _rss10PrefixMap.putAll(getExtensionPrefixMap()); 93 _rss10PrefixMap = Collections.unmodifiableMap(_rss10PrefixMap); 94 } 95 return _rss10PrefixMap; 96 } 97 98 private synchronized static Map<String, String> getRss20PrefixMap(){ 99 if(_rss20PrefixMap == null){ 100 _rss20PrefixMap = new HashMap<String, String>(); 101 _rss20PrefixMap.putAll(getExtensionPrefixMap()); 102 _rss20PrefixMap = Collections.unmodifiableMap(_rss20PrefixMap); 103 } 104 return _rss20PrefixMap; 105 } 106 107 private synchronized static Map<String, String> getAtom10PrefixMap(){ 108 if(_atom03PrefixMap == null){ 109 _atom03PrefixMap = new HashMap<String, String>(); 110 _atom03PrefixMap.put("http://www.w3.org/2005/Atom", ""); //atom 1.0 elements go to default 111 _atom03PrefixMap.putAll(getExtensionPrefixMap()); 112 _atom03PrefixMap = Collections.unmodifiableMap(_atom03PrefixMap); 113 } 114 return _atom03PrefixMap; 115 } 116 117 private synchronized static Map<String, String> getAtom03PrefixMap(){ 118 if(_atom10PrefixMap == null){ 119 _atom10PrefixMap = new HashMap<String, String>(); 120 _atom10PrefixMap.put("http://purl.org/atom/ns#", ""); //atom 0.3 elements go to default 121 _atom10PrefixMap.putAll(getExtensionPrefixMap()); 122 _atom10PrefixMap = Collections.unmodifiableMap(_atom10PrefixMap); 123 } 124 return _atom10PrefixMap; 125 } 126 127 private synchronized static Map<String, String> getExtensionPrefixMap(){ 128 if(_extensionPrefixMap == null){ 129 _extensionPrefixMap = new HashMap<String, String>(); 130 _extensionPrefixMap.put("http://www.w3.org/1999/02/22-rdf-syntax-ns#", "rdf"); 131 _extensionPrefixMap.put("http://www.w3.org/XML/1998/namespace", "xml"); 132 _extensionPrefixMap.put("http://base.google.com/ns/1.0", "g" );//google base 133 _extensionPrefixMap.put("http://www.itunes.com/dtds/podcast-1.0.dtd", "itunes"); 134 _extensionPrefixMap.put("http://tools.search.yahoo.com/mrss/", "mrss"); 135 _extensionPrefixMap.put("http://wellformedweb.org/CommentAPI/", "wfw"); 136 _extensionPrefixMap.put("http://www.georss.org/georss/10", "georss"); 137 _extensionPrefixMap.put("http://purl.org/dc/elements/1.1/", "dc"); 138 _extensionPrefixMap.put("http://purl.org/rss/1.0/modules/syndication/", "sy"); 139 _extensionPrefixMap.put("http://webns.net/mvcb/", "admin"); 140 _extensionPrefixMap.put("http://rssnamespace.org/feedburner/ext/1.0", "feedburner"); 141 _extensionPrefixMap.put("http://purl.org/rss/1.0/modules/slash/", "slash"); 142 _extensionPrefixMap.put("http://www.blogger.com/atom/ns#", "blogger"); 143 _extensionPrefixMap.put("http://purl.org/atom-blog/ns#", "draft"); 144 _extensionPrefixMap.put("http://www.w3.org/1999/xhtml", "xhtml"); 145 _extensionPrefixMap = Collections.unmodifiableMap(_extensionPrefixMap); 146 } 147 return _extensionPrefixMap; 148 } 149 150 /*** 151 * Gets the {@link JAXBContext} based on the input {@link FeedFormat} 152 * @param format 153 * @return 154 * @throws JAXBException 155 */ 156 public static synchronized JAXBContext getContext(FeedFormat format) throws JAXBException{ 157 158 if(format == FeedFormat.RSS10){ 159 if(RSS10_CONTEXT == null){ 160 RSS10_CONTEXT = JAXBContext.newInstance(CommonUtils.RSS10_JAXB_CONTEXT); 161 } 162 return RSS10_CONTEXT; 163 } 164 if(format == FeedFormat.RSS20){ 165 if(RSS20_CONTEXT == null){ 166 RSS20_CONTEXT = JAXBContext.newInstance(CommonUtils.RSS20_JAXB_CONTEXT); 167 } 168 return RSS20_CONTEXT; 169 } 170 171 if(format == FeedFormat.ATOM10){ 172 if(ATOM10_CONTEXT == null){ 173 ATOM10_CONTEXT = JAXBContext.newInstance(CommonUtils.ATOM10_JAXB_CONTEXT); 174 } 175 return ATOM10_CONTEXT; 176 } 177 178 if(format == FeedFormat.ATOM03){ 179 if(ATOM03_CONTEXT == null){ 180 ATOM03_CONTEXT = JAXBContext.newInstance(CommonUtils.ATOM03_JAXB_CONTEXT); 181 } 182 return ATOM03_CONTEXT; 183 } 184 throw new UnsupportedOperationException("Unsupported format: "+ format); 185 186 } 187 }