View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.maven.plugin.eclipse;
21  
22  import org.codehaus.plexus.util.xml.XMLWriter;
23  import org.codehaus.plexus.util.xml.Xpp3Dom;
24  
25  /**
26   * Represents a LinkedResources section in the <code>.project</code> file.
27   * 
28   * @author <a href="mailto:ashoknanw@gmail.com">Ashokkumar Sankaran</a>
29   */
30  public class LinkedResource
31  {
32      /** Resource name */
33      private String name;
34  
35      /** Type */
36      private String type;
37  
38      /** Resource location */
39      private String location;
40  
41      public String getName()
42      {
43          return name;
44      }
45  
46      public void setName( String name )
47      {
48          this.name = name;
49      }
50  
51      public String getType()
52      {
53          return type;
54      }
55  
56      public void setType( String type )
57      {
58          this.type = type;
59      }
60  
61      public String getLocation()
62      {
63          return location;
64      }
65  
66      public void setLocation( String location )
67      {
68          this.location = location;
69      }
70  
71      /**
72       * Default constructor
73       */
74      public LinkedResource()
75      {
76          super();
77      }
78  
79      /**
80       * Creates a LinkedResource from a DOM subtree
81       * <p>
82       * The subtree must represent a &lt;linkedResources&gt; section from an Eclipse .project file
83       * 
84       * @param node DOM node
85       */
86      public LinkedResource( Xpp3Dom node )
87      {
88          Xpp3Dom nameNode = node.getChild( "name" );
89  
90          if ( nameNode == null )
91          {
92              throw new IllegalArgumentException( "No name node." );
93          }
94  
95          name = nameNode.getValue();
96  
97          Xpp3Dom typeNode = node.getChild( "type" );
98  
99          if ( typeNode == null )
100         {
101             throw new IllegalArgumentException( "No type node." );
102         }
103 
104         type = typeNode.getValue();
105 
106         Xpp3Dom locationNode = node.getChild( "location" );
107 
108         if ( locationNode == null )
109         {
110             throw new IllegalArgumentException( "No location node." );
111         }
112 
113         location = locationNode.getValue();
114     }
115 
116     public void print( XMLWriter writer )
117     {
118         writer.startElement( "link" );
119 
120         writer.startElement( "name" );
121         writer.writeText( name );
122         writer.endElement(); // name
123 
124         writer.startElement( "type" );
125         writer.writeText( type );
126         writer.endElement(); // type
127 
128         writer.startElement( "location" );
129         writer.writeText( location );
130         writer.endElement(); // location
131         writer.endElement();// link
132     }
133 
134     public boolean equals( Object obj )
135     {
136         if ( obj instanceof LinkedResource )
137         {
138             LinkedResource b = (LinkedResource) obj;
139 
140             return name.equals( b.name ) && ( type == null ? b.type == null : type.equals( b.type ) )
141                 && ( location == null ? b.location == null : location.equals( b.location ) );
142         }
143         else
144         {
145             return false;
146         }
147     }
148 
149     public int hashCode()
150     {
151         return name.hashCode() + ( type == null ? 0 : 13 * type.hashCode() )
152             + ( location == null ? 0 : 17 * location.hashCode() );
153     }
154 }