1 package org.apache.archiva.web.tags;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import org.apache.archiva.admin.model.beans.ManagedRepository;
23 import org.apache.archiva.web.util.ContextUtils;
24 import org.apache.commons.lang.StringEscapeUtils;
25 import org.apache.struts2.views.annotations.StrutsTag;
26 import org.apache.struts2.views.annotations.StrutsTagAttribute;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
30 import javax.servlet.jsp.JspException;
31 import javax.servlet.jsp.JspWriter;
32 import javax.servlet.jsp.PageContext;
33 import javax.servlet.jsp.tagext.TagSupport;
34 import java.io.IOException;
41 @StrutsTag(name = "copy-paste-snippet", tldBodyContent = "empty", tldTagClass = "org.apache.archiva.web.tags.CopyPasteSnippetTag", description = "Render a copy paste snippet for the provided object")
42 public class CopyPasteSnippetTag
45 private Logger log = LoggerFactory.getLogger( CopyPasteSnippetTag.class );
47 private Object object;
49 private String wrapper = PRE;
51 public static final String PRE = "pre";
53 public static final String TOGGLE = "toggle";
66 StringBuilder prefix = new StringBuilder();
67 StringBuilder buf = new StringBuilder();
68 StringBuilder suffix = new StringBuilder();
72 buf.append( "Error generating snippet." );
73 log.error( "Unable to generate snippet for null object." );
75 else if ( object instanceof ManagedRepository )
77 ManagedRepository repo = (ManagedRepository) object;
79 if ( TOGGLE.equals( wrapper ) )
81 prefix.append( "<a href=\"#\" class=\"expand\">Show POM Snippet</a><br/>" );
82 prefix.append( "<pre class=\"pom\"><code>" );
84 suffix.append( "</code></pre>" );
86 else if ( PRE.equals( wrapper ) )
88 prefix.append( "<pre>" );
89 suffix.append( "</pre>" );
92 createSnippet( buf, repo, pageContext );
96 buf.append( "Unable to generate snippet for object " ).append( object.getClass().getName() );
101 JspWriter out = pageContext.getOut();
103 out.write( prefix.toString() );
104 out.write( StringEscapeUtils.escapeXml( buf.toString() ) );
105 out.write( suffix.toString() );
109 catch ( IOException e )
111 throw new JspException( "Unable to write snippet to output: " + e.getMessage(), e );
114 return super.doEndTag();
117 @StrutsTagAttribute(description = "The Object to Render", type = "String", defaultValue = "", required = true, rtexprvalue = true)
118 public void setObject( Object object )
120 this.object = object;
123 @StrutsTagAttribute(description = "The wrapper type to use, can be 'pre' or 'toggle'", type = "String", defaultValue = "", required = false, rtexprvalue = true)
124 public void setWrapper( String wrapper )
126 this.wrapper = wrapper;
129 private void createSnippet( StringBuilder snippet, ManagedRepository repo, PageContext pageContext )
131 snippet.append( "<project>\n" );
132 snippet.append( " ...\n" );
133 snippet.append( " <distributionManagement>\n" );
135 String distRepoName = "repository";
136 if ( repo.isSnapshots() )
138 distRepoName = "snapshotRepository";
141 snippet.append( " <" ).append( distRepoName ).append( ">\n" );
142 snippet.append( " <id>" ).append( repo.getId() ).append( "</id>\n" );
143 snippet.append( " <url>" ).append( ContextUtils.getBaseURL( pageContext, "repository" ) );
144 snippet.append( "/" ).append( repo.getId() ).append( "/" ).append( "</url>\n" );
146 if ( !"default".equals( repo.getLayout() ) )
148 snippet.append( " <layout>" ).append( repo.getLayout() ).append( "</layout>" );
151 snippet.append( " </" ).append( distRepoName ).append( ">\n" );
152 snippet.append( " </distributionManagement>\n" );
153 snippet.append( "\n" );
155 snippet.append( " <repositories>\n" );
156 snippet.append( " <repository>\n" );
157 snippet.append( " <id>" ).append( repo.getId() ).append( "</id>\n" );
158 snippet.append( " <name>" ).append( repo.getName() ).append( "</name>\n" );
160 snippet.append( " <url>" );
161 snippet.append( ContextUtils.getBaseURL( pageContext, "repository" ) );
162 snippet.append( "/" ).append( repo.getId() ).append( "/" );
164 snippet.append( "</url>\n" );
166 if ( !"default".equals( repo.getLayout() ) )
168 snippet.append( " <layout>" ).append( repo.getLayout() ).append( "</layout>\n" );
171 snippet.append( " <releases>\n" );
172 snippet.append( " <enabled>" ).append( Boolean.valueOf( repo.isReleases() ) ).append( "</enabled>\n" );
173 snippet.append( " </releases>\n" );
174 snippet.append( " <snapshots>\n" );
175 snippet.append( " <enabled>" ).append( Boolean.valueOf( repo.isSnapshots() ) ).append( "</enabled>\n" );
176 snippet.append( " </snapshots>\n" );
177 snippet.append( " </repository>\n" );
178 snippet.append( " </repositories>\n" );
179 snippet.append( " <pluginRepositories>\n" );
180 snippet.append( " <pluginRepository>\n" );
181 snippet.append( " <id>" ).append( repo.getId() ).append( "</id>\n" );
182 snippet.append( " <name>" ).append( repo.getName() ).append( "</name>\n" );
184 snippet.append( " <url>" );
185 snippet.append( ContextUtils.getBaseURL( pageContext, "repository" ) );
186 snippet.append( "/" ).append( repo.getId() ).append( "/" );
188 snippet.append( "</url>\n" );
190 if ( !"default".equals( repo.getLayout() ) )
192 snippet.append( " <layout>" ).append( repo.getLayout() ).append( "</layout>\n" );
195 snippet.append( " <releases>\n" );
196 snippet.append( " <enabled>" ).append( Boolean.valueOf( repo.isReleases() ) ).append( "</enabled>\n" );
197 snippet.append( " </releases>\n" );
198 snippet.append( " <snapshots>\n" );
199 snippet.append( " <enabled>" ).append( Boolean.valueOf( repo.isSnapshots() ) ).append( "</enabled>\n" );
200 snippet.append( " </snapshots>\n" );
201 snippet.append( " </pluginRepository>\n" );
202 snippet.append( " </pluginRepositories>\n" );
204 snippet.append( " ...\n" );
205 snippet.append( "</project>\n" );