|
View:
New views
2 Messages
—
Rating Filter:
Alert me
|
|
|
Suppressing unnecessary namespace declarationsMy Atom project makes heavy use of ROME modules. As I mentioned in January, it is annoying (and to some people, even disturbing) to have 30+ xmlns: declarations in every Atom feed we generate, when only 4-5 modules (5-6 namespaces) are needed or used in any one feed.
I asked in January how to get ROME to suppress the excessive declarations, and was told there isn't a way. But there is a way, if you are willing to modify and re-build the ROME source. I offer the following code for anyone with a similar problem... although I really think the ROME developers should fix it in their base code. Anyway, in com/sun/syndication/io/impl/Atom10Generator.java, I now have this: public Document generate(WireFeed wFeed) throws FeedException { Feed feed = (Feed) wFeed; Element root = createRootElement(feed); populateFeed(feed,root); purgeUnusedNamespaceDeclarations(root); // sfly WEB-17627 introduces this line return createDocument(root); } /** * sfly WEB-17627 introduces this method * * Purging unused declarations is less optimal, performance-wise, than never * adding them in the first place. So, we should still ask the ROME guys to * fix their code (not adding dozens of unnecessary module declarations). * Having said that: purging them here, before XML generation, is more * efficient than parsing and re-molding the XML after ROME generates it. * * Note that the calling app could still add declarations/modules to the Feed * tree after this. Which is fine. But those modules are then responsible * for crawling to the root of the tree, at generate() time, to make sure * their namespace declarations are present. */ static private void purgeUnusedNamespaceDeclarations(Element root) { java.util.Set<String> usedPrefixes = new java.util.HashSet<String>(); collectUsedPrefixes(root, usedPrefixes); List list = root.getAdditionalNamespaces(); List<org.jdom.Namespace> additionalNamespaces = new java.util.ArrayList<org.jdom.Namespace>(); additionalNamespaces.addAll(list); // the duplication will prevent a ConcurrentModificationException below for (org.jdom.Namespace ns: additionalNamespaces) { String prefix = ns.getPrefix(); if (prefix != null && prefix.length() > 0 && !usedPrefixes.contains(prefix)) { root.removeNamespaceDeclaration(ns); } } return; } /** * sfly WEB-17627 introduces this method */ static private void collectUsedPrefixes (Element el, java.util.Set<String> collector) { String prefix = el.getNamespacePrefix(); if (prefix != null && prefix.length() > 0 && !collector.contains(prefix)) { collector.add(prefix); } List<org.jdom.Element> kids = el.getChildren(); for (Element kid: kids) { collectUsedPrefixes (kid, collector); // recursion - worth it } } --------------------------------------------------------------------- To unsubscribe, e-mail: users-unsubscribe@... For additional commands, e-mail: users-help@... |
|
|
Re: Suppressing unnecessary namespace declarationsJeff,
I've just had time to look at it, clever. I'm incorporating it to ROME code for all generators. Thanks. Alejandro On Sat, Jun 28, 2008 at 12:24 AM, Jeff Carty <jcarty@...> wrote: > My Atom project makes heavy use of ROME modules. As I mentioned in January, it is annoying (and to some people, even disturbing) to have 30+ xmlns: declarations in every Atom feed we generate, when only 4-5 modules (5-6 namespaces) are needed or used in any one feed. > > I asked in January how to get ROME to suppress the excessive declarations, and was told there isn't a way. But there is a way, if you are willing to modify and re-build the ROME source. I offer the following code for anyone with a similar problem... although I really think the ROME developers should fix it in their base code. > > Anyway, in com/sun/syndication/io/impl/Atom10Generator.java, I now have this: > > public Document generate(WireFeed wFeed) throws FeedException { > Feed feed = (Feed) wFeed; > Element root = createRootElement(feed); > populateFeed(feed,root); > purgeUnusedNamespaceDeclarations(root); // sfly WEB-17627 introduces this line > return createDocument(root); > } > > /** > * sfly WEB-17627 introduces this method > * > * Purging unused declarations is less optimal, performance-wise, than never > * adding them in the first place. So, we should still ask the ROME guys to > * fix their code (not adding dozens of unnecessary module declarations). > * Having said that: purging them here, before XML generation, is more > * efficient than parsing and re-molding the XML after ROME generates it. > * > * Note that the calling app could still add declarations/modules to the Feed > * tree after this. Which is fine. But those modules are then responsible > * for crawling to the root of the tree, at generate() time, to make sure > * their namespace declarations are present. > */ > static private void purgeUnusedNamespaceDeclarations(Element root) > { > java.util.Set<String> usedPrefixes = new java.util.HashSet<String>(); > collectUsedPrefixes(root, usedPrefixes); > > List list = root.getAdditionalNamespaces(); > List<org.jdom.Namespace> additionalNamespaces = new java.util.ArrayList<org.jdom.Namespace>(); > additionalNamespaces.addAll(list); // the duplication will prevent a ConcurrentModificationException below > > for (org.jdom.Namespace ns: additionalNamespaces) { > String prefix = ns.getPrefix(); > if (prefix != null && prefix.length() > 0 && !usedPrefixes.contains(prefix)) { > root.removeNamespaceDeclaration(ns); > } > } > return; > } > > /** > * sfly WEB-17627 introduces this method > */ > static private void collectUsedPrefixes (Element el, java.util.Set<String> collector) > { > String prefix = el.getNamespacePrefix(); > if (prefix != null && prefix.length() > 0 && !collector.contains(prefix)) { > collector.add(prefix); > } > List<org.jdom.Element> kids = el.getChildren(); > for (Element kid: kids) { > collectUsedPrefixes (kid, collector); // recursion - worth it > } > } > > > --------------------------------------------------------------------- > To unsubscribe, e-mail: users-unsubscribe@... > For additional commands, e-mail: users-help@... > > --------------------------------------------------------------------- To unsubscribe, e-mail: users-unsubscribe@... For additional commands, e-mail: users-help@... |
| Free Forum Powered by Nabble | Forum Help |