001: /*
002: * Copyright (c) 1998 - 2005 Versant Corporation
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * Versant Corporation - initial API and implementation
010: */
011: package com.versant.core.metadata.parser;
012:
013: import com.versant.core.metadata.MDStaticUtils;
014: import com.versant.core.common.Debug;
015: import com.versant.core.common.BindingSupportImpl;
016:
017: import java.io.PrintStream;
018: import java.util.Collections;
019:
020: /**
021: * Collection element from a .jdo file.
022: */
023: public final class JdoCollection extends JdoElement {
024:
025: public String elementType;
026: public int embeddedElement;
027: public JdoExtension[] extensions;
028: public JdoField parent;
029:
030: public JdoElement getParent() {
031: return parent;
032: }
033:
034: /**
035: * Get information for this element to be used in building up a
036: * context string.
037: * @see #getContext
038: */
039: public String getSubContext() {
040: return "collection";
041: }
042:
043: public String toString() {
044: StringBuffer s = new StringBuffer();
045: s.append("collection elementType=");
046: s.append(elementType);
047: s.append(" embeddedElement=");
048: s.append(MDStaticUtils.toTriStateString(embeddedElement));
049: return s.toString();
050: }
051:
052: public void dump() {
053: dump(Debug.OUT, "");
054: }
055:
056: public void dump(PrintStream out, String indent) {
057: out.println(indent + this );
058: if (extensions != null) {
059: for (int i = 0; i < extensions.length; i++) {
060: extensions[i].dump(out, indent + " ");
061: }
062: }
063: }
064:
065: /**
066: * Get the fully qualified name of our element type (or null if none).
067: */
068: public String getElementTypeQName() {
069: if (elementType == null)
070: return null;
071: int i = elementType.indexOf('.');
072: if (i >= 0)
073: return elementType;
074: String packageName = parent.parent.parent.name;
075: if (packageName.length() == 0)
076: return elementType;
077: return packageName + '.' + elementType;
078: }
079:
080: public JdoCollection createCopy(JdoField parent) {
081: JdoCollection tmp = new JdoCollection();
082: tmp.parent = parent;
083: tmp.elementType = elementType;
084: tmp.embeddedElement = embeddedElement;
085:
086: if (extensions != null) {
087: tmp.extensions = new JdoExtension[extensions.length];
088: for (int i = 0; i < extensions.length; i++) {
089: tmp.extensions[i] = extensions[i].createCopy(tmp);
090: }
091: }
092: return tmp;
093: }
094:
095: /**
096: *
097: */
098: public void synchronizeForHorizontal(JdoCollection col) {
099: if (!elementType.equals(col.elementType)) {
100: throw BindingSupportImpl
101: .getInstance()
102: .invalidOperation(
103: "May not override the element type of collection. "
104: + "\nAttemptin to change 'element-type' from "
105: + col.elementType + " to "
106: + elementType);
107: }
108:
109: if (col.extensions != null) {
110: JdoExtension[] copy = new JdoExtension[col.extensions.length];
111: for (int i = 0; i < col.extensions.length; i++) {
112: copy[i] = col.extensions[i].createCopy(this );
113: }
114: if (extensions != null) {
115: JdoExtension.synchronize3(extensions, copy,
116: Collections.EMPTY_SET, false);
117: } else {
118: extensions = copy;
119: }
120: }
121: }
122:
123: /**
124: * Create the extension if it does not exist, else update it if overwrite is true
125: * @param key
126: * @param value
127: * @param overwrite
128: */
129: public JdoExtension findCreate(int key, String value,
130: boolean overwrite) {
131: if (extensions == null) {
132: extensions = new JdoExtension[] { createChild(key, value,
133: this ) };
134: return extensions[0];
135: }
136:
137: JdoExtension ext = JdoExtension.find(key, extensions);
138: if (ext == null) {
139: extensions = addExtension(extensions, (ext = createChild(
140: key, value, this)));
141: } else if (overwrite) {
142: ext.value = value;
143: }
144: return ext;
145: }
146: }
|