01: /*
02: * Copyright 2007 Bastian Schenke Licensed under the Apache License, Version 2.0 (the "License");
03: * you may not use this file except in compliance with the License.
04: * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
05: * Unless required by applicable law or agreed to in writing, software distributed under the
06: * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
07: * either express or implied. See the License for the specific language governing permissions
08: * and limitations under the License.
09: */
10: package nz.org.take.r2ml;
11:
12: import java.util.ArrayList;
13: import java.util.Collection;
14: import java.util.List;
15:
16: import javax.xml.bind.JAXBElement;
17:
18: import nz.org.take.Fact;
19: import nz.org.take.Prerequisite;
20: import nz.org.take.r2ml.util.R2MLUtil;
21: import de.tu_cottbus.r2ml.QfAndOrNafNegFormula;
22: import de.tu_cottbus.r2ml.QfConjunction;
23:
24: /**
25: * @author Bastian Schenke (bastian.schenke@googlemail.com)
26: *
27: */
28: public class QfConjunctionHandler implements XmlTypeHandler {
29:
30: /**
31: * Map a quantifier free conjunction from R2ML (QfConjunction) to a list of take
32: * prerequisites (List<Prerequisite>).
33: * If the disjunction still contains any formulas (nested conjunctions or negations) they will be flatened.
34: *
35: * @param obj
36: * a QfConjunction object
37: * @return a list of take prerequisites
38: *
39: * @see nz.org.take.r2ml.XmlTypeHandler#importObject(java.lang.Object,
40: * nz.org.take.r2ml.R2MLDriver)
41: */
42: @SuppressWarnings("unchecked")
43: public Object importObject(Object obj) throws R2MLException {
44:
45: R2MLDriver driver = R2MLDriver.get();
46: MappingContext context = MappingContext.get();
47: List<Prerequisite> prereqs = new ArrayList<Prerequisite>();
48:
49: try {
50: QfConjunction formula = (QfConjunction) obj;
51: for (JAXBElement<? extends QfAndOrNafNegFormula> item : formula
52: .getQfAndOrNafNegFormula()) {
53: XmlTypeHandler handler = driver
54: .getHandlerByXmlType(item.getValue().getClass());
55: Object takeArtefact = handler.importObject(item
56: .getValue());
57: if (takeArtefact instanceof Collection)
58: prereqs.addAll((List<Prerequisite>) takeArtefact);
59: else
60: prereqs.add(R2MLUtil
61: .factAsPrerequisite((Fact) takeArtefact));
62: }
63: } catch (ClassCastException e) {
64: //throw new R2MLException("Wrong XmlTypeHandler [" + handler.getClass() + "] for [" + obj.getClass() + "]", R2MLException.CLASS_CAST_ERROR);
65: }
66:
67: return prereqs;
68: }
69:
70: }
|