001: /*
002: * Copyright 2007 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.tm;
017:
018: import org.outerj.daisy.repository.Repository;
019: import org.outerj.daisy.repository.RepositoryException;
020: import org.outerj.daisy.repository.schema.FieldTypeNotFoundException;
021: import org.outerj.daisy.repository.schema.PartTypeNotFoundException;
022: import org.outerj.daisy.repository.schema.DocumentTypeNotFoundException;
023: import org.outerj.daisy.xmlutil.LocalXPathFactory;
024: import org.outerj.daisy.xmlutil.DocumentHelper;
025: import org.outerj.daisy.tools.importexport.ImportExportException;
026: import org.outerj.daisy.tools.importexport.tm.TMConfig;
027: import org.w3c.dom.Document;
028: import org.w3c.dom.Element;
029: import org.w3c.dom.NodeList;
030:
031: import javax.xml.parsers.DocumentBuilderFactory;
032: import javax.xml.xpath.*;
033: import java.io.InputStream;
034: import java.io.BufferedInputStream;
035: import java.io.File;
036: import java.io.FileInputStream;
037:
038: public class TMConfigFactory {
039: private static XPathExpression CROSS_LANG_PARTS_XPATH;
040: private static XPathExpression CROSS_LANG_FIELDS_XPATH;
041:
042: static {
043: try {
044: XPathFactory xpathFactory = LocalXPathFactory.get();
045: XPath xpath = xpathFactory.newXPath();
046:
047: CROSS_LANG_PARTS_XPATH = xpath
048: .compile("/tm-config/language-independent/part");
049: CROSS_LANG_FIELDS_XPATH = xpath
050: .compile("/tm-config/language-independent/field");
051:
052: } catch (XPathExpressionException e) {
053: throw new RuntimeException(
054: "Error initializing xpath expressions.", e);
055: }
056: }
057:
058: public static TMConfig parseFromXml(String fileName,
059: Repository repository) throws Exception {
060: File file = new File(fileName);
061: FileInputStream is = new FileInputStream(file);
062: try {
063: return parseFromXml(is, repository);
064: } finally {
065: is.close();
066: }
067: }
068:
069: public static TMConfig parseFromXml(InputStream is,
070: Repository repository) throws Exception {
071: DocumentBuilderFactory factory = DocumentBuilderFactory
072: .newInstance();
073: factory.setNamespaceAware(true);
074:
075: if (!(is instanceof BufferedInputStream))
076: is = new BufferedInputStream(is);
077:
078: Document document = factory.newDocumentBuilder().parse(is);
079:
080: TMConfig tmConfig = new TMConfig();
081:
082: // read cross-lang fields
083: {
084: NodeList nodeList = (NodeList) CROSS_LANG_FIELDS_XPATH
085: .evaluate(document, XPathConstants.NODESET);
086: for (int i = 0; i < nodeList.getLength(); i++) {
087: String type = DocumentHelper.getAttribute(
088: (Element) nodeList.item(i), "type", true);
089: String documentType = DocumentHelper.getAttribute(
090: (Element) nodeList.item(i), "documentType",
091: false);
092:
093: type = validateFieldTypeExists(type, repository);
094: if (documentType != null)
095: documentType = validateDocumentTypeExists(
096: documentType, repository);
097:
098: System.out.println("type = " + type);
099: tmConfig.addLangIndepField(documentType, type);
100: }
101: }
102:
103: // read cross-lang parts
104: {
105: NodeList nodeList = (NodeList) CROSS_LANG_PARTS_XPATH
106: .evaluate(document, XPathConstants.NODESET);
107: for (int i = 0; i < nodeList.getLength(); i++) {
108: String type = DocumentHelper.getAttribute(
109: (Element) nodeList.item(i), "type", true);
110: String documentType = DocumentHelper.getAttribute(
111: (Element) nodeList.item(i), "documentType",
112: false);
113:
114: type = validatePartTypeExists(type, repository);
115: if (documentType != null)
116: documentType = validateDocumentTypeExists(
117: documentType, repository);
118:
119: System.out.println("type = " + type);
120: tmConfig.addLangIndepPart(documentType, type);
121: }
122: }
123:
124: return tmConfig;
125: }
126:
127: private static String validateFieldTypeExists(String fieldType,
128: Repository repository) throws ImportExportException,
129: RepositoryException {
130: try {
131: return repository.getRepositorySchema().getFieldType(
132: fieldType, false).getName();
133: } catch (FieldTypeNotFoundException e) {
134: throw new ImportExportException(
135: "Field type specified in translation management config does not exist: "
136: + fieldType);
137: }
138: }
139:
140: private static String validatePartTypeExists(String partType,
141: Repository repository) throws ImportExportException,
142: RepositoryException {
143: try {
144: return repository.getRepositorySchema().getPartType(
145: partType, false).getName();
146: } catch (PartTypeNotFoundException e) {
147: throw new ImportExportException(
148: "Part type specified in translation management config does not exist: "
149: + partType);
150: }
151: }
152:
153: private static String validateDocumentTypeExists(
154: String documentType, Repository repository)
155: throws ImportExportException, RepositoryException {
156: try {
157: return repository.getRepositorySchema().getDocumentType(
158: documentType, false).getName();
159: } catch (DocumentTypeNotFoundException e) {
160: throw new ImportExportException(
161: "Document type specified in translation management config does not exist: "
162: + documentType);
163: }
164: }
165: }
|