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<String>();
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 buf = new StringBuilder(1024);
080        buf.append("<extension target=\"" + target + "\" point=\"" + point + "\">\n").append(content).append(
081                "\n</extension>");
082        extensions.add(buf.toString());
083    }
084
085    public void addExtension(String target, String point, Object... contribs) {
086        if (contribs != null && contribs.length > 0) {
087            addExtension(target, point, Arrays.asList(contribs));
088        }
089    }
090
091    public void addExtension(String target, String point, List<Object> contribs) {
092        DocumentBuilder docBuilder;
093        try {
094            docBuilder = dbfac.newDocumentBuilder();
095        } catch (ParserConfigurationException e) {
096            throw new RuntimeException(e);
097        }
098        Document doc = docBuilder.newDocument();
099        // create root element
100        Element root = doc.createElement("extension");
101        root.setAttribute("target", target);
102        root.setAttribute("point", point);
103        doc.appendChild(root);
104
105        XMap xmap = new XMap();
106        for (Object contrib : contribs) {
107            xmap.register(contrib.getClass());
108            xmap.toXML(contrib, root);
109        }
110        try {
111            extensions.add(DOMSerializer.toStringOmitXml(root));
112        } catch (IOException e) {
113            throw new RuntimeException(e);
114        }
115    }
116
117    @Override
118    public String getContent() {
119        StringBuilder buf = new StringBuilder(1024 * 32);
120        buf.append("<component name=\"").append(ContributionPersistenceComponent.getComponentName(name)).append("\" ");
121        if (bundle != null) {
122            buf.append("bundle=\"").append(bundle).append("\" ");
123        }
124        buf.append(">\n\n");
125        if (description != null) {
126            buf.append("<documentation>\n").append(description).append("\n</documentation>\n\n");
127        }
128        for (String xt : extensions) {
129            buf.append(xt).append("\n\n");
130        }
131        buf.append("</component>\n");
132        return buf.toString();
133    }
134
135    @Override
136    public InputStream getStream() {
137        return new ByteArrayInputStream(getContent().getBytes());
138    }
139
140}