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