001/* 002 * (C) Copyright 2012 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Thierry Delprat 018 */ 019package org.nuxeo.apidoc.documentation; 020 021import java.io.StringReader; 022import java.util.ArrayList; 023import java.util.Iterator; 024import java.util.List; 025 026import org.dom4j.Document; 027import org.dom4j.DocumentException; 028import org.dom4j.Element; 029import org.dom4j.Visitor; 030import org.dom4j.VisitorSupport; 031import org.dom4j.io.SAXReader; 032 033public class XMLContributionParser { 034 035 public static String prettyfy(String xml) throws DocumentException { 036 037 StringBuffer writter = new StringBuffer(); 038 039 SAXReader reader = new SAXReader(); 040 Document document = reader.read(new StringReader(xml)); 041 042 Element root = document.getRootElement(); 043 044 for (Iterator<?> i = root.elementIterator(); i.hasNext();) { 045 Element element = (Element) i.next(); 046 if (!element.getName().equals("documentation")) { 047 ContributionItem fragment = parseContrib(element); 048 fragment.write(writter); 049 } 050 } 051 return writter.toString(); 052 } 053 054 public static List<ContributionItem> extractContributionItems(String xml) throws DocumentException { 055 056 List<ContributionItem> items = new ArrayList<>(); 057 058 SAXReader reader = new SAXReader(); 059 Document document = reader.read(new StringReader(xml)); 060 061 Element root = document.getRootElement(); 062 063 for (Iterator<?> i = root.elementIterator(); i.hasNext();) { 064 Element element = (Element) i.next(); 065 if (!element.getName().equals("documentation")) { 066 ContributionItem fragment = parseContrib(element); 067 items.add(fragment); 068 } 069 } 070 return items; 071 } 072 073 protected static ContributionItem parseContrib(Element element) { 074 075 final ContributionItem fragment = new ContributionItem(); 076 fragment.tagName = element.getName(); 077 078 fragment.nameOrId = element.attributeValue("name"); 079 if (fragment.nameOrId == null) { 080 fragment.nameOrId = element.attributeValue("id"); 081 } 082 083 Visitor docExtractor = new VisitorSupport() { 084 @Override 085 public void visit(Element node) { 086 if ("documentation".equalsIgnoreCase(node.getName())) { 087 fragment.documentation = getNodeAsString(node); 088 } else if ("description".equalsIgnoreCase(node.getName())) { 089 fragment.documentation = getNodeAsString(node); 090 } else { 091 super.visit(node); 092 } 093 } 094 }; 095 element.accept(docExtractor); 096 097 fragment.xml = element.asXML(); 098 099 return fragment; 100 } 101 102 protected static String getNodeAsString(Element node) { 103 String data = node.asXML(); 104 data = data.substring(node.getName().length() + 2); 105 if (data.length() > node.getName().length()) { 106 data = data.substring(0, data.length() - node.getName().length() - 3); 107 } 108 data = data.trim(); 109 return data; 110 } 111}