| /******************************************************************************* |
| * Copyright (c) 2012-2014 SAP SE. |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License v1.0 |
| * which accompanies this distribution, and is available at |
| * http://www.eclipse.org/legal/epl-v10.html |
| * |
| * Contributors: |
| * SAP SE - initial API and implementation and/or initial documentation |
| * |
| *******************************************************************************/ |
| package org.eclipse.ogee.client.parser.impl; |
| |
| import org.eclipse.ogee.client.model.atom.ODataPropertyImpl; |
| import org.eclipse.ogee.client.model.generic.Arrays; |
| import org.eclipse.ogee.client.model.generic.ComplexTypeCollection; |
| import org.eclipse.ogee.client.model.generic.ODataProperty; |
| import org.xml.sax.Attributes; |
| |
| /** |
| * Auxiliary class for parsing a complex type collection. Not to be used |
| * explicitly. |
| * |
| */ |
| public class CollectionComplexTagFactory implements TagFactory { |
| private ComplexTypeCollection root; |
| |
| public CollectionComplexTagFactory(String serviceOperation) { |
| super(); |
| root = new ComplexTypeCollection(serviceOperation); |
| } |
| |
| public TagObject createTagObject(String tagName, Attributes attr) { |
| TagObject result = new TagObject(); |
| result.setTag(tagName); |
| |
| int attrCount = attr.getLength(); |
| |
| if (tagName.equalsIgnoreCase(this.root.getQualifiedName())) { |
| if (attrCount > 0) { |
| String attrName, attrValue; |
| for (int i = 0; i < attrCount; i++) { |
| attrName = attr.getQName(i); |
| attrValue = attr.getValue(i); |
| |
| this.root.setAttribute(attrName, attrValue); |
| } |
| } |
| result.setObject(this.root); |
| } else if (tagName.equalsIgnoreCase("d:element")) { |
| ODataPropertyImpl element = new ODataPropertyImpl(); |
| if (attrCount > 0) { |
| String attrName, attrValue; |
| for (int i = 0; i < attrCount; i++) { |
| attrName = attr.getQName(i); |
| attrValue = attr.getValue(i); |
| if (attrName.equalsIgnoreCase(M_TYPE)) |
| element.setType(attr.getValue(i)); |
| else |
| element.setAttribute(attrName, attrValue); |
| } |
| } |
| result.setObject(element); |
| } else if (tagName.startsWith(D)) { |
| ODataPropertyImpl valueObject = new ODataPropertyImpl(); |
| if (attrCount > 0) { |
| String attrName, attrValue; |
| for (int i = 0; i < attrCount; i++) { |
| attrName = attr.getQName(i); |
| attrValue = attr.getValue(i); |
| if (attrName.equalsIgnoreCase(M_TYPE)) |
| valueObject.setType(attrValue); |
| else |
| valueObject.setAttribute(attrName, attrValue); |
| } |
| } |
| result.setObject(valueObject); |
| } |
| |
| return result; |
| } |
| |
| public void setValue(TagObject tagObject, String value) { |
| String tagName = tagObject.getTag(); |
| if (!tagName.equalsIgnoreCase(this.root.getName())) { |
| if (tagName.startsWith(D) |
| && !tagName.equalsIgnoreCase(this.root.getQualifiedName())) { |
| ODataPropertyImpl valueObject = (ODataPropertyImpl) tagObject |
| .getObject(); |
| valueObject.setValue(value); |
| valueObject.setName(tagName); |
| } else if (tagObject.getObject() == null && value != null) { |
| tagObject.setObject(value); |
| } |
| } |
| |
| } |
| |
| public void addChild(TagObject parent, TagObject tagObject) { |
| String tag = tagObject.getTag(); |
| String parentTag = parent.getTag(); |
| |
| if (parentTag.equalsIgnoreCase(this.root.getQualifiedName())) { |
| this.root = (ComplexTypeCollection) parent.getObject(); |
| |
| if (tag.equalsIgnoreCase("d:element")) { |
| ODataProperty[] elements = (ODataProperty[]) this.root |
| .getElements(); |
| if (elements == null) { |
| this.root.setElements(new ODataPropertyImpl[0]); |
| elements = (ODataPropertyImpl[]) this.root.getElements(); |
| } |
| |
| ODataPropertyImpl element = (ODataPropertyImpl) tagObject |
| .getObject(); |
| Arrays.add(this.root, elements, element); |
| |
| } |
| |
| } else if (parentTag.equalsIgnoreCase("d:element")) { |
| ODataPropertyImpl element = (ODataPropertyImpl) parent.getObject(); |
| |
| if (tag.startsWith(D)) { |
| ODataPropertyImpl dataValue = (ODataPropertyImpl) tagObject |
| .getObject(); |
| element.putChildDataValue(dataValue); |
| } |
| } else if (!parentTag.equalsIgnoreCase(this.root.getQualifiedName())) { |
| ODataPropertyImpl parentDataValue = (ODataPropertyImpl) parent |
| .getObject(); |
| if (tag.startsWith(D)) { |
| ODataPropertyImpl dataValue = (ODataPropertyImpl) tagObject |
| .getObject(); |
| parentDataValue.putChildDataValue(dataValue); |
| } |
| } |
| |
| } |
| |
| } |