/** * Stack an object onto {@link this}. Stacking means: sequentially adding {@PListObject}s onto the * {@PList}. The previous object that was stacked affects the context of the current object being * stacked. For example - if the previous element stacked was an {@link Array} or {@link Dict} - * the current object being stacked will be a child. * * @param obj * @param key If the parent of the element being added is a {@link Dict} - this is required and * must be non-null. Otherwise it's not used. * @throws Exception TODO: refactor - move me */ public void stackObject(PListObject obj, java.lang.String key) throws Exception { if (null == key && stackCtxInDict) { throw new Exception("PList objects with Dict parents require a key."); } if (stackCtxNestedDepth > 0 && !stackCtxInDict && !stackCtxInArray) { // if obj is not at root, its parent should be an Array or // Dict throw new Exception( "PList elements that are not at the root should have an Array or Dict parent."); } switch (obj.getType()) { case DICT: attachPListObjToParent(obj, key); stack.push(obj); stackCtxInArray = false; stackCtxInDict = true; stackCtxNestedDepth++; break; case ARRAY: attachPListObjToParent(obj, key); stack.push(obj); stackCtxInArray = true; stackCtxInDict = false; stackCtxNestedDepth++; break; default: attachPListObjToParent(obj, key); } }
/** * @param stack * @param key * @param obj */ private void attachPListObjToArrayParent(Stack<PListObject> stack, PListObject obj) { if (Log.isLoggable(TAG, Log.VERBOSE)) { Log.v( stringer.newBuilder().append(TAG).append("#attachPListObjToArrayParent").toString(), stringer .newBuilder() .append("obj-type|obj: ") .append(Constants.PIPE) .append(obj.getType()) .append(Constants.PIPE) .append(obj.toString()) .append(Constants.PIPE) .toString()); } Array parent = (Array) stack.pop(); parent.add(obj); stack.push(parent); }
/** * @param stack * @param key * @param obj */ private void attachPListObjToDictParent(PListObject obj, java.lang.String key) { if (Log.isLoggable(TAG, Log.VERBOSE)) { Log.v( stringer.newBuilder().append(TAG).append("#attachPListObjToDictParent").toString(), stringer .newBuilder() .append("key|obj-type|obj: ") .append(key) .append(Constants.PIPE) .append(obj.getType()) .append(Constants.PIPE) .append(obj.toString()) .append(Constants.PIPE) .toString()); } Dict parent = (Dict) stack.pop(); parent.putConfig(key, obj); stack.push(parent); }