001/* 002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the Eclipse Public License v1.0 006 * which accompanies this distribution, and is available at 007 * http://www.eclipse.org/legal/epl-v10.html 008 * 009 * Contributors: 010 * bstefanescu 011 */ 012package org.nuxeo.runtime.model.persistence; 013 014import java.io.ByteArrayInputStream; 015import java.io.IOException; 016import java.io.InputStream; 017import java.net.URL; 018import java.util.ArrayList; 019import java.util.Arrays; 020import java.util.List; 021 022import javax.xml.parsers.DocumentBuilder; 023import javax.xml.parsers.DocumentBuilderFactory; 024import javax.xml.parsers.ParserConfigurationException; 025 026import org.nuxeo.common.xmap.DOMSerializer; 027import org.nuxeo.common.xmap.XMap; 028import org.w3c.dom.Document; 029import org.w3c.dom.Element; 030 031/** 032 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> 033 */ 034public class ContributionBuilder extends AbstractContribution { 035 036 protected final DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance(); 037 038 protected final List<String> extensions; 039 040 protected String bundle; 041 042 public ContributionBuilder(String name) { 043 super(name); 044 this.extensions = new ArrayList<String>(); 045 } 046 047 @Override 048 public URL asURL() { 049 return null; 050 } 051 052 public void setBundle(String bundle) { 053 this.bundle = bundle; 054 } 055 056 @Override 057 public String getDescription() { 058 return description; 059 } 060 061 @Override 062 public boolean isDisabled() { 063 return disabled; 064 } 065 066 @Override 067 public void setDisabled(boolean isDisabled) { 068 this.disabled = isDisabled; 069 } 070 071 public void addXmlExtension(String target, String point, String content) { 072 StringBuilder buf = new StringBuilder(1024); 073 buf.append("<extension target=\"" + target + "\" point=\"" + point + "\">\n").append(content).append( 074 "\n</extension>"); 075 extensions.add(buf.toString()); 076 } 077 078 public void addExtension(String target, String point, Object... contribs) { 079 if (contribs != null && contribs.length > 0) { 080 addExtension(target, point, Arrays.asList(contribs)); 081 } 082 } 083 084 public void addExtension(String target, String point, List<Object> contribs) { 085 DocumentBuilder docBuilder; 086 try { 087 docBuilder = dbfac.newDocumentBuilder(); 088 } catch (ParserConfigurationException e) { 089 throw new RuntimeException(e); 090 } 091 Document doc = docBuilder.newDocument(); 092 // create root element 093 Element root = doc.createElement("extension"); 094 root.setAttribute("target", target); 095 root.setAttribute("point", point); 096 doc.appendChild(root); 097 098 XMap xmap = new XMap(); 099 for (Object contrib : contribs) { 100 xmap.register(contrib.getClass()); 101 xmap.toXML(contrib, root); 102 } 103 try { 104 extensions.add(DOMSerializer.toStringOmitXml(root)); 105 } catch (IOException e) { 106 throw new RuntimeException(e); 107 } 108 } 109 110 @Override 111 public String getContent() { 112 StringBuilder buf = new StringBuilder(1024 * 32); 113 buf.append("<component name=\"").append(ContributionPersistenceComponent.getComponentName(name)).append("\" "); 114 if (bundle != null) { 115 buf.append("bundle=\"").append(bundle).append("\" "); 116 } 117 buf.append(">\n\n"); 118 if (description != null) { 119 buf.append("<documentation>\n").append(description).append("\n</documentation>\n\n"); 120 } 121 for (String xt : extensions) { 122 buf.append(xt).append("\n\n"); 123 } 124 buf.append("</component>\n"); 125 return buf.toString(); 126 } 127 128 @Override 129 public InputStream getStream() { 130 return new ByteArrayInputStream(getContent().getBytes()); 131 } 132 133}