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.repository.managed.ManagedRepository;
23 import org.apache.archiva.web.util.ContextUtils;
24 import org.apache.commons.lang.StringEscapeUtils;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
28 import javax.servlet.jsp.JspException;
29 import javax.servlet.jsp.JspWriter;
30 import javax.servlet.jsp.PageContext;
31 import javax.servlet.jsp.tagext.TagSupport;
32 import java.io.IOException;
39 public class CopyPasteSnippetTag
42 private Logger log = LoggerFactory.getLogger( CopyPasteSnippetTag.class );
44 private Object object;
46 private String wrapper = PRE;
48 public static final String PRE = "pre";
50 public static final String TOGGLE = "toggle";
63 StringBuilder prefix = new StringBuilder();
64 StringBuilder buf = new StringBuilder();
65 StringBuilder suffix = new StringBuilder();
69 buf.append( "Error generating snippet." );
70 log.error( "Unable to generate snippet for null object." );
72 else if ( object instanceof ManagedRepository )
74 ManagedRepository repo = (ManagedRepository) object;
76 if ( TOGGLE.equals( wrapper ) )
78 prefix.append( "<a href=\"#\" class=\"expand\">Show POM Snippet</a><br/>" );
79 prefix.append( "<pre class=\"pom\"><code>" );
81 suffix.append( "</code></pre>" );
83 else if ( PRE.equals( wrapper ) )
85 prefix.append( "<pre>" );
86 suffix.append( "</pre>" );
89 createSnippet( buf, repo, pageContext );
93 buf.append( "Unable to generate snippet for object " ).append( object.getClass().getName() );
98 JspWriter out = pageContext.getOut();
100 out.write( prefix.toString() );
101 out.write( StringEscapeUtils.escapeXml( buf.toString() ) );
102 out.write( suffix.toString() );
106 catch ( IOException e )
108 throw new JspException( "Unable to write snippet to output: " + e.getMessage(), e );
111 return super.doEndTag();
114 public void setObject( Object object )
116 this.object = object;
119 public void setWrapper( String wrapper )
121 this.wrapper = wrapper;
124 private void createSnippet( StringBuilder snippet, ManagedRepository repo, PageContext pageContext )
126 snippet.append( "<project>\n" );
127 snippet.append( " ...\n" );
128 snippet.append( " <distributionManagement>\n" );
130 String distRepoName = "repository";
131 if ( repo.isSnapshots() )
133 distRepoName = "snapshotRepository";
136 snippet.append( " <" ).append( distRepoName ).append( ">\n" );
137 snippet.append( " <id>" ).append( repo.getId() ).append( "</id>\n" );
138 snippet.append( " <url>" ).append( ContextUtils.getBaseURL( pageContext, "repository" ) );
139 snippet.append( "/" ).append( repo.getId() ).append( "/" ).append( "</url>\n" );
141 if ( !"default".equals( repo.getLayout() ) )
143 snippet.append( " <layout>" ).append( repo.getLayout() ).append( "</layout>" );
146 snippet.append( " </" ).append( distRepoName ).append( ">\n" );
147 snippet.append( " </distributionManagement>\n" );
148 snippet.append( "\n" );
150 snippet.append( " <repositories>\n" );
151 snippet.append( " <repository>\n" );
152 snippet.append( " <id>" ).append( repo.getId() ).append( "</id>\n" );
153 snippet.append( " <name>" ).append( repo.getName() ).append( "</name>\n" );
155 snippet.append( " <url>" );
156 snippet.append( ContextUtils.getBaseURL( pageContext, "repository" ) );
157 snippet.append( "/" ).append( repo.getId() ).append( "/" );
159 snippet.append( "</url>\n" );
161 if ( !"default".equals( repo.getLayout() ) )
163 snippet.append( " <layout>" ).append( repo.getLayout() ).append( "</layout>\n" );
166 snippet.append( " <releases>\n" );
167 snippet.append( " <enabled>" ).append( Boolean.valueOf( repo.isReleases() ) ).append( "</enabled>\n" );
168 snippet.append( " </releases>\n" );
169 snippet.append( " <snapshots>\n" );
170 snippet.append( " <enabled>" ).append( Boolean.valueOf( repo.isSnapshots() ) ).append( "</enabled>\n" );
171 snippet.append( " </snapshots>\n" );
172 snippet.append( " </repository>\n" );
173 snippet.append( " </repositories>\n" );
175 snippet.append( " ...\n" );
176 snippet.append( "</project>\n" );