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: * Map element from a .jdo file.
022: */
023: public final class JdoMap extends JdoElement {
024:
025: public String keyType;
026: public int embeddedKey;
027: public String valueType;
028: public int embeddedValue;
029: public JdoExtension[] extensions;
030: public JdoField parent;
031:
032: public JdoElement getParent() {
033: return parent;
034: }
035:
036: /**
037: * Get information for this element to be used in building up a
038: * context string.
039: * @see #getContext
040: */
041: public String getSubContext() {
042: return "map";
043: }
044:
045: public String toString() {
046: StringBuffer s = new StringBuffer();
047: s.append("map keyType=");
048: s.append(keyType);
049: s.append(" embeddedKey=");
050: s.append(MDStaticUtils.toTriStateString(embeddedKey));
051: s.append(" valueType=");
052: s.append(valueType);
053: s.append(" embeddedValue=");
054: s.append(MDStaticUtils.toTriStateString(embeddedValue));
055: return s.toString();
056: }
057:
058: public void dump() {
059: dump(Debug.OUT, "");
060: }
061:
062: public void dump(PrintStream out, String indent) {
063: out.println(indent + this );
064: String is = indent + " ";
065: if (extensions != null) {
066: for (int i = 0; i < extensions.length; i++) {
067: extensions[i].dump(out, is);
068: }
069: }
070: }
071:
072: /**
073: * Get the fully qualified name of our value type (or null if none).
074: */
075: public String getValueTypeQName() {
076: return getQName(valueType);
077: }
078:
079: /**
080: * Get the fully qualified name of our key type (or null if none).
081: */
082: public String getKeyTypeQName() {
083: return getQName(keyType);
084: }
085:
086: private String getQName(String n) {
087: if (n == null)
088: return null;
089: int i = n.indexOf('.');
090: if (i >= 0)
091: return n;
092: String packageName = parent.parent.parent.name;
093: if (packageName.length() == 0)
094: return n;
095: return packageName + '.' + n;
096: }
097:
098: public JdoMap createCopy(JdoField field) {
099: JdoMap tmp = new JdoMap();
100: tmp.parent = field;
101: tmp.keyType = keyType;
102: tmp.embeddedKey = embeddedKey;
103: tmp.valueType = valueType;
104: tmp.embeddedValue = embeddedValue;
105:
106: if (extensions != null) {
107: tmp.extensions = new JdoExtension[extensions.length];
108: for (int i = 0; i < extensions.length; i++) {
109: tmp.extensions[i] = extensions[i].createCopy(tmp);
110: }
111: }
112: return tmp;
113: }
114:
115: /**
116: * May not update key of value types. Only columns may be renamed.
117: */
118: public void synchronizeForHorizontal(JdoMap map) {
119: if (!valueType.equals(map.valueType)
120: || !keyType.equals(map.keyType)) {
121: throw BindingSupportImpl.getInstance().invalidOperation(
122: "May not override the key of value type of map. "
123: + "\nUpdated: " + this + "\nOriginal: "
124: + map);
125: }
126:
127: if (map.extensions != null) {
128: JdoExtension[] copy = new JdoExtension[map.extensions.length];
129: for (int i = 0; i < map.extensions.length; i++) {
130: copy[i] = map.extensions[i].createCopy(this );
131: }
132: if (extensions != null) {
133: JdoExtension.synchronize3(extensions, copy,
134: Collections.EMPTY_SET, false);
135: } else {
136: extensions = copy;
137: }
138: }
139: }
140: }
|