001: /*
002: * Copyright 2004 Outerthought bvba and Schaubroeck nv
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.outerj.daisy.tools.importexport.import_.schema;
017:
018: import org.outerj.daisy.repository.ValueType;
019: import org.xml.sax.ContentHandler;
020: import org.xml.sax.SAXException;
021: import org.xml.sax.helpers.AttributesImpl;
022:
023: import java.util.List;
024: import java.util.ArrayList;
025:
026: public abstract class BaseSchemaLoadListener implements
027: SchemaLoadListener {
028: private List<LoadInfo> loadedFieldTypes = new ArrayList<LoadInfo>();
029: private List<LoadInfo> loadedPartTypes = new ArrayList<LoadInfo>();
030: private List<LoadInfo> loadedDocumentTypes = new ArrayList<LoadInfo>();
031:
032: public void conflictingFieldType(String fieldTypeName,
033: ValueType requiredType, ValueType foundType)
034: throws Exception {
035: throw new Exception("Conflicting field type for "
036: + fieldTypeName + ". Expected " + requiredType
037: + " but found " + foundType + ".");
038: }
039:
040: public void conflictingMultiValue(String fieldTypeName,
041: boolean needMultivalue, boolean foundMultivalue)
042: throws Exception {
043: throw new Exception(
044: "Conflicting multivalue setting for field type "
045: + fieldTypeName + ". Expected "
046: + needMultivalue + " but found "
047: + foundMultivalue + ".");
048: }
049:
050: public void conflictingHierarchical(String fieldTypeName,
051: boolean needHierarchical, boolean foundHierarchical)
052: throws Exception {
053: throw new Exception(
054: "Conflicting hierarchical setting for field type "
055: + fieldTypeName + ". Expected "
056: + needHierarchical + " but found "
057: + foundHierarchical + ".");
058: }
059:
060: public void fieldTypeLoaded(String fieldTypeName,
061: SchemaLoadResult result) {
062: loadedFieldTypes.add(new LoadInfo(fieldTypeName, result));
063: }
064:
065: public void partTypeLoaded(String partTypeName,
066: SchemaLoadResult result) {
067: loadedPartTypes.add(new LoadInfo(partTypeName, result));
068: }
069:
070: public void documentTypeLoaded(String documentTypeName,
071: SchemaLoadResult result) {
072: loadedDocumentTypes.add(new LoadInfo(documentTypeName, result));
073: }
074:
075: public List<LoadInfo> getLoadedFieldTypes() {
076: return loadedFieldTypes;
077: }
078:
079: public List<LoadInfo> getLoadedPartTypes() {
080: return loadedPartTypes;
081: }
082:
083: public List<LoadInfo> getLoadedDocumentTypes() {
084: return loadedDocumentTypes;
085: }
086:
087: public int count(List<LoadInfo> list, SchemaLoadResult result) {
088: int count = 0;
089: for (LoadInfo loadInfo : list) {
090: if (loadInfo.getResult() == result)
091: count++;
092: }
093: return count;
094: }
095:
096: public void done() {
097: }
098:
099: public static class LoadInfo {
100: private String typeName;
101: private SchemaLoadResult result;
102:
103: public LoadInfo(String typeName, SchemaLoadResult result) {
104: this .typeName = typeName;
105: this .result = result;
106: }
107:
108: public String getTypeName() {
109: return typeName;
110: }
111:
112: public SchemaLoadResult getResult() {
113: return result;
114: }
115: }
116:
117: public void generateSaxFragment(ContentHandler contentHandler)
118: throws SAXException {
119: contentHandler.startElement("", SCHEMA_EL, SCHEMA_EL,
120: new AttributesImpl());
121: generateSaxFragment(loadedFieldTypes, "fieldType",
122: contentHandler);
123: generateSaxFragment(loadedPartTypes, "partType", contentHandler);
124: generateSaxFragment(loadedDocumentTypes, "documentType",
125: contentHandler);
126: contentHandler.endElement("", SCHEMA_EL, SCHEMA_EL);
127: }
128:
129: private void generateSaxFragment(List<LoadInfo> types,
130: String tagName, ContentHandler contentHandler)
131: throws SAXException {
132: for (LoadInfo info : types) {
133: AttributesImpl attrs = new AttributesImpl();
134: attrs.addAttribute("", "name", "name", "CDATA", info
135: .getTypeName());
136: attrs.addAttribute("", "result", "result", "CDATA", info
137: .getResult().toString());
138: contentHandler.startElement("", tagName, tagName, attrs);
139: contentHandler.endElement("", tagName, tagName);
140: }
141: }
142:
143: private static final String SCHEMA_EL = "schemaImportResult";
144: }
|