1 package org.apache.maven.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.maven.archiva.web.util.ContextUtils;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
38 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
41 public class CopyPasteSnippetTag
44 private Logger log = LoggerFactory.getLogger( CopyPasteSnippetTag.class );
46 private Object object;
48 private String wrapper = PRE;
50 public static final String PRE = "pre";
52 public static final String TOGGLE = "toggle";
65 StringBuffer prefix = new StringBuffer();
66 StringBuffer buf = new StringBuffer();
67 StringBuffer suffix = new StringBuffer();
71 buf.append( "Error generating snippet." );
72 log.error( "Unable to generate snippet for null object." );
74 else if ( object instanceof ManagedRepositoryConfiguration )
76 ManagedRepositoryConfiguration repo = (ManagedRepositoryConfiguration) object;
78 if ( TOGGLE.equals( wrapper ) )
80 prefix.append( "<a href=\"#\" onclick=\"Effect.toggle('repoPom" );
81 prefix.append( repo.getId() ).append( "','slide'); return false;\">Show POM Snippet</a><br/>" );
82 prefix.append( "<pre class=\"pom\" style=\"display: none;\" id=\"repoPom" ).append( repo.getId() );
83 prefix.append( "\"><code>" );
85 suffix.append( "</code></pre>" );
87 else if ( PRE.equals( wrapper ) )
89 prefix.append( "<pre>" );
90 suffix.append( "</pre>" );
93 createSnippet( buf, repo, pageContext );
97 buf.append( "Unable to generate snippet for object " ).append( object.getClass().getName() );
102 JspWriter out = pageContext.getOut();
104 out.write( prefix.toString() );
105 out.write( StringEscapeUtils.escapeXml( buf.toString() ) );
106 out.write( suffix.toString() );
110 catch ( IOException e )
112 throw new JspException( "Unable to write snippet to output: " + e.getMessage(), e );
115 return super.doEndTag();
118 public void setObject( Object object )
120 this.object = object;
123 public void setWrapper( String wrapper )
125 this.wrapper = wrapper;
128 private void createSnippet( StringBuffer snippet, ManagedRepositoryConfiguration repo, PageContext pageContext )
130 snippet.append( "<project>\n" );
131 snippet.append( " ...\n" );
132 snippet.append( " <distributionManagement>\n" );
134 String distRepoName = "repository";
135 if ( repo.isSnapshots() )
137 distRepoName = "snapshotRepository";
140 snippet.append( " <" ).append( distRepoName ).append( ">\n" );
141 snippet.append( " <id>" ).append( repo.getId() ).append( "</id>\n" );
142 snippet.append( " <url>dav:" ).append( ContextUtils.getBaseURL( pageContext, "repository" ) );
143 snippet.append( "/" ).append( repo.getId() ).append( "/" ).append( "</url>\n" );
145 if ( !"default".equals( repo.getLayout() ) )
147 snippet.append( " <layout>" ).append( repo.getLayout() ).append( "</layout>" );
150 snippet.append( " </" ).append( distRepoName ).append( ">\n" );
151 snippet.append( " </distributionManagement>\n" );
152 snippet.append( "\n" );
154 snippet.append( " <repositories>\n" );
155 snippet.append( " <repository>\n" );
156 snippet.append( " <id>" ).append( repo.getId() ).append( "</id>\n" );
157 snippet.append( " <name>" ).append( repo.getName() ).append( "</name>\n" );
159 snippet.append( " <url>" );
160 snippet.append( ContextUtils.getBaseURL( pageContext, "repository" ) );
161 snippet.append( "/" ).append( repo.getId() ).append( "/" );
163 snippet.append( "</url>\n" );
165 if ( !"default".equals( repo.getLayout() ) )
167 snippet.append( " <layout>" ).append( repo.getLayout() ).append( "</layout>\n" );
170 snippet.append( " <releases>\n" );
171 snippet.append( " <enabled>" ).append( Boolean.valueOf( repo.isReleases() ) ).append( "</enabled>\n" );
172 snippet.append( " </releases>\n" );
173 snippet.append( " <snapshots>\n" );
174 snippet.append( " <enabled>" ).append( Boolean.valueOf( repo.isSnapshots() ) ).append( "</enabled>\n" );
175 snippet.append( " </snapshots>\n" );
176 snippet.append( " </repository>\n" );
177 snippet.append( " </repositories>\n" );
179 snippet.append( " ...\n" );
180 snippet.append( "</project>\n" );