package com.perforce.p4java_ext; import java.util.*; /** * A class that provides a bean-style interface over the client spec map returned by the * IOptionsServer map command. The main goal of this class is to deal with funky whitespace issues * in the output Map and generating a Spec for input. * <p/> * A key function is to provide a nice ID for each mapping it ends up seeing. This mostly appends * the "ViewX" label to the client name. This is the output of the getView() command. This should * basically enable simple "fetch" interface for most values. There should be very little * _encoding_ changes, just some tweaks like whitespace trimming etc. */ public class ClientSpec { private Map<String, Object> clientMap; public ClientSpec(Map<String, Object> clientMap) { this.clientMap = clientMap; } /** * The clientMap is the underlying map *from* the execMap command. */ public Map<String, Object> getClientMap() { return clientMap; } public void setClientMap(Map<String, Object> clientMap) { this.clientMap = clientMap; } /** * A lot of values come out of the API with space, etc, appended, so this just makes sure the * value is just a normal string, cleaning up most client calls. * * @param key The client property, e.g., "Client", "Owner" * @return String value of the property */ public String get(String key) { return ((String) clientMap.get(key)).trim(); } public void put(String key, String value) { clientMap.put(key, value); } public String getName() { return get("Name"); } public void setName(String n) { put("Name", n); } public String getRoot() { return get("Root"); } public void setRoot(String r) { put("Root", r); } public String getHost() { return get("Host"); } public void setHost(String h) { put("Host", h); } public String getOwner() { return get("Owner"); } public void setOwner(String s) { put("Owner", s); } public String getDescription() { return get("Description"); } public void setDescription(String d) { put("Description", d); } /** * Converts each "ViewX" row from the map command to a map of the ID to * the left and right parts of that mapping. * <p/> * So say the view has the mapping "\"//depot/x y/...\" //client/x/...". You will get a key * value like <code>"View2" -> {"//depot/x y/...", "//client/x/..."}</code>. Note that the * enquoting is removed - no decoding is performed at this point though. */ public Map<String, String[]> getView() { Map<String, String[]> view = new LinkedHashMap<String, String[]>(); for (String key : getViewKeys()) { String mapStr = ((String) clientMap.get(key)).trim(); String[] mapping = splitMapping(mapStr); view.put(key, mapping); } return view; } public Map<String, String> getDepotView() { Map<String, String> depot = new LinkedHashMap<String, String>(); Map<String, String[]> view = getView(); for (Map.Entry<String, String[]> entry : view.entrySet()) { depot.put(entry.getKey(), entry.getValue()[0]); } return depot; } public Map<String, String> getClientView() { Map<String, String> depot = new LinkedHashMap<String, String>(); Map<String, String[]> view = getView(); for (Map.Entry<String, String[]> entry : view.entrySet()) { depot.put(entry.getKey(), entry.getValue()[1]); } return depot; } public Map<String, String> getNonViewOptions() { Map<String, String> nonView = new LinkedHashMap<String, String>(); for (Map.Entry<String, Object> entry : clientMap.entrySet()) { if (!entry.getKey().startsWith("View") && !Character.isLowerCase(entry.getKey().charAt(0))) { nonView.put(entry.getKey(), ((String) entry.getValue()).trim()); } } return nonView; } /** * Removes all mappings in the ClientSpec. */ public void clearView() { Collection<String> viewKeys = getViewKeys(); for (String key : viewKeys) { clientMap.remove(key); } } /** * Adds a new mapping to the ClientSpec view * * @param depot * @param client */ public void addMapping(String depot, String client) { int viewNum = getViewKeys().size(); String viewId = "View" + Integer.toString(viewNum, 10); List<String> parts = new ArrayList<String>(); parts.add(depot); parts.add(client); String mapping = SpecUtils.joinAndEnquote(parts); clientMap.put(viewId, mapping); } /** * Converts string mappings. We're mostly looking to just remove quotes, * because we assume P4Java has done the encoding work from the p4 client * command. * * @param mapStr String in conventional mapping output * @return */ private String[] splitMapping(String mapStr) { StringBuilder left = new StringBuilder(); StringBuilder right = new StringBuilder(); int idx = 0; boolean inQuote = false; StringBuilder cur = left; for (; idx < mapStr.length(); ++idx) { char c = mapStr.charAt(idx); if (c == '\"') { inQuote = !inQuote; } else if (c == ' ' && !inQuote) { cur = right; } else { cur.append(c); } } return new String[]{left.toString(), right.toString()}; } public List<String> getViewKeys() { return SpecUtils.keys("View", clientMap); } }
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#2 | 9086 | tjuricek |
0.1.0 Initial release of p4java_ext Mostly just contains enough of the P4 API to build the test data initializer. About 4 commands have minimal testing, so it works. Kind of. |
||
#1 | 9085 | tjuricek | First import of the project, before package refactoring. |