001/* 002 * (C) Copyright 2006-2011 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 * bstefanescu 018 */ 019package org.nuxeo.runtime.model.persistence; 020 021import java.io.ByteArrayInputStream; 022import java.io.IOException; 023import java.io.InputStream; 024import java.net.URL; 025import java.util.ArrayList; 026import java.util.Arrays; 027import java.util.List; 028 029import javax.xml.parsers.DocumentBuilder; 030import javax.xml.parsers.DocumentBuilderFactory; 031import javax.xml.parsers.ParserConfigurationException; 032 033import org.nuxeo.common.xmap.DOMSerializer; 034import org.nuxeo.common.xmap.XMap; 035import org.w3c.dom.Document; 036import org.w3c.dom.Element; 037 038/** 039 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> 040 */ 041public class ContributionBuilder extends AbstractContribution { 042 043 protected final DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance(); 044 045 protected final List<String> extensions; 046 047 protected String bundle; 048 049 public ContributionBuilder(String name) { 050 super(name); 051 this.extensions = new ArrayList<>(); 052 } 053 054 @Override 055 public URL asURL() { 056 return null; 057 } 058 059 public void setBundle(String bundle) { 060 this.bundle = bundle; 061 } 062 063 @Override 064 public String getDescription() { 065 return description; 066 } 067 068 @Override 069 public boolean isDisabled() { 070 return disabled; 071 } 072 073 @Override 074 public void setDisabled(boolean isDisabled) { 075 this.disabled = isDisabled; 076 } 077 078 public void addXmlExtension(String target, String point, String content) { 079 StringBuilder sb = new StringBuilder(1024); 080 sb.append("<extension target=\"") 081 .append(target) 082 .append("\" point=\"") 083 .append(point) 084 .append("\">\n") 085 .append(content) 086 .append("\n</extension>"); 087 extensions.add(sb.toString()); 088 } 089 090 public void addExtension(String target, String point, Object... contribs) { 091 if (contribs != null && contribs.length > 0) { 092 addExtension(target, point, Arrays.asList(contribs)); 093 } 094 } 095 096 public void addExtension(String target, String point, List<Object> contribs) { 097 DocumentBuilder docBuilder; 098 try { 099 docBuilder = dbfac.newDocumentBuilder(); 100 } catch (ParserConfigurationException e) { 101 throw new RuntimeException(e); 102 } 103 Document doc = docBuilder.newDocument(); 104 // create root element 105 Element root = doc.createElement("extension"); 106 root.setAttribute("target", target); 107 root.setAttribute("point", point); 108 doc.appendChild(root); 109 110 XMap xmap = new XMap(); 111 for (Object contrib : contribs) { 112 xmap.register(contrib.getClass()); 113 xmap.toXML(contrib, root); 114 } 115 try { 116 extensions.add(DOMSerializer.toStringOmitXml(root)); 117 } catch (IOException e) { 118 throw new RuntimeException(e); 119 } 120 } 121 122 @Override 123 public String getContent() { 124 StringBuilder sb = new StringBuilder(1024 * 32); 125 sb.append("<component name=\"").append(ContributionPersistenceComponent.getComponentName(name)).append("\" "); 126 if (bundle != null) { 127 sb.append("bundle=\"").append(bundle).append("\" "); 128 } 129 sb.append(">\n\n"); 130 if (description != null) { 131 sb.append("<documentation>\n").append(description).append("\n</documentation>\n\n"); 132 } 133 for (String xt : extensions) { 134 sb.append(xt).append("\n\n"); 135 } 136 sb.append("</component>\n"); 137 return sb.toString(); 138 } 139 140 @Override 141 public InputStream getStream() { 142 return new ByteArrayInputStream(getContent().getBytes()); 143 } 144 145}