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 java.io.IOException;
24 import javax.servlet.jsp.JspException;
25 import javax.servlet.jsp.JspWriter;
26 import javax.servlet.jsp.PageContext;
27 import javax.servlet.jsp.tagext.TagSupport;
29 import org.apache.commons.lang.StringEscapeUtils;
30 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
31 import org.apache.archiva.web.util.ContextUtils;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
40 public class CopyPasteSnippetTag
43 private Logger log = LoggerFactory.getLogger( CopyPasteSnippetTag.class );
45 private Object object;
47 private String wrapper = PRE;
49 public static final String PRE = "pre";
51 public static final String TOGGLE = "toggle";
64 StringBuilder prefix = new StringBuilder();
65 StringBuilder buf = new StringBuilder();
66 StringBuilder suffix = new StringBuilder();
70 buf.append( "Error generating snippet." );
71 log.error( "Unable to generate snippet for null object." );
73 else if ( object instanceof ManagedRepositoryConfiguration )
75 ManagedRepositoryConfiguration repo = (ManagedRepositoryConfiguration) object;
77 if ( TOGGLE.equals( wrapper ) )
79 prefix.append( "<a href=\"#\" class=\"expand\">Show POM Snippet</a><br/>" );
80 prefix.append( "<pre class=\"pom\"><code>" );
82 suffix.append( "</code></pre>" );
84 else if ( PRE.equals( wrapper ) )
86 prefix.append( "<pre>" );
87 suffix.append( "</pre>" );
90 createSnippet( buf, repo, pageContext );
94 buf.append( "Unable to generate snippet for object " ).append( object.getClass().getName() );
99 JspWriter out = pageContext.getOut();
101 out.write( prefix.toString() );
102 out.write( StringEscapeUtils.escapeXml( buf.toString() ) );
103 out.write( suffix.toString() );
107 catch ( IOException e )
109 throw new JspException( "Unable to write snippet to output: " + e.getMessage(), e );
112 return super.doEndTag();
115 public void setObject( Object object )
117 this.object = object;
120 public void setWrapper( String wrapper )
122 this.wrapper = wrapper;
125 private void createSnippet( StringBuilder snippet, ManagedRepositoryConfiguration repo, PageContext pageContext )
127 snippet.append( "<project>\n" );
128 snippet.append( " ...\n" );
129 snippet.append( " <distributionManagement>\n" );
131 String distRepoName = "repository";
132 if ( repo.isSnapshots() )
134 distRepoName = "snapshotRepository";
137 snippet.append( " <" ).append( distRepoName ).append( ">\n" );
138 snippet.append( " <id>" ).append( repo.getId() ).append( "</id>\n" );
139 snippet.append( " <url>" ).append( ContextUtils.getBaseURL( pageContext, "repository" ) );
140 snippet.append( "/" ).append( repo.getId() ).append( "/" ).append( "</url>\n" );
142 if ( !"default".equals( repo.getLayout() ) )
144 snippet.append( " <layout>" ).append( repo.getLayout() ).append( "</layout>" );
147 snippet.append( " </" ).append( distRepoName ).append( ">\n" );
148 snippet.append( " </distributionManagement>\n" );
149 snippet.append( "\n" );
151 snippet.append( " <repositories>\n" );
152 snippet.append( " <repository>\n" );
153 snippet.append( " <id>" ).append( repo.getId() ).append( "</id>\n" );
154 snippet.append( " <name>" ).append( repo.getName() ).append( "</name>\n" );
156 snippet.append( " <url>" );
157 snippet.append( ContextUtils.getBaseURL( pageContext, "repository" ) );
158 snippet.append( "/" ).append( repo.getId() ).append( "/" );
160 snippet.append( "</url>\n" );
162 if ( !"default".equals( repo.getLayout() ) )
164 snippet.append( " <layout>" ).append( repo.getLayout() ).append( "</layout>\n" );
167 snippet.append( " <releases>\n" );
168 snippet.append( " <enabled>" ).append( Boolean.valueOf( repo.isReleases() ) ).append( "</enabled>\n" );
169 snippet.append( " </releases>\n" );
170 snippet.append( " <snapshots>\n" );
171 snippet.append( " <enabled>" ).append( Boolean.valueOf( repo.isSnapshots() ) ).append( "</enabled>\n" );
172 snippet.append( " </snapshots>\n" );
173 snippet.append( " </repository>\n" );
174 snippet.append( " </repositories>\n" );
176 snippet.append( " ...\n" );
177 snippet.append( "</project>\n" );