001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.openjpa.lib.meta;
020:
021: import java.util.Collection;
022: import java.util.Iterator;
023: import java.util.LinkedList;
024: import java.util.Map;
025: import java.util.LinkedHashMap;
026:
027: import org.xml.sax.SAXException;
028:
029: /**
030: * Helps serialize metadata objects to package and class elements.
031: *
032: * @author Abe White
033: * @nojavadoc
034: * @see CFMetaDataParser
035: */
036: public abstract class CFMetaDataSerializer extends
037: XMLMetaDataSerializer {
038:
039: private String _package = null;
040:
041: /**
042: * The default package for objects being serialized.
043: */
044: protected String getPackage() {
045: return _package;
046: }
047:
048: /**
049: * The default package for objects being serialized.
050: */
051: protected void setPackage(String pkg) {
052: _package = pkg;
053: }
054:
055: /**
056: * Helper method to group objects by package.
057: *
058: * @return mapping of package name to a collection of objects in
059: * that package
060: */
061: protected Map groupByPackage(Collection objs) throws SAXException {
062: Map packages = new LinkedHashMap();
063: String packageName;
064: Collection packageObjs;
065: Object obj;
066: for (Iterator itr = objs.iterator(); itr.hasNext();) {
067: obj = itr.next();
068: packageName = getPackage(obj);
069: packageObjs = (Collection) packages.get(packageName);
070: if (packageObjs == null) {
071: packageObjs = new LinkedList();
072: packages.put(packageName, packageObjs);
073: }
074: packageObjs.add(obj);
075: }
076: return packages;
077: }
078:
079: /**
080: * Return the package name of the given object, or null if not in a
081: * package. Used by {@link #groupByPackage}. Returns null by default.
082: */
083: protected String getPackage(Object obj) {
084: return null;
085: }
086:
087: /**
088: * Returns the given class name, stripping the package if it is not needed.
089: */
090: protected String getClassName(String name) {
091: // check if in current package; make sure not in a sub-package
092: if (_package != null
093: && name.lastIndexOf('.') == _package.length()
094: && name.startsWith(_package))
095: return name.substring(_package.length() + 1);
096:
097: // check other known packages
098: String[] packages = CFMetaDataParser.PACKAGES;
099: for (int i = 0; i < packages.length; i++)
100: if (name.startsWith(packages[i])
101: && name.lastIndexOf('.') == packages[i].length() - 1)
102: return name.substring(packages[i].length());
103: return name;
104: }
105: }
|