001: /**********************************************************************
002: Copyright (c) 2006 Andy Jefferson and others. All rights reserved.
003: Licensed under the Apache License, Version 2.0 (the "License");
004: you may not use this file except in compliance with the License.
005: You may obtain a copy of the License at
006:
007: http://www.apache.org/licenses/LICENSE-2.0
008:
009: Unless required by applicable law or agreed to in writing, software
010: distributed under the License is distributed on an "AS IS" BASIS,
011: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: See the License for the specific language governing permissions and
013: limitations under the License.
014:
015: Contributors:
016: ...
017: **********************************************************************/package org.jpox.jdo.metadata;
018:
019: import org.xml.sax.Attributes;
020: import org.xml.sax.EntityResolver;
021: import org.xml.sax.SAXException;
022:
023: import org.jpox.metadata.AbstractClassMetaData;
024: import org.jpox.metadata.AbstractElementMetaData;
025: import org.jpox.metadata.AbstractMemberMetaData;
026: import org.jpox.metadata.ArrayMetaData;
027: import org.jpox.metadata.ClassMetaData;
028: import org.jpox.metadata.CollectionMetaData;
029: import org.jpox.metadata.ColumnMetaData;
030: import org.jpox.metadata.DiscriminatorMetaData;
031: import org.jpox.metadata.ElementMetaData;
032: import org.jpox.metadata.EmbeddedMetaData;
033: import org.jpox.metadata.FetchGroupMetaData;
034: import org.jpox.metadata.FetchPlanMetaData;
035: import org.jpox.metadata.FieldMetaData;
036: import org.jpox.metadata.FileMetaData;
037: import org.jpox.metadata.ForeignKeyMetaData;
038: import org.jpox.metadata.IdentityMetaData;
039: import org.jpox.metadata.ImplementsMetaData;
040: import org.jpox.metadata.IndexMetaData;
041: import org.jpox.metadata.InheritanceMetaData;
042: import org.jpox.metadata.InterfaceMetaData;
043: import org.jpox.metadata.JoinMetaData;
044: import org.jpox.metadata.KeyMetaData;
045: import org.jpox.metadata.MapMetaData;
046: import org.jpox.metadata.MetaData;
047: import org.jpox.metadata.MetaDataManager;
048: import org.jpox.metadata.OrderMetaData;
049: import org.jpox.metadata.PackageMetaData;
050: import org.jpox.metadata.PrimaryKeyMetaData;
051: import org.jpox.metadata.PropertyMetaData;
052: import org.jpox.metadata.QueryMetaData;
053: import org.jpox.metadata.SequenceMetaData;
054: import org.jpox.metadata.UniqueMetaData;
055: import org.jpox.metadata.ValueMetaData;
056: import org.jpox.metadata.VersionMetaData;
057: import org.jpox.metadata.xml.AbstractMetaDataHandler;
058: import org.jpox.util.JPOXLogger;
059:
060: /**
061: * Parser handler for JDO MetaData.
062: * Implements DefaultHandler and handles the extracting of MetaData for JDO
063: * from the XML elements/attributes. This class simply constructs the MetaData
064: * representation mirroring what is in the MetaData file. It has no knowledge
065: * of the class(es) that it represents, simply the information in the MetaData
066: * file. The knowledge of the classes is imposed on the representation at a
067: * later stage where necessary.
068: * <P>Operates the parse process using a Stack. MetaData components are added
069: * to the stack as they are encountered and created. They are then popped off
070: * the stack when the end element is encountered.</P>
071: *
072: * @since 1.1
073: * @version $Revision: 1.2 $
074: */
075: public class JDOMetaDataHandler extends AbstractMetaDataHandler {
076: /**
077: * Constructor. Protected to prevent instantiation.
078: * @param mgr the metadata manager
079: * @param filename The name of the file to parse
080: * @param resolver Entity Resolver to use (null if not available)
081: */
082: public JDOMetaDataHandler(MetaDataManager mgr, String filename,
083: EntityResolver resolver) {
084: super (mgr, filename, resolver);
085: metadata = new FileMetaData(filename, mgr, null, null);
086: pushStack(metadata);
087: }
088:
089: /**
090: * Utility to create a new class component.
091: * @param pmd The parent PackageMetaData
092: * @param attrs The attributes
093: * @return The ClassMetaData
094: */
095: protected ClassMetaData newClassObject(PackageMetaData pmd,
096: Attributes attrs) {
097: return mgr.getMetaDataFactory().newClassObject(pmd,
098: getAttr(attrs, "name"),
099: getAttr(attrs, "identity-type"),
100: getAttr(attrs, "objectid-class"),
101: getAttr(attrs, "requires-extent"),
102: getAttr(attrs, "detachable"),
103: getAttr(attrs, "embedded-only"),
104: getAttr(attrs, "persistence-modifier"),
105: getAttr(attrs, "persistence-capable-superclass"),
106: getAttr(attrs, "catalog"), getAttr(attrs, "schema"),
107: getAttr(attrs, "table"), null);
108: }
109:
110: /**
111: * Utility to create a new field component.
112: * @param md The parent MetaData
113: * @param attrs The attributes
114: * @return The FieldMetaData
115: */
116: protected FieldMetaData newFieldObject(MetaData md, Attributes attrs) {
117: return mgr.getMetaDataFactory().newFieldObject(
118: md,
119: getAttr(attrs, "name"),
120: getAttr(attrs, "primary-key"),
121: getAttr(attrs, "persistence-modifier"),
122: getAttr(attrs, "default-fetch-group"),
123: getAttr(attrs, "null-value"),
124: getAttr(attrs, "embedded"),
125: getAttr(attrs, "serialized"),
126: getAttr(attrs, "dependent"),
127: getAttr(attrs, "mapped-by"),
128: getAttr(attrs, "column"),
129: getAttr(attrs, "table"),
130: null, // Catalog not supported in JDO2 metadata
131: null, // Schema not supported in JDO2 metadata
132: getAttr(attrs, "delete-action"),
133: getAttr(attrs, "indexed"), getAttr(attrs, "unique"),
134: getAttr(attrs, "recursion-depth"),
135: getAttr(attrs, "load-fetch-group"),
136: getAttr(attrs, "value-strategy"),
137: getAttr(attrs, "sequence"),
138: getAttr(attrs, "field-type"));
139: }
140:
141: /**
142: * Utility to create a new property component.
143: * @param md The parent MetaData
144: * @param attrs The attributes
145: * @return The PropertyMetaData
146: */
147: protected PropertyMetaData newPropertyObject(MetaData md,
148: Attributes attrs) {
149: return mgr.getMetaDataFactory().newPropertyObject(
150: md,
151: getAttr(attrs, "name"),
152: getAttr(attrs, "primary-key"),
153: getAttr(attrs, "persistence-modifier"),
154: getAttr(attrs, "default-fetch-group"),
155: getAttr(attrs, "null-value"),
156: getAttr(attrs, "embedded"),
157: getAttr(attrs, "serialized"),
158: getAttr(attrs, "dependent"),
159: getAttr(attrs, "mapped-by"),
160: getAttr(attrs, "column"),
161: getAttr(attrs, "table"),
162: null, // Catalog not supported in JDO2 metadata
163: null, // Schema not supported in JDO2 metadata
164: getAttr(attrs, "delete-action"),
165: getAttr(attrs, "indexed"), getAttr(attrs, "unique"),
166: getAttr(attrs, "recursion-depth"),
167: getAttr(attrs, "load-fetch-group"),
168: getAttr(attrs, "value-strategy"),
169: getAttr(attrs, "sequence"),
170: getAttr(attrs, "field-type"),
171: getAttr(attrs, "field-name"));
172: }
173:
174: /**
175: * Handler method called at the start of an element.
176: * @param uri URI of the tag
177: * @param localName Local name
178: * @param qName Element name
179: * @param attrs Attributes for this element
180: * @throws SAXException in parsing errors
181: */
182: public void startElement(String uri, String localName,
183: String qName, Attributes attrs) throws SAXException {
184: if (charactersBuffer.length() > 0) {
185: // Cater for subelements that appear before the end of the body text (save the body text with the parent)
186: String currentString = getString().trim();
187: if (getStack() instanceof QueryMetaData) {
188: ((QueryMetaData) getStack()).setQuery(currentString
189: .trim());
190: }
191: }
192:
193: if (JPOXLogger.METADATA.isDebugEnabled()) {
194: StringBuffer sb = new StringBuffer();
195: sb.append("<" + qName);
196: for (int i = 0; i < attrs.getLength(); i++) {
197: sb.append(" ");
198: sb.append(attrs.getQName(i)).append("=\"").append(
199: attrs.getValue(i)).append("\"");
200: }
201: sb.append(">");
202: JPOXLogger.METADATA.debug(LOCALISER.msg("044034", sb
203: .toString(), "" + stack.size()));
204: }
205: if (localName.length() < 1) {
206: localName = qName;
207: }
208: try {
209: // New JDO file
210: if (localName.equals("jdo")) {
211: FileMetaData filemd = (FileMetaData) getStack();
212: filemd.setType(FileMetaData.JDO_FILE);
213: filemd.setCatalog(getAttr(attrs, "catalog"));
214: filemd.setSchema(getAttr(attrs, "schema"));
215: }
216: // New JDO O/R mapping file
217: else if (localName.equals("orm")) {
218: FileMetaData filemd = (FileMetaData) getStack();
219: filemd.setType(FileMetaData.ORM_FILE);
220: filemd.setCatalog(getAttr(attrs, "catalog"));
221: filemd.setSchema(getAttr(attrs, "schema"));
222: }
223: // New JDO Query file
224: else if (localName.equals("jdoquery")) {
225: FileMetaData filemd = (FileMetaData) getStack();
226: filemd.setType(FileMetaData.JDOQUERY_FILE);
227: }
228: // New fetch-plan for this file
229: else if (localName.equals("fetch-plan")) {
230: FileMetaData filemd = (FileMetaData) getStack();
231: FetchPlanMetaData fpmd = new FetchPlanMetaData(filemd,
232: getAttr(attrs, "name"), getAttr(attrs,
233: "max-fetch-depth"), getAttr(attrs,
234: "fetch-size"));
235: ((FileMetaData) metadata).addFetchPlan(fpmd);
236: pushStack(fpmd);
237: }
238: // New package for this file
239: else if (localName.equals("package")) {
240: FileMetaData filemd = (FileMetaData) getStack();
241: PackageMetaData pmd = new PackageMetaData(filemd,
242: getAttr(attrs, "name"), getAttr(attrs,
243: "catalog"), getAttr(attrs, "schema"));
244: ((FileMetaData) metadata).addPackage(pmd);
245: pushStack(pmd);
246: }
247: // New class for this package
248: else if (localName.equals("class")) {
249: PackageMetaData pmd = (PackageMetaData) getStack();
250: ClassMetaData cmd = newClassObject(pmd, attrs);
251: pmd.addClass(cmd);
252:
253: pushStack(cmd);
254: }
255: // New interface for this package
256: else if (localName.equals("interface")) {
257: PackageMetaData pmd = (PackageMetaData) getStack();
258: InterfaceMetaData imd = mgr.getMetaDataFactory()
259: .newInterfaceObject(pmd,
260: getAttr(attrs, "name"),
261: getAttr(attrs, "identity-type"),
262: getAttr(attrs, "objectid-class"),
263: getAttr(attrs, "requires-extent"),
264: getAttr(attrs, "detachable"),
265: getAttr(attrs, "embedded-only"),
266: getAttr(attrs, "catalog"),
267: getAttr(attrs, "schema"),
268: getAttr(attrs, "table"), null);
269: pmd.addInterface(imd);
270: pushStack(imd);
271: }
272: // New primary-key for class/interface
273: else if (localName.equals("primary-key")) {
274: MetaData md = getStack();
275: PrimaryKeyMetaData pkmd = new PrimaryKeyMetaData(md,
276: getAttr(attrs, "name"),
277: getAttr(attrs, "column"));
278: if (md instanceof AbstractClassMetaData) {
279: ((AbstractClassMetaData) md)
280: .setPrimaryKeyMetaData(pkmd);
281: } else if (md instanceof JoinMetaData) {
282: ((JoinMetaData) md).setPrimaryKeyMetaData(pkmd);
283: }
284: pushStack(pkmd);
285: }
286: // New implements for this class
287: else if (localName.equals("implements")) {
288: ClassMetaData cmd = (ClassMetaData) getStack();
289: ImplementsMetaData imd = new ImplementsMetaData(cmd,
290: getAttr(attrs, "name"));
291: cmd.addImplements(imd);
292: pushStack(imd);
293: }
294: // New property for this interface
295: else if (localName.equals("property")) {
296: MetaData parent = getStack();
297: if (parent instanceof AbstractClassMetaData) {
298: AbstractClassMetaData acmd = (AbstractClassMetaData) parent;
299: PropertyMetaData propmd = newPropertyObject(acmd,
300: attrs);
301: acmd.addMember(propmd);
302: pushStack(propmd);
303: } else if (parent instanceof EmbeddedMetaData) {
304: EmbeddedMetaData emd = (EmbeddedMetaData) parent;
305: PropertyMetaData propmd = newPropertyObject(emd,
306: attrs);
307: emd.addMember(propmd);
308: pushStack(propmd);
309: } else if (parent instanceof ImplementsMetaData) {
310: ImplementsMetaData implmd = (ImplementsMetaData) parent;
311: PropertyMetaData propmd = newPropertyObject(implmd,
312: attrs);
313: implmd.addProperty(propmd);
314: pushStack(propmd);
315: } else if (parent instanceof FetchGroupMetaData) {
316: FetchGroupMetaData fgmd = (FetchGroupMetaData) parent;
317: PropertyMetaData propmd = newPropertyObject(fgmd,
318: attrs);
319: fgmd.addMember(propmd);
320: pushStack(propmd);
321: }
322: }
323: // New datastore-identity for this class
324: else if (localName.equals("datastore-identity")) {
325: AbstractClassMetaData acmd = (AbstractClassMetaData) getStack();
326: IdentityMetaData imd = new IdentityMetaData(acmd,
327: getAttr(attrs, "column"), getAttr(attrs,
328: "strategy"), getAttr(attrs, "sequence"));
329: acmd.setIdentityMetaData(imd);
330: pushStack(imd);
331: }
332: // New inheritance for this class
333: else if (localName.equals("inheritance")) {
334: MetaData parent = getStack();
335: AbstractClassMetaData acmd = (AbstractClassMetaData) parent;
336: InheritanceMetaData imd = new InheritanceMetaData(acmd,
337: getAttr(attrs, "strategy"));
338: acmd.setInheritanceMetaData(imd);
339: pushStack(imd);
340: }
341: // Discriminator for this inheritance
342: else if (localName.equals("discriminator")) {
343: InheritanceMetaData imd = (InheritanceMetaData) getStack();
344: DiscriminatorMetaData dismd = new DiscriminatorMetaData(
345: imd, getAttr(attrs, "column"), getAttr(attrs,
346: "value"), getAttr(attrs, "strategy"),
347: getAttr(attrs, "indexed"));
348: imd.setDiscriminatorMetaData(dismd);
349: pushStack(dismd);
350: }
351: // New query for this class
352: else if (localName.equals("query")) {
353: MetaData emd = getStack();
354: QueryMetaData qmd = new QueryMetaData(
355: emd,
356: (emd instanceof ClassMetaData ? ((ClassMetaData) emd)
357: .getFullClassName()
358: : null), getAttr(attrs, "name"),
359: getAttr(attrs, "language"), getAttr(attrs,
360: "unmodifiable"), getAttr(attrs,
361: "result-class"), null, getAttr(attrs,
362: "unique"), getAttr(attrs, "fetch-plan"));
363: if (emd instanceof ClassMetaData) {
364: ClassMetaData cmd = (ClassMetaData) emd;
365: cmd.addQuery(qmd);
366: } else if (emd instanceof FileMetaData) {
367: FileMetaData fmd = (FileMetaData) emd;
368: fmd.addQuery(qmd);
369: }
370: pushStack(qmd);
371: }
372: // New sequence for this class
373: else if (localName.equals("sequence")) {
374: MetaData emd = getStack();
375: SequenceMetaData seqmd = new SequenceMetaData(emd,
376: getAttr(attrs, "name"), getAttr(attrs,
377: "datastore-sequence"), getAttr(attrs,
378: "factory-class"), getAttr(attrs,
379: "strategy"), null, null);
380: if (emd instanceof PackageMetaData) {
381: PackageMetaData pmd = (PackageMetaData) emd;
382: pmd.addSequence(seqmd);
383: }
384: pushStack(seqmd);
385: }
386: // New field for this class
387: else if (localName.equals("field")) {
388: MetaData md = getStack();
389: FieldMetaData fmd = newFieldObject(md, attrs);
390: if (md instanceof ClassMetaData) {
391: ClassMetaData cmd = (ClassMetaData) md;
392: cmd.addMember(fmd);
393: } else if (md instanceof FetchGroupMetaData) {
394: FetchGroupMetaData fgmd = (FetchGroupMetaData) md;
395: fgmd.addMember(fmd);
396: } else if (md instanceof EmbeddedMetaData) {
397: EmbeddedMetaData emd = (EmbeddedMetaData) md;
398: emd.addMember(fmd);
399: } else if (md instanceof ForeignKeyMetaData) {
400: ForeignKeyMetaData fkmd = (ForeignKeyMetaData) md;
401: fkmd.addMember(fmd);
402: } else if (md instanceof IndexMetaData) {
403: IndexMetaData imd = (IndexMetaData) md;
404: imd.addMember(fmd);
405: } else if (md instanceof UniqueMetaData) {
406: UniqueMetaData umd = (UniqueMetaData) md;
407: umd.addMember(fmd);
408: }
409: pushStack(fmd);
410: }
411: // New join
412: else if (localName.equals("join")) {
413: MetaData parent = getStack();
414: JoinMetaData jnmd = new JoinMetaData(parent, getAttr(
415: attrs, "table"), null, null, getAttr(attrs,
416: "column"), getAttr(attrs, "outer"), getAttr(
417: attrs, "delete-action"), getAttr(attrs,
418: "indexed"), getAttr(attrs, "unique"));
419: if (parent instanceof AbstractMemberMetaData) {
420: AbstractMemberMetaData fmd = (AbstractMemberMetaData) parent;
421: fmd.setJoinMetaData(jnmd);
422: } else if (parent instanceof AbstractClassMetaData) {
423: AbstractClassMetaData cmd = (AbstractClassMetaData) parent;
424: cmd.addJoin(jnmd);
425: } else if (parent instanceof InheritanceMetaData) {
426: InheritanceMetaData imd = (InheritanceMetaData) parent;
427: imd.setJoinMetaData(jnmd);
428: }
429: pushStack(jnmd);
430: }
431: // New map container for this field
432: else if (localName.equals("map")) {
433: AbstractMemberMetaData fmd = (AbstractMemberMetaData) getStack();
434: MapMetaData map = new MapMetaData(fmd, getAttr(attrs,
435: "key-type"), getAttr(attrs, "embedded-key"),
436: getAttr(attrs, "dependent-key"), getAttr(attrs,
437: "serialized-key"), getAttr(attrs,
438: "value-type"), getAttr(attrs,
439: "embedded-value"), getAttr(attrs,
440: "dependent-value"), getAttr(attrs,
441: "serialized-value"));
442: fmd.setContainer(map);
443: pushStack(map);
444: }
445: // New array container for this field
446: else if (localName.equals("array")) {
447: AbstractMemberMetaData fmd = (AbstractMemberMetaData) getStack();
448: ArrayMetaData amd = new ArrayMetaData(fmd, getAttr(
449: attrs, "element-type"), getAttr(attrs,
450: "embedded-element"), getAttr(attrs,
451: "dependent-element"), getAttr(attrs,
452: "serialized-element"));
453: fmd.setContainer(amd);
454: pushStack(amd);
455: }
456: // New collection container for this field
457: else if (localName.equals("collection")) {
458: AbstractMemberMetaData fmd = (AbstractMemberMetaData) getStack();
459: CollectionMetaData colmd = new CollectionMetaData(fmd,
460: getAttr(attrs, "element-type"), getAttr(attrs,
461: "embedded-element"), getAttr(attrs,
462: "dependent-element"), getAttr(attrs,
463: "serialized-element"));
464: fmd.setContainer(colmd);
465: pushStack(colmd);
466: }
467: // New column
468: else if (localName.equals("column")) {
469: MetaData md = getStack();
470: ColumnMetaData clnmd = new ColumnMetaData(md, getAttr(
471: attrs, "name"), getAttr(attrs, "target"),
472: getAttr(attrs, "target-field"), getAttr(attrs,
473: "jdbc-type"),
474: getAttr(attrs, "sql-type"), getAttr(attrs,
475: "length"), getAttr(attrs, "scale"),
476: getAttr(attrs, "allows-null"), getAttr(attrs,
477: "default-value"), getAttr(attrs,
478: "insert-value"), null, null, null);
479: if (md instanceof AbstractMemberMetaData) {
480: AbstractMemberMetaData fmd = (AbstractMemberMetaData) md;
481: fmd.addColumn(clnmd);
482: } else if (md instanceof AbstractElementMetaData) {
483: AbstractElementMetaData elemd = (AbstractElementMetaData) md;
484: elemd.addColumn(clnmd);
485: } else if (md instanceof JoinMetaData) {
486: JoinMetaData jnmd = (JoinMetaData) md;
487: jnmd.addColumn(clnmd);
488: } else if (md instanceof IdentityMetaData) {
489: IdentityMetaData idmd = (IdentityMetaData) md;
490: idmd.addColumn(clnmd);
491: } else if (md instanceof ForeignKeyMetaData) {
492: ForeignKeyMetaData fkmd = (ForeignKeyMetaData) md;
493: fkmd.addColumn(clnmd);
494: } else if (md instanceof IndexMetaData) {
495: IndexMetaData idxmd = (IndexMetaData) md;
496: idxmd.addColumn(clnmd);
497: } else if (md instanceof UniqueMetaData) {
498: UniqueMetaData unimd = (UniqueMetaData) md;
499: unimd.addColumn(clnmd);
500: } else if (md instanceof OrderMetaData) {
501: OrderMetaData ormd = (OrderMetaData) md;
502: ormd.addColumn(clnmd);
503: } else if (md instanceof DiscriminatorMetaData) {
504: DiscriminatorMetaData dismd = (DiscriminatorMetaData) md;
505: dismd.setColumnMetaData(clnmd);
506: } else if (md instanceof VersionMetaData) {
507: VersionMetaData vermd = (VersionMetaData) md;
508: vermd.addColumn(clnmd);
509: } else if (md instanceof AbstractClassMetaData) {
510: AbstractClassMetaData cmd = (AbstractClassMetaData) md;
511: cmd.addUnmappedColumn(clnmd);
512: } else if (md instanceof PrimaryKeyMetaData) {
513: PrimaryKeyMetaData pkmd = (PrimaryKeyMetaData) md;
514: pkmd.addColumn(clnmd);
515: }
516: pushStack(clnmd);
517: }
518: // New element
519: else if (localName.equals("element")) {
520: AbstractMemberMetaData fmd = (AbstractMemberMetaData) getStack();
521: ElementMetaData elemd = new ElementMetaData(fmd,
522: getAttr(attrs, "column"), getAttr(attrs,
523: "delete-action"), getAttr(attrs,
524: "update-action"), getAttr(attrs,
525: "indexed"), getAttr(attrs, "unique"),
526: getAttr(attrs, "mapped-by"));
527: fmd.setElementMetaData(elemd);
528: pushStack(elemd);
529: }
530: // New key
531: else if (localName.equals("key")) {
532: AbstractMemberMetaData fmd = (AbstractMemberMetaData) getStack();
533: KeyMetaData keymd = new KeyMetaData(fmd, getAttr(attrs,
534: "column"), getAttr(attrs, "delete-action"),
535: getAttr(attrs, "update-action"), getAttr(attrs,
536: "indexed"), getAttr(attrs, "unique"),
537: getAttr(attrs, "mapped-by"));
538: fmd.setKeyMetaData(keymd);
539: pushStack(keymd);
540: }
541: // New value
542: else if (localName.equals("value")) {
543: AbstractMemberMetaData fmd = (AbstractMemberMetaData) getStack();
544: ValueMetaData valuemd = new ValueMetaData(fmd, getAttr(
545: attrs, "column"), getAttr(attrs,
546: "delete-action"), getAttr(attrs,
547: "update-action"), getAttr(attrs, "indexed"),
548: getAttr(attrs, "unique"), getAttr(attrs,
549: "mapped-by"));
550: fmd.setValueMetaData(valuemd);
551: pushStack(valuemd);
552: }
553: // New fetch-group
554: else if (localName.equals("fetch-group")) {
555: MetaData md = getStack();
556: FetchGroupMetaData fgmd = new FetchGroupMetaData(md,
557: getAttr(attrs, "post-load"), getAttr(attrs,
558: "name"));
559: if (md instanceof FetchGroupMetaData) {
560: FetchGroupMetaData fgmdParent = (FetchGroupMetaData) md;
561: fgmdParent.addFetchGroup(fgmd);
562: } else if (md instanceof AbstractClassMetaData) {
563: AbstractClassMetaData cmd = (AbstractClassMetaData) md;
564: cmd.addFetchGroup(fgmd);
565: } else if (md instanceof FetchPlanMetaData) {
566: FetchPlanMetaData fpmd = (FetchPlanMetaData) md;
567: fpmd.addFetchGroup(fgmd);
568: }
569: pushStack(fgmd);
570: }
571: // New extension for this tag
572: else if (localName.equals("extension")) {
573: MetaData md = getStack();
574: md.addExtension(getAttr(attrs, "vendor-name"), getAttr(
575: attrs, "key"), getAttr(attrs, "value"));
576: }
577: // New version
578: else if (localName.equals("version")) {
579: AbstractClassMetaData cmd = (AbstractClassMetaData) getStack();
580: VersionMetaData vermd = new VersionMetaData(getAttr(
581: attrs, "strategy"), getAttr(attrs, "column"),
582: getAttr(attrs, "indexed"));
583: cmd.setVersionMetaData(vermd);
584: pushStack(vermd);
585: }
586: // New index
587: else if (localName.equals("index")) {
588: MetaData md = getStack();
589: IndexMetaData idxmd = new IndexMetaData(getAttr(attrs,
590: "name"), getAttr(attrs, "table"), getAttr(
591: attrs, "unique"));
592: if (md instanceof AbstractClassMetaData) {
593: AbstractClassMetaData cmd = (AbstractClassMetaData) md;
594: cmd.addIndex(idxmd);
595: } else if (md instanceof AbstractMemberMetaData) {
596: AbstractMemberMetaData fmd = (AbstractMemberMetaData) md;
597: fmd.setIndexMetaData(idxmd);
598: } else if (md instanceof JoinMetaData) {
599: JoinMetaData jmd = (JoinMetaData) md;
600: jmd.setIndexMetaData(idxmd);
601: } else if (md instanceof AbstractElementMetaData) {
602: AbstractElementMetaData elmd = (AbstractElementMetaData) md;
603: elmd.setIndexMetaData(idxmd);
604: } else if (md instanceof OrderMetaData) {
605: OrderMetaData omd = (OrderMetaData) md;
606: omd.setIndexMetaData(idxmd);
607: } else if (md instanceof VersionMetaData) {
608: VersionMetaData vermd = (VersionMetaData) md;
609: vermd.setIndexMetaData(idxmd);
610: } else if (md instanceof DiscriminatorMetaData) {
611: DiscriminatorMetaData dismd = (DiscriminatorMetaData) md;
612: dismd.setIndexMetaData(idxmd);
613: }
614: pushStack(idxmd);
615: }
616: // New unique constraint
617: else if (localName.equals("unique")) {
618: MetaData md = getStack();
619: UniqueMetaData unimd = new UniqueMetaData(getAttr(
620: attrs, "name"), getAttr(attrs, "table"),
621: getAttr(attrs, "deferred"));
622: if (md instanceof AbstractClassMetaData) {
623: AbstractClassMetaData cmd = (AbstractClassMetaData) md;
624: cmd.addUniqueConstraint(unimd);
625: } else if (md instanceof AbstractMemberMetaData) {
626: AbstractMemberMetaData fmd = (AbstractMemberMetaData) md;
627: fmd.setUniqueMetaData(unimd);
628: } else if (md instanceof JoinMetaData) {
629: JoinMetaData jmd = (JoinMetaData) md;
630: jmd.setUniqueMetaData(unimd);
631: } else if (md instanceof AbstractElementMetaData) {
632: AbstractElementMetaData elmd = (AbstractElementMetaData) md;
633: elmd.setUniqueMetaData(unimd);
634: }
635: pushStack(unimd);
636: }
637: // New foreign key
638: else if (localName.equals("foreign-key")) {
639: MetaData md = getStack();
640: ForeignKeyMetaData fkmd = new ForeignKeyMetaData(
641: getAttr(attrs, "name"),
642: getAttr(attrs, "table"), getAttr(attrs,
643: "unique"), getAttr(attrs, "deferred"),
644: getAttr(attrs, "delete-action"), getAttr(attrs,
645: "update-action"));
646: if (md instanceof AbstractClassMetaData) {
647: AbstractClassMetaData cmd = (AbstractClassMetaData) md;
648: cmd.addForeignKey(fkmd);
649: } else if (md instanceof AbstractMemberMetaData) {
650: AbstractMemberMetaData fmd = (AbstractMemberMetaData) md;
651: fmd.setForeignKeyMetaData(fkmd);
652: } else if (md instanceof JoinMetaData) {
653: JoinMetaData jmd = (JoinMetaData) md;
654: jmd.setForeignKeyMetaData(fkmd);
655: } else if (md instanceof AbstractElementMetaData) {
656: AbstractElementMetaData elmd = (AbstractElementMetaData) md;
657: elmd.setForeignKeyMetaData(fkmd);
658: }
659: pushStack(fkmd);
660: }
661: // New order
662: else if (localName.equals("order")) {
663: OrderMetaData ormd = new OrderMetaData(getAttr(attrs,
664: "column"), getAttr(attrs, "indexed"), getAttr(
665: attrs, "mapped-by"));
666: AbstractMemberMetaData fmd = (AbstractMemberMetaData) getStack();
667: fmd.setOrderMetaData(ormd);
668: pushStack(ormd);
669: }
670: // Embedded
671: else if (localName.equals("embedded")) {
672: MetaData md = getStack();
673: EmbeddedMetaData emd = new EmbeddedMetaData(md,
674: getAttr(attrs, "owner-field"), getAttr(attrs,
675: "null-indicator-column"), getAttr(
676: attrs, "null-indicator-value"));
677: if (md instanceof AbstractMemberMetaData) {
678: AbstractMemberMetaData fmd = (AbstractMemberMetaData) md;
679: fmd.setEmbeddedMetaData(emd);
680: } else if (md instanceof KeyMetaData) {
681: KeyMetaData kmd = (KeyMetaData) md;
682: kmd.setEmbeddedMetaData(emd);
683: } else if (md instanceof ValueMetaData) {
684: ValueMetaData vmd = (ValueMetaData) md;
685: vmd.setEmbeddedMetaData(emd);
686: } else if (md instanceof ElementMetaData) {
687: ElementMetaData elmd = (ElementMetaData) md;
688: elmd.setEmbeddedMetaData(emd);
689: }
690: pushStack(emd);
691: } else {
692: String message = LOCALISER.msg("044037", qName);
693: JPOXLogger.METADATA.error(message);
694: throw new RuntimeException(message);
695: }
696: } catch (RuntimeException ex) {
697: JPOXLogger.METADATA.error(LOCALISER.msg("044042", qName,
698: getStack(), uri), ex);
699: throw ex;
700: }
701: }
702:
703: /**
704: * Handler method called at the end of an element.
705: * @param uri URI of the tag
706: * @param localName local name
707: * @param qName Name of element just ending
708: * @throws SAXException in parsing errors
709: */
710: public void endElement(String uri, String localName, String qName)
711: throws SAXException {
712: if (JPOXLogger.METADATA.isDebugEnabled()) {
713: JPOXLogger.METADATA.debug(LOCALISER.msg("044035", "<"
714: + qName + ">", "" + stack.size()));
715: }
716: if (localName.length() < 1) {
717: localName = qName;
718: }
719:
720: // Save the current string for elements that have a body value
721: String currentString = getString().trim();
722: if (currentString.length() > 0) {
723: MetaData md = getStack();
724: if (localName.equals("query")) {
725: ((QueryMetaData) md).setQuery(currentString);
726: }
727: }
728:
729: // Pop the tag
730: // If startElement pushes an element onto the stack need a remove here for that type
731: if (localName.equals("package")
732: || localName.equals("fetch-plan")
733: || localName.equals("class")
734: || localName.equals("interface")
735: || localName.equals("implements")
736: || localName.equals("property")
737: || localName.equals("datastore-identity")
738: || localName.equals("inheritance")
739: || localName.equals("primary-key")
740: || localName.equals("version")
741: || localName.equals("unmapped")
742: || localName.equals("query")
743: || localName.equals("sequence")
744: || localName.equals("field") || localName.equals("map")
745: || localName.equals("element")
746: || localName.equals("embedded")
747: || localName.equals("key") || localName.equals("value")
748: || localName.equals("array")
749: || localName.equals("collection")
750: || localName.equals("join")
751: || localName.equals("index")
752: || localName.equals("unique")
753: || localName.equals("foreign-key")
754: || localName.equals("order")
755: || localName.equals("fetch-group")
756: || localName.equals("column")
757: || localName.equals("discriminator")) {
758: popStack();
759: }
760: }
761: }
|