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.archiva.admin.repository.managed.ManagedRepository;
30 import org.apache.commons.lang.StringEscapeUtils;
31 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
32 import org.apache.archiva.web.util.ContextUtils;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
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 StringBuilder prefix = new StringBuilder();
66 StringBuilder buf = new StringBuilder();
67 StringBuilder suffix = new StringBuilder();
71 buf.append( "Error generating snippet." );
72 log.error( "Unable to generate snippet for null object." );
74 else if ( object instanceof ManagedRepository )
76 ManagedRepository repo = (ManagedRepository) object;
78 if ( TOGGLE.equals( wrapper ) )
80 prefix.append( "<a href=\"#\" class=\"expand\">Show POM Snippet</a><br/>" );
81 prefix.append( "<pre class=\"pom\"><code>" );
83 suffix.append( "</code></pre>" );
85 else if ( PRE.equals( wrapper ) )
87 prefix.append( "<pre>" );
88 suffix.append( "</pre>" );
91 createSnippet( buf, repo, pageContext );
95 buf.append( "Unable to generate snippet for object " ).append( object.getClass().getName() );
100 JspWriter out = pageContext.getOut();
102 out.write( prefix.toString() );
103 out.write( StringEscapeUtils.escapeXml( buf.toString() ) );
104 out.write( suffix.toString() );
108 catch ( IOException e )
110 throw new JspException( "Unable to write snippet to output: " + e.getMessage(), e );
113 return super.doEndTag();
116 public void setObject( Object object )
118 this.object = object;
121 public void setWrapper( String wrapper )
123 this.wrapper = wrapper;
126 private void createSnippet( StringBuilder snippet, ManagedRepository repo, PageContext pageContext )
128 snippet.append( "<project>\n" );
129 snippet.append( " ...\n" );
130 snippet.append( " <distributionManagement>\n" );
132 String distRepoName = "repository";
133 if ( repo.isSnapshots() )
135 distRepoName = "snapshotRepository";
138 snippet.append( " <" ).append( distRepoName ).append( ">\n" );
139 snippet.append( " <id>" ).append( repo.getId() ).append( "</id>\n" );
140 snippet.append( " <url>" ).append( ContextUtils.getBaseURL( pageContext, "repository" ) );
141 snippet.append( "/" ).append( repo.getId() ).append( "/" ).append( "</url>\n" );
143 if ( !"default".equals( repo.getLayout() ) )
145 snippet.append( " <layout>" ).append( repo.getLayout() ).append( "</layout>" );
148 snippet.append( " </" ).append( distRepoName ).append( ">\n" );
149 snippet.append( " </distributionManagement>\n" );
150 snippet.append( "\n" );
152 snippet.append( " <repositories>\n" );
153 snippet.append( " <repository>\n" );
154 snippet.append( " <id>" ).append( repo.getId() ).append( "</id>\n" );
155 snippet.append( " <name>" ).append( repo.getName() ).append( "</name>\n" );
157 snippet.append( " <url>" );
158 snippet.append( ContextUtils.getBaseURL( pageContext, "repository" ) );
159 snippet.append( "/" ).append( repo.getId() ).append( "/" );
161 snippet.append( "</url>\n" );
163 if ( !"default".equals( repo.getLayout() ) )
165 snippet.append( " <layout>" ).append( repo.getLayout() ).append( "</layout>\n" );
168 snippet.append( " <releases>\n" );
169 snippet.append( " <enabled>" ).append( Boolean.valueOf( repo.isReleases() ) ).append( "</enabled>\n" );
170 snippet.append( " </releases>\n" );
171 snippet.append( " <snapshots>\n" );
172 snippet.append( " <enabled>" ).append( Boolean.valueOf( repo.isSnapshots() ) ).append( "</enabled>\n" );
173 snippet.append( " </snapshots>\n" );
174 snippet.append( " </repository>\n" );
175 snippet.append( " </repositories>\n" );
177 snippet.append( " ...\n" );
178 snippet.append( "</project>\n" );