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;
28 import org.apache.commons.lang.StringEscapeUtils;
29 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
30 import org.apache.maven.archiva.web.util.ContextUtils;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
37 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
39 * @plexus.component role="org.apache.maven.archiva.web.tags.CopyPasteSnippet"
41 public class CopyPasteSnippet
43 private Logger log = LoggerFactory.getLogger( CopyPasteSnippet.class );
45 public static final String PRE = "pre";
47 public static final String TOGGLE = "toggle";
49 public void write( String wrapper, Object o, PageContext pageContext )
52 StringBuffer prefix = new StringBuffer();
53 StringBuffer buf = new StringBuffer();
54 StringBuffer suffix = new StringBuffer();
58 buf.append( "Error generating snippet." );
59 log.error( "Unable to generate snippet for null object." );
61 else if ( o instanceof ManagedRepositoryConfiguration )
63 ManagedRepositoryConfiguration repo = (ManagedRepositoryConfiguration) o;
65 if ( TOGGLE.equals( wrapper ) )
67 prefix.append( "<a href=\"#\" onclick=\"Effect.toggle('repoPom" );
68 prefix.append( repo.getId() ).append( "','slide'); return false;\">Show POM Snippet</a><br/>" );
69 prefix.append( "<pre class=\"pom\" style=\"display: none;\" id=\"repoPom" ).append( repo.getId() );
70 prefix.append( "\"><code>" );
72 suffix.append( "</code></pre>" );
74 else if ( PRE.equals( wrapper ) )
76 prefix.append( "<pre>" );
77 suffix.append( "</pre>" );
80 createSnippet( buf, repo, pageContext );
84 buf.append( "Unable to generate snippet for object " ).append( o.getClass().getName() );
89 JspWriter out = pageContext.getOut();
91 out.write( prefix.toString() );
92 out.write( StringEscapeUtils.escapeXml( buf.toString() ) );
93 out.write( suffix.toString() );
97 catch ( IOException e )
99 throw new JspException( "Unable to write snippet to output: " + e.getMessage(), e );
103 private void createSnippet( StringBuffer snippet, ManagedRepositoryConfiguration repo, PageContext pageContext )
105 snippet.append( "<project>\n" );
106 snippet.append( " ...\n" );
107 snippet.append( " <distributionManagement>\n" );
109 String distRepoName = "repository";
110 if ( repo.isSnapshots() )
112 distRepoName = "snapshotRepository";
115 snippet.append( " <" ).append( distRepoName ).append( ">\n" );
116 snippet.append( " <id>" ).append( repo.getId() ).append( "</id>\n" );
117 snippet.append( " <url>dav:" ).append( ContextUtils.getBaseURL( pageContext, "repository" ) );
118 snippet.append( "/" ).append( repo.getId() ).append( "/" ).append( "</url>\n" );
120 if ( !"default".equals( repo.getLayout() ) )
122 snippet.append( " <layout>" ).append( repo.getLayout() ).append( "</layout>" );
125 snippet.append( " </" ).append( distRepoName ).append( ">\n" );
126 snippet.append( " </distributionManagement>\n" );
127 snippet.append( "\n" );
129 snippet.append( " <repositories>\n" );
130 snippet.append( " <repository>\n" );
131 snippet.append( " <id>" ).append( repo.getId() ).append( "</id>\n" );
132 snippet.append( " <name>" ).append( repo.getName() ).append( "</name>\n" );
134 snippet.append( " <url>" );
135 snippet.append( ContextUtils.getBaseURL( pageContext, "repository" ) );
136 snippet.append( "/" ).append( repo.getId() ).append( "/" );
138 snippet.append( "</url>\n" );
140 if ( !"default".equals( repo.getLayout() ) )
142 snippet.append( " <layout>" ).append( repo.getLayout() ).append( "</layout>\n" );
145 snippet.append( " <releases>\n" );
146 snippet.append( " <enabled>" ).append( Boolean.valueOf( repo.isReleases() ) ).append( "</enabled>\n" );
147 snippet.append( " </releases>\n" );
148 snippet.append( " <snapshots>\n" );
149 snippet.append( " <enabled>" ).append( Boolean.valueOf( repo.isSnapshots() ) ).append( "</enabled>\n" );
150 snippet.append( " </snapshots>\n" );
151 snippet.append( " </repository>\n" );
152 snippet.append( " </repositories>\n" );
154 snippet.append( " ...\n" );
155 snippet.append( "</project>\n" );