svnno****@sourc*****
svnno****@sourc*****
2009年 1月 16日 (金) 01:33:47 JST
Revision: 2426 http://svn.sourceforge.jp/view?root=jiemamy&view=rev&rev=2426 Author: daisuke_m Date: 2009-01-16 01:33:47 +0900 (Fri, 16 Jan 2009) Log Message: ----------- DomSerializerが8割完成。 Modified Paths: -------------- artemis/trunk/org.jiemamy.core/src/main/java/org/jiemamy/serializer/DomBuilder.java zeus/trunk/org.jiemamy.spec.core/src/main/java/org/jiemamy/JiemamyFactory.java zeus/trunk/org.jiemamy.spec.core/src/main/java/org/jiemamy/xml/Nodes.java -------------- next part -------------- Modified: artemis/trunk/org.jiemamy.core/src/main/java/org/jiemamy/serializer/DomBuilder.java =================================================================== --- artemis/trunk/org.jiemamy.core/src/main/java/org/jiemamy/serializer/DomBuilder.java 2009-01-15 15:31:33 UTC (rev 2425) +++ artemis/trunk/org.jiemamy.core/src/main/java/org/jiemamy/serializer/DomBuilder.java 2009-01-15 16:33:47 UTC (rev 2426) @@ -18,12 +18,17 @@ */ package org.jiemamy.serializer; +import static org.jiemamy.xml.Nodes.ADAPTER; import static org.jiemamy.xml.Nodes.BEGIN_SCRIPT; import static org.jiemamy.xml.Nodes.COLUMN; +import static org.jiemamy.xml.Nodes.COLUMNS; import static org.jiemamy.xml.Nodes.CONNECTIONS; +import static org.jiemamy.xml.Nodes.CONSTRAINT_COLUMN; import static org.jiemamy.xml.Nodes.CONTENTS; import static org.jiemamy.xml.Nodes.DATA_SET; +import static org.jiemamy.xml.Nodes.DATA_TYPE; import static org.jiemamy.xml.Nodes.DEFAULT_VALUE; +import static org.jiemamy.xml.Nodes.DEFERRABLE; import static org.jiemamy.xml.Nodes.DEFINITION; import static org.jiemamy.xml.Nodes.DESCRIPTION; import static org.jiemamy.xml.Nodes.DIAGRAM_PRESENTATIONS; @@ -33,20 +38,31 @@ import static org.jiemamy.xml.Nodes.END_SCRIPT; import static org.jiemamy.xml.Nodes.FOREIGN_KEY; import static org.jiemamy.xml.Nodes.FREE_STRING; +import static org.jiemamy.xml.Nodes.INITIALLY_CHECK_TIME; import static org.jiemamy.xml.Nodes.INSERT_DATA_SET; import static org.jiemamy.xml.Nodes.LOGICAL_NAME; +import static org.jiemamy.xml.Nodes.MAPPING; +import static org.jiemamy.xml.Nodes.MAPPINGS; +import static org.jiemamy.xml.Nodes.MATCH_TYPE; import static org.jiemamy.xml.Nodes.NAME; import static org.jiemamy.xml.Nodes.NODES; import static org.jiemamy.xml.Nodes.NOT_NULL; +import static org.jiemamy.xml.Nodes.ON_DELETE; +import static org.jiemamy.xml.Nodes.ON_UPDATE; import static org.jiemamy.xml.Nodes.RECORD; +import static org.jiemamy.xml.Nodes.REFERENCE_COLUMN; +import static org.jiemamy.xml.Nodes.REPRESENTATION; import static org.jiemamy.xml.Nodes.ROOT_MODEL; import static org.jiemamy.xml.Nodes.SCHEMA_NAME; +import static org.jiemamy.xml.Nodes.SOURCE; import static org.jiemamy.xml.Nodes.SOURCE_CONNECTIONS; import static org.jiemamy.xml.Nodes.STICKY; import static org.jiemamy.xml.Nodes.TABLE; +import static org.jiemamy.xml.Nodes.TARGET; import static org.jiemamy.xml.Nodes.TARGET_CONNECTIONS; import static org.jiemamy.xml.Nodes.VIEW; +import java.lang.reflect.Field; import java.util.List; import java.util.Map; @@ -55,6 +71,9 @@ import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; +import org.apache.commons.lang.ObjectUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; @@ -62,9 +81,11 @@ import org.jiemamy.model.RootModel; import org.jiemamy.model.attribute.ColumnModel; import org.jiemamy.model.connection.AbstractConnectionModel; +import org.jiemamy.model.connection.ForeignKeyMapping; import org.jiemamy.model.connection.ForeignKeyModel; import org.jiemamy.model.dataset.InsertDataSetModel; import org.jiemamy.model.dataset.RecordModel; +import org.jiemamy.model.datatype.DataType; import org.jiemamy.model.datatype.DomainModel; import org.jiemamy.model.node.AbstractEntityModel; import org.jiemamy.model.node.AbstractNodeModel; @@ -80,18 +101,57 @@ */ class DomBuilder { + private static Logger logger = LoggerFactory.getLogger(DomBuilder.class); + + private static Element newChild(Element parentElement, QName qName) { Element childElement = newElement(parentElement.getOwnerDocument(), qName); parentElement.appendChild(childElement); return childElement; } + private static Element newChild(Element parentElement, QName qName, Enum<?> e) { + Element childElement = newChild(parentElement, qName); + if (e != null) { + setText(childElement, e.name()); + } + return childElement; + } + private static Element newChild(Element parentElement, QName qName, String text) { Element childElement = newChild(parentElement, qName); setText(childElement, text); return childElement; } + private static Element newDataTypeElement(Element parentElement, DataType dataType) { + Element dataTypeElement = newChild(parentElement, DATA_TYPE); + if (dataType instanceof DomainModel) { + DomainModel domainModel = (DomainModel) dataType; + dataTypeElement.setAttribute("ref", domainModel.getId().toString()); + } else { + for (Object adapter : dataType.getAdapters()) { + Element adapterElement = newChild(dataTypeElement, ADAPTER); + adapterElement.setAttribute("class", adapter.getClass().getName()); + for (Field field : adapter.getClass().getDeclaredFields()) { + String value = null; + try { + field.setAccessible(true); + value = ObjectUtils.toString(field.get(adapter)); + } catch (IllegalArgumentException e) { + logger.error(e.getMessage()); + throw new UnexpectedConditionError("フィールドは必ず存在するはず", e); + } catch (IllegalAccessException e) { + // THINK ignore? + logger.warn(e.getMessage()); + } + newChild(adapterElement, new QName(Namespaces.CORE.namespaceURI, field.getName()), value); + } + } + } + return dataTypeElement; + } + private static Element newElement(Document document, QName qName) { Element e = document.createElement(qName.getLocalPart()); // TODO qName.getNamespaceURI()に対応するprefixを付加する @@ -119,9 +179,9 @@ } /** - * TODO for daisuke + * DOM Documentを構築する。 * - * @return + * @return DOM Document * @throws ParserConfigurationException */ public Document build() throws ParserConfigurationException { @@ -130,18 +190,18 @@ DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); Document document = documentBuilder.newDocument(); - Element root = newElement(document, ROOT_MODEL); - root.setAttribute("id", rootModel.getId().toString()); - root.setAttribute("xmlns", Namespaces.CORE.namespaceURI); - root.setAttribute("xmlns:" + Namespaces.VIEW.prefix, Namespaces.VIEW.namespaceURI); - document.appendChild(root); + Element rootElement = newElement(document, ROOT_MODEL); + rootElement.setAttribute("id", rootModel.getId().toString()); + rootElement.setAttribute("xmlns", Namespaces.CORE.namespaceURI); + rootElement.setAttribute("xmlns:" + Namespaces.VIEW.prefix, Namespaces.VIEW.namespaceURI); + document.appendChild(rootElement); - buildMiscSection(root); - buildDomainSection(root); - buildNodeSection(root); - buildConnectionSection(root); - buildDataSection(root); - buildPresentationSection(root); + buildMiscSection(rootElement); + buildDomainSection(rootElement); + buildNodeSection(rootElement); + buildConnectionSection(rootElement); + buildDataSection(rootElement); + buildPresentationSection(rootElement); return document; } @@ -151,8 +211,30 @@ for (AbstractConnectionModel connectionModel : rootModel.getConnections()) { Element connectionElement; if (connectionModel instanceof ForeignKeyModel) { + ForeignKeyModel foreignKeyModel = (ForeignKeyModel) connectionModel; connectionElement = newChild(connectionsElement, FOREIGN_KEY); - // UNDONE + connectionElement.setAttribute("id", foreignKeyModel.getId().toString()); + newChild(connectionElement, NAME, foreignKeyModel.getName()); + newChild(connectionElement, LOGICAL_NAME, foreignKeyModel.getLogicalName()); + newChild(connectionElement, SOURCE).setAttribute("ref", foreignKeyModel.getSource().getId().toString()); + newChild(connectionElement, TARGET).setAttribute("ref", foreignKeyModel.getTarget().getId().toString()); + + Element mappingsElement = newChild(connectionElement, MAPPINGS); + for (ForeignKeyMapping mapping : foreignKeyModel.getMappings()) { + Element mappingElement = newChild(mappingsElement, MAPPING); +// mappingElement.setAttribute("id", mapping.getId().toString()); + newChild(mappingElement, CONSTRAINT_COLUMN).setAttribute("ref", + mapping.getConstraintColumn().getId().toString()); + newChild(mappingElement, REFERENCE_COLUMN).setAttribute("ref", + mapping.getReferenceColumn().getId().toString()); + } + + newChild(connectionElement, MATCH_TYPE, foreignKeyModel.getMatchType()); + newChild(connectionElement, ON_DELETE, foreignKeyModel.getOnDelete()); + newChild(connectionElement, ON_UPDATE, foreignKeyModel.getOnUpdate()); + newChild(connectionElement, DEFERRABLE, Boolean.toString(foreignKeyModel.isDeferrable())); + newChild(connectionElement, INITIALLY_CHECK_TIME, foreignKeyModel.getInitiallyCheckTime()); + newChild(connectionElement, DESCRIPTION, foreignKeyModel.getDescription()); } else { throw new UnexpectedConditionError("unknown connection:" + connectionModel.getClass().getName()); } @@ -164,7 +246,7 @@ Element insertDataSetElement = newChild(rootElement, INSERT_DATA_SET); for (InsertDataSetModel insertDataSetModel : rootModel.getInsertDataSets()) { Element insertDataSet = newChild(insertDataSetElement, DATA_SET); -// insertDataSet.setAttribute("id", insertDataSetModel.getId()); +// insertDataSet.setAttribute("id", insertDataSetModel.getId().toString()); newChild(insertDataSet, NAME, insertDataSetModel.getName()); for (Map.Entry<AbstractEntityModel, List<RecordModel>> entry : insertDataSetModel.getRecords().entrySet()) { AbstractEntityModel entity = entry.getKey(); @@ -189,7 +271,7 @@ domainElement.setAttribute("id", domainModel.getId().toString()); newChild(domainElement, NAME, domainModel.getName()); newChild(domainElement, LOGICAL_NAME, domainModel.getLogicalName()); - // TODO dataType + newDataTypeElement(domainElement, domainModel.getDataType()); newChild(domainElement, DEFAULT_VALUE, domainModel.getDefaultValue()); newChild(domainElement, NOT_NULL, Boolean.toString(domainModel.isNotNull())); newChild(domainElement, FREE_STRING, domainModel.getFreeString()); @@ -228,7 +310,21 @@ newChild(nodeElement, BEGIN_SCRIPT, tableModel.getBeginScript()); newChild(nodeElement, END_SCRIPT, tableModel.getEndScript()); newChild(nodeElement, DESCRIPTION, tableModel.getDescription()); - // UNDONE columns + + Element columnsElement = newChild(nodeElement, COLUMNS); + for (ColumnModel columnModel : tableModel.getColumns()) { + Element columnElement = newChild(columnsElement, COLUMN); + columnElement.setAttribute("id", columnModel.getId().toString()); + newChild(columnElement, NAME, columnModel.getName()); + newChild(columnElement, LOGICAL_NAME, columnModel.getLogicalName()); + newDataTypeElement(columnElement, columnModel.getDataType()); + + newChild(columnElement, DEFAULT_VALUE, columnModel.getDefaultValue()); + newChild(columnElement, NOT_NULL, Boolean.toString(columnModel.isNotNull())); + newChild(columnElement, FREE_STRING, columnModel.getFreeString()); + newChild(columnElement, DESCRIPTION, columnModel.getDescription()); + newChild(columnElement, REPRESENTATION, Boolean.toString(columnModel.isRepresentation())); + } } else if (nodeModel instanceof ViewModel) { ViewModel viewModel = (ViewModel) nodeModel; nodeElement = newChild(nodesElement, VIEW); @@ -253,5 +349,6 @@ private void buildPresentationSection(Element rootElement) { // TODO view:diagramPresentations Element diagramPresentationsElement = newChild(rootElement, DIAGRAM_PRESENTATIONS); +// rootModel.getAdapter(diagramPresentations.class); // FIXME だーー。アクセスできねえじゃん。 } } Modified: zeus/trunk/org.jiemamy.spec.core/src/main/java/org/jiemamy/JiemamyFactory.java =================================================================== --- zeus/trunk/org.jiemamy.spec.core/src/main/java/org/jiemamy/JiemamyFactory.java 2009-01-15 15:31:33 UTC (rev 2425) +++ zeus/trunk/org.jiemamy.spec.core/src/main/java/org/jiemamy/JiemamyFactory.java 2009-01-15 16:33:47 UTC (rev 2426) @@ -18,6 +18,7 @@ */ package org.jiemamy; +import java.util.Set; import java.util.UUID; import org.jiemamy.model.RootModel; @@ -40,6 +41,12 @@ JiemamyImplementation getImplementation(); /** + * 追加仕様のサポート状態を取得する。 + * @return サポートされている追加仕様の{@link Set} + */ + Set<OptionalSpec> getSupportedSpecs(); + + /** * 新しいモデルビルダを生成する。 * @param <M> モデルの型 * @param <B> モデルビルダの型 Modified: zeus/trunk/org.jiemamy.spec.core/src/main/java/org/jiemamy/xml/Nodes.java =================================================================== --- zeus/trunk/org.jiemamy.spec.core/src/main/java/org/jiemamy/xml/Nodes.java 2009-01-15 15:31:33 UTC (rev 2425) +++ zeus/trunk/org.jiemamy.spec.core/src/main/java/org/jiemamy/xml/Nodes.java 2009-01-15 16:33:47 UTC (rev 2426) @@ -48,8 +48,10 @@ public static final QName LOGICAL_NAME = new QName(Namespaces.CORE.namespaceURI, "logicalName"); - public static final QName DATATYPE_DESC = new QName(Namespaces.CORE.namespaceURI, "dataType"); + public static final QName DATA_TYPE = new QName(Namespaces.CORE.namespaceURI, "dataType"); + public static final QName ADAPTER = new QName(Namespaces.CORE.namespaceURI, "adapter"); + public static final QName DEFAULT_VALUE = new QName(Namespaces.CORE.namespaceURI, "defaultValue"); public static final QName NOT_NULL = new QName(Namespaces.CORE.namespaceURI, "notNull"); @@ -102,6 +104,8 @@ public static final QName REFERENCE_COLUMN = new QName(Namespaces.CORE.namespaceURI, "referenceColumn"); + public static final QName MATCH_TYPE = new QName(Namespaces.CORE.namespaceURI, "matchType"); + public static final QName ON_DELETE = new QName(Namespaces.CORE.namespaceURI, "onDelete"); public static final QName ON_UPDATE = new QName(Namespaces.CORE.namespaceURI, "onUpdate");