View Javadoc

1   package net.sf.jpkgmk.prototype;
2   
3   import net.sf.jpkgmk.pkgmap.PkgMapEntryType;
4   import net.sf.jpkgmk.prototype.PrototypeEntryBlockDevice.PrototypeEntryBlockDeviceParser;
5   import net.sf.jpkgmk.prototype.PrototypeEntryCharacterDevice.PrototypeEntryCharacterDeviceParser;
6   import net.sf.jpkgmk.prototype.PrototypeEntryCommand.PrototypeEntryCommandParser;
7   import net.sf.jpkgmk.prototype.PrototypeEntryComment.PrototypeEntryCommentParser;
8   import net.sf.jpkgmk.prototype.PrototypeEntryDirectory.PrototypeEntryDirectoryParser;
9   import net.sf.jpkgmk.prototype.PrototypeEntryDirectoryExclusive.PrototypeEntryDirectoryExclusiveParser;
10  import net.sf.jpkgmk.prototype.PrototypeEntryFile.PrototypeEntryFileParser;
11  import net.sf.jpkgmk.prototype.PrototypeEntryFileEditable.PrototypeEntryFileEditableParser;
12  import net.sf.jpkgmk.prototype.PrototypeEntryFileVolatile.PrototypeEntryFileVolatileParser;
13  import net.sf.jpkgmk.prototype.PrototypeEntryInfo.PrototypeEntryInfoParser;
14  import net.sf.jpkgmk.prototype.PrototypeEntryLink.PrototypeEntryLinkParser;
15  import net.sf.jpkgmk.prototype.PrototypeEntryPipe.PrototypeEntryPipeParser;
16  import net.sf.jpkgmk.prototype.PrototypeEntrySymbolicLink.PrototypeEntrySymbolicLinkParser;
17  import net.sf.jpkgmk.util.StringUtil;
18  
19  
20  /**
21   * Typesafe enumeration for the filetypes that can be used for prototype lines
22   * @author gommma (gommma AT users.sourceforge.net)
23   * @author Last changed by: $Author: gommma $
24   * @version $Revision: 2 $ $Date: 2008-08-20 21:14:19 +0200 (Mi, 20 Aug 2008) $
25   * @since 1.0
26   */
27  public class PrototypeEntryType
28  {
29  	public static final PrototypeEntryType COMMAND = new PrototypeEntryType("!", null, new PrototypeEntryCommandParser(),
30  			"COMMAND - An exclamation point (!) at the beginning of a line indicates that the line contains a command. " +
31  			"These commands are used to incorporate files in other directories, to locate objects on a host machine, and to set permanent defaults.") {
32  	    @Override
33  		public boolean matches(String key) {
34  			// A command can start with exclamationmark followed by the command directly without whitespace, e.g. "!PROJDIR=/root/tut/gut"
35  			if(key.startsWith(this.getKey())) {
36  				return true;
37  			}
38  			return false;
39  		}
40  	};
41  	
42  	public static final PrototypeEntryType COMMENT = new PrototypeEntryType("#", PkgMapEntryType.COMMENT, new PrototypeEntryCommentParser(),
43  			"COMMENT - Comment lines begin with a '#' and are ignored.") {
44  	    @Override
45  		public boolean matches(String key) {
46  			// A comment can start with '#' followed by the comment directly without whitespace, e.g. "#My beautiful comment"
47  			if(key.startsWith(this.getKey())) {
48  				return true;
49  			}
50  			return false;
51  		}
52  	};
53  	
54  	public static final PrototypeEntryType B = new PrototypeEntryType("b", PkgMapEntryType.BLOCK_SPECIAL, new PrototypeEntryBlockDeviceParser(), "block special device");
55  	public static final PrototypeEntryType C = new PrototypeEntryType("c", PkgMapEntryType.CHARACTER_SPECIAL, new PrototypeEntryCharacterDeviceParser(), "character special device");
56  	public static final PrototypeEntryType D = new PrototypeEntryType("d", PkgMapEntryType.DIRECTORY, new PrototypeEntryDirectoryParser(), "directory");
57  	public static final PrototypeEntryType E = new PrototypeEntryType("e", PkgMapEntryType.FILE_EDITABLE, new PrototypeEntryFileEditableParser(), "a file to be edited upon installation or removal (may be shared by several packages)");
58  	public static final PrototypeEntryType F = new PrototypeEntryType("f", PkgMapEntryType.FILE, new PrototypeEntryFileParser(), "a standard executable or data file");
59  	public static final PrototypeEntryType I = new PrototypeEntryType("i", PkgMapEntryType.FILE_INFO, new PrototypeEntryInfoParser(), "installation script or information file");
60  	public static final PrototypeEntryType L = new PrototypeEntryType("l", PkgMapEntryType.FILE_LINKED, new PrototypeEntryLinkParser(), "linked file");
61  	public static final PrototypeEntryType P = new PrototypeEntryType("p", PkgMapEntryType.PIPE, new PrototypeEntryPipeParser(), "named pipe");
62  	public static final PrototypeEntryType S = new PrototypeEntryType("s", PkgMapEntryType.SYMBOLIC_LINK, new PrototypeEntrySymbolicLinkParser(), "symbolic link");
63  	public static final PrototypeEntryType V = new PrototypeEntryType("v", PkgMapEntryType.FILE_VOLATILE, new PrototypeEntryFileVolatileParser(), "volatile file (one whose contents are expected to change, like a log file)");
64  	public static final PrototypeEntryType X = new PrototypeEntryType("x", PkgMapEntryType.DIRECTORY_EXCLUSIVE, new PrototypeEntryDirectoryExclusiveParser(), "an exclusive directory accessible only by this package");
65  	
66      public static PrototypeEntryType[] VALUES = new PrototypeEntryType[] {
67      	COMMAND,
68      	COMMENT,
69          B, 
70          C, 
71          D,
72          E,
73          F,
74          I,
75          L,
76          P,
77          S,
78          V,
79          X
80      };
81  
82  	private String key;
83  	private String description;
84  	/**
85  	 * The corresponding entry type for the pkgmap file.
86  	 */
87  	private PkgMapEntryType pkgMapEntryType;
88  	/**
89  	 * The parser that is used to create a PrototypeEntry object from a input line
90  	 */
91  	private PrototypeEntryParser parser;
92  	
93  	/**
94  	 * Private constructor
95  	 * @param key
96  	 * @param pkgMapEntryType
97  	 * @param parser
98  	 * @param description
99  	 */
100 	private PrototypeEntryType(String key, PkgMapEntryType pkgMapEntryType, PrototypeEntryParser parser, String description)
101 	{
102 		if(key==null) {
103 			throw new NullPointerException("The parameter 'id' must not be null");
104 		}
105 		this.key=key;
106 		this.pkgMapEntryType=pkgMapEntryType;
107 		this.parser=parser;
108 		this.description=description;
109 	}
110 	
111 	public PkgMapEntryType getPkgMapEntryType() 
112 	{
113 		return this.pkgMapEntryType;
114 	}
115 
116 	/**
117 	 * @return Returns the identifier for this entry type, for example a "!" for a command.
118 	 */
119 	public String getKey() {
120 		return key;
121 	}
122 
123 	public String getDescription() {
124 		return description;
125 	}
126 
127 	public PrototypeEntryParser getParser() 
128 	{
129 		return this.parser;
130 	}
131 
132 	public boolean matches(String key) {
133 		return this.key.equals(key);
134 	}
135 	
136     @Override
137 	public int hashCode() {
138 		final int prime = 31;
139 		int result = 1;
140 		result = prime * result + key.hashCode();
141 		return result;
142 	}
143 
144     @Override
145 	public boolean equals(Object obj) {
146 		if (this == obj)
147 			return true;
148 		if (obj == null)
149 			return false;
150 		if (this.getClass() != obj.getClass())
151 			return false;
152 		final PrototypeEntryType other = (PrototypeEntryType) obj;
153 		if (!key.equals(other.key))
154 			return false;
155 		return true;
156 	}
157 
158     @Override
159 	public String toString() {
160 		StringBuffer sb = new StringBuffer();
161 		sb.append(this.getClass().getName()).append("[");
162 		sb.append("key=").append(key);
163 		sb.append(",pkgMapEntryType=").append(pkgMapEntryType);
164 		sb.append(",parser=").append(parser);
165 		sb.append(",description=").append(StringUtil.truncateToMaxLength(this.description, 20));
166 		sb.append("]");
167 		return sb.toString();
168 	}
169 	
170     public static PrototypeEntryType findType(String key) {
171         for(int i=0; i<VALUES.length; i++) {
172             if(VALUES[i].matches(key))
173                 return VALUES[i];
174         }
175         // The type was not found. Return null.
176         return null;
177     }
178 
179 
180 }