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  package org.apache.maven.index.treeview;
20  
21  import org.apache.maven.index.ArtifactInfo;
22  import org.apache.maven.index.treeview.TreeNode.Type;
23  
24  /**
25   * A default implementation of TreeNodeFactory, that is fairly simple to extend.
26   *
27   * @author Tamas Cservenak
28   */
29  public class DefaultTreeNodeFactory implements TreeNodeFactory {
30      private final String repositoryId;
31  
32      public DefaultTreeNodeFactory(String id) {
33          this.repositoryId = id;
34      }
35  
36      public String getRepositoryId() {
37          return repositoryId;
38      }
39  
40      public TreeNode createGNode(IndexTreeView tview, TreeViewRequest req, String path, String groupName) {
41          TreeNode result = createNode(tview, req, path, false, groupName, Type.G);
42  
43          return decorateGNode(tview, req, path, groupName, result);
44      }
45  
46      protected TreeNode decorateGNode(
47              IndexTreeView tview, TreeViewRequest req, String path, String groupName, TreeNode node) {
48          return node;
49      }
50  
51      public TreeNode createANode(IndexTreeView tview, TreeViewRequest req, ArtifactInfo ai, String path) {
52          TreeNode result = createNode(tview, req, path, false, ai.getArtifactId(), Type.A);
53  
54          result.setGroupId(ai.getGroupId());
55  
56          result.setArtifactId(ai.getArtifactId());
57  
58          return decorateANode(tview, req, ai, path, result);
59      }
60  
61      protected TreeNode decorateANode(
62              IndexTreeView tview, TreeViewRequest req, ArtifactInfo ai, String path, TreeNode node) {
63          return node;
64      }
65  
66      public TreeNode createVNode(IndexTreeView tview, TreeViewRequest req, ArtifactInfo ai, String path) {
67          TreeNode result = createNode(tview, req, path, false, ai.getVersion(), Type.V);
68  
69          result.setGroupId(ai.getGroupId());
70  
71          result.setArtifactId(ai.getArtifactId());
72  
73          result.setVersion(ai.getVersion());
74  
75          return decorateVNode(tview, req, ai, path, result);
76      }
77  
78      protected TreeNode decorateVNode(
79              IndexTreeView tview, TreeViewRequest req, ArtifactInfo ai, String path, TreeNode node) {
80          return node;
81      }
82  
83      public TreeNode createArtifactNode(IndexTreeView tview, TreeViewRequest req, ArtifactInfo ai, String path) {
84          StringBuilder sb = new StringBuilder(ai.getArtifactId()).append("-").append(ai.getVersion());
85  
86          if (ai.getClassifier() != null) {
87              sb.append("-").append(ai.getClassifier());
88          }
89  
90          sb.append(".").append(ai.getFileExtension() == null ? "jar" : ai.getFileExtension());
91  
92          TreeNode result = createNode(tview, req, path, true, sb.toString(), Type.artifact);
93  
94          result.setGroupId(ai.getGroupId());
95  
96          result.setArtifactId(ai.getArtifactId());
97  
98          result.setVersion(ai.getVersion());
99  
100         return decorateArtifactNode(tview, req, ai, path, result);
101     }
102 
103     protected TreeNode decorateArtifactNode(
104             IndexTreeView tview, TreeViewRequest req, ArtifactInfo ai, String path, TreeNode node) {
105         return node;
106     }
107 
108     protected TreeNode createNode(
109             IndexTreeView tview, TreeViewRequest req, String path, boolean leaf, String nodeName, Type type) {
110         TreeNode result = instantiateNode(tview, req, path, leaf, nodeName);
111 
112         result.setPath(path);
113 
114         result.setType(type);
115 
116         result.setLeaf(leaf);
117 
118         result.setNodeName(nodeName);
119 
120         result.setRepositoryId(getRepositoryId());
121 
122         return result;
123     }
124 
125     protected TreeNode instantiateNode(
126             IndexTreeView tview, TreeViewRequest req, String path, boolean leaf, String nodeName) {
127         return new DefaultTreeNode(tview, req);
128     }
129 }