?? xmlparser.java
字號:
/*
* Copyright 2005-2007 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.sf.dozer.util.mapping.util;
import java.io.IOException;
import java.io.InputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import net.sf.dozer.util.mapping.converters.CustomConverterContainer;
import net.sf.dozer.util.mapping.converters.CustomConverterDescription;
import net.sf.dozer.util.mapping.fieldmap.ClassMap;
import net.sf.dozer.util.mapping.fieldmap.Configuration;
import net.sf.dozer.util.mapping.fieldmap.CopyByReference;
import net.sf.dozer.util.mapping.fieldmap.CopyByReferenceContainer;
import net.sf.dozer.util.mapping.fieldmap.DozerClass;
import net.sf.dozer.util.mapping.fieldmap.ExcludeFieldMap;
import net.sf.dozer.util.mapping.fieldmap.Field;
import net.sf.dozer.util.mapping.fieldmap.FieldMap;
import net.sf.dozer.util.mapping.fieldmap.GenericFieldMap;
import net.sf.dozer.util.mapping.fieldmap.Hint;
import net.sf.dozer.util.mapping.fieldmap.Mappings;
import org.apache.commons.lang.BooleanUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;
/**
* @author garsombke.franz
*/
public class XMLParser implements MapperConstants {
private static final Logger log = Logger.getLogger(XMLParser.class);
private final Mappings mappings = new Mappings();
public Mappings parse(InputStream inputSource) throws SAXException, ParserConfigurationException, IOException,
ClassNotFoundException {
DocumentBuilderFactory factory = createDocumentBuilderFactory();
DocumentBuilder builder = createDocumentBuilder(factory);
Document document = builder.parse(inputSource);
Element theRoot = document.getDocumentElement();
NodeList nl = theRoot.getChildNodes();
for (int i = 0; i < nl.getLength(); i++) {
Node node = nl.item(i);
if (node instanceof Element) {
Element ele = (Element) node;
log.info("name: " + ele.getNodeName());
if (CONFIGURATION_ELEMENT.equals(ele.getNodeName())) {
parseConfiguration(ele);
} else if (MAPPING_ELEMENT.equals(ele.getNodeName())) {
parseMapping(ele);
}
}
}
return mappings;
}
private void parseMapping(Element ele) throws ClassNotFoundException {
ClassMap classMap = new ClassMap();
mappings.getMapping().add(classMap);
if (StringUtils.isNotEmpty(ele.getAttribute(DATE_FORMAT_ATTRIBUTE))) {
classMap.setDateFormat(ele.getAttribute(DATE_FORMAT_ATTRIBUTE));
}
if (StringUtils.isNotEmpty(ele.getAttribute(MAP_NULL_ATTRIBUTE))) {
classMap.setMapNull(BooleanUtils.toBoolean(ele.getAttribute(MAP_NULL_ATTRIBUTE)));
}
if (StringUtils.isNotEmpty(ele.getAttribute(MAP_EMPTY_STRING_ATTRIBUTE))) {
classMap.setMapEmptyString(BooleanUtils.toBoolean(ele.getAttribute(MAP_EMPTY_STRING_ATTRIBUTE)));
}
if (StringUtils.isNotEmpty(ele.getAttribute(BEAN_FACTORY_ATTRIBUTE))) {
classMap.setBeanFactory(ele.getAttribute(BEAN_FACTORY_ATTRIBUTE));
}
if (StringUtils.isNotEmpty(ele.getAttribute(WILDCARD_ATTRIBUTE))) {
classMap.setWildcard(BooleanUtils.toBoolean(ele.getAttribute(WILDCARD_ATTRIBUTE)));
}
if (StringUtils.isNotEmpty(ele.getAttribute(STOP_ON_ERRORS_ATTRIBUTE))) {
classMap.setStopOnErrors(BooleanUtils.toBoolean(ele.getAttribute(STOP_ON_ERRORS_ATTRIBUTE)));
}
if (StringUtils.isNotEmpty(ele.getAttribute(MAPID_ATTRIBUTE))) {
classMap.setMapId(ele.getAttribute(MAPID_ATTRIBUTE));
}
if (StringUtils.isNotEmpty(ele.getAttribute(TYPE_ATTRIBUTE))) {
classMap.setType(ele.getAttribute(TYPE_ATTRIBUTE));
}
if (StringUtils.isNotEmpty(ele.getAttribute(IS_ACCESSIBLE_ATTRIBUTE))) {
classMap.setAccessible(BooleanUtils.toBoolean(ele.getAttribute(IS_ACCESSIBLE_ATTRIBUTE)));
}
NodeList nl = ele.getChildNodes();
for (int i = 0; i < nl.getLength(); i++) {
Node node = nl.item(i);
if (node instanceof Element) {
Element element = (Element) node;
log.info("config name: " + element.getNodeName());
log.info(" value: " + element.getFirstChild().getNodeValue());
if (CLASS_A_ELEMENT.equals(element.getNodeName())) {
DozerClass source = new DozerClass();
source.setName(element.getFirstChild().getNodeValue().trim());
if (StringUtils.isNotEmpty(element.getAttribute(MAP_GET_METHOD_ATTRIBUTE))) {
source.setMapGetMethod(element.getAttribute(MAP_GET_METHOD_ATTRIBUTE));
}
if (StringUtils.isNotEmpty(element.getAttribute(MAP_SET_METHOD_ATTRIBUTE))) {
source.setMapSetMethod(element.getAttribute(MAP_SET_METHOD_ATTRIBUTE));
}
if (StringUtils.isNotEmpty(element.getAttribute(BEAN_FACTORY_ATTRIBUTE))) {
source.setBeanFactory(element.getAttribute(BEAN_FACTORY_ATTRIBUTE));
}
if (StringUtils.isNotEmpty(element.getAttribute(FACTORY_BEANID_ATTRIBUTE))) {
source.setFactoryBeanId(element.getAttribute(FACTORY_BEANID_ATTRIBUTE));
}
if (StringUtils.isNotEmpty(element.getAttribute(CREATE_METHOD_ATTRIBUTE))) {
source.setCreateMethod(element.getAttribute(CREATE_METHOD_ATTRIBUTE));
}
if (StringUtils.isNotEmpty(element.getAttribute(MAP_NULL_ATTRIBUTE))) {
source.setMapNull(Boolean.valueOf(element.getAttribute(MAP_NULL_ATTRIBUTE)));
}
if (StringUtils.isNotEmpty(element.getAttribute(MAP_EMPTY_STRING_ATTRIBUTE))) {
source.setMapEmptyString(Boolean.valueOf(element.getAttribute(MAP_EMPTY_STRING_ATTRIBUTE)));
}
classMap.setSourceClass(source);
}
if (CLASS_B_ELEMENT.equals(element.getNodeName())) {
DozerClass dest = new DozerClass();
dest.setName(element.getFirstChild().getNodeValue().trim());
if (StringUtils.isNotEmpty(element.getAttribute(MAP_GET_METHOD_ATTRIBUTE))) {
dest.setMapGetMethod(element.getAttribute(MAP_GET_METHOD_ATTRIBUTE));
}
if (StringUtils.isNotEmpty(element.getAttribute(MAP_SET_METHOD_ATTRIBUTE))) {
dest.setMapSetMethod(element.getAttribute(MAP_SET_METHOD_ATTRIBUTE));
}
if (StringUtils.isNotEmpty(element.getAttribute(BEAN_FACTORY_ATTRIBUTE))) {
dest.setBeanFactory(element.getAttribute(BEAN_FACTORY_ATTRIBUTE));
}
if (StringUtils.isNotEmpty(element.getAttribute(FACTORY_BEANID_ATTRIBUTE))) {
dest.setFactoryBeanId(element.getAttribute(FACTORY_BEANID_ATTRIBUTE));
}
if (StringUtils.isNotEmpty(element.getAttribute(CREATE_METHOD_ATTRIBUTE))) {
dest.setCreateMethod(element.getAttribute(CREATE_METHOD_ATTRIBUTE));
}
if (StringUtils.isNotEmpty(element.getAttribute(MAP_NULL_ATTRIBUTE))) {
dest.setMapNull(Boolean.valueOf(element.getAttribute(MAP_NULL_ATTRIBUTE)));
}
if (StringUtils.isNotEmpty(element.getAttribute(MAP_EMPTY_STRING_ATTRIBUTE))) {
dest.setMapEmptyString(Boolean.valueOf(element.getAttribute(MAP_EMPTY_STRING_ATTRIBUTE)));
}
classMap.setDestClass(dest);
}
if (FIELD_ELEMENT.equals(element.getNodeName())) {
parseGenericFieldMap(element, classMap);
} else if (FIELD_EXCLUDE_ELEMENT.equals(element.getNodeName())) {
parseFieldExcludeMap(element, classMap);
}
}
}
}
private void parseFieldExcludeMap(Element ele, ClassMap classMap) {
ExcludeFieldMap efm = new ExcludeFieldMap();
if (StringUtils.isNotEmpty(ele.getAttribute(TYPE_ATTRIBUTE))) {
efm.setType(ele.getAttribute(TYPE_ATTRIBUTE));
}
classMap.addFieldMapping(efm);
NodeList nl = ele.getChildNodes();
for (int i = 0; i < nl.getLength(); i++) {
Node node = nl.item(i);
if (node instanceof Element) {
Element element = (Element) node;
log.info("config name: " + element.getNodeName());
log.info(" value: " + element.getFirstChild().getNodeValue());
parseFieldElements(element, efm);
}
}
}
private void parseFieldElements(Element element, FieldMap fieldMap) {
if (A_ELEMENT.equals(element.getNodeName())) {
fieldMap.setSourceField(parseField(element));
}
if (B_ELEMENT.equals(element.getNodeName())) {
fieldMap.setDestField(parseField(element));
}
}
private void parseGenericFieldMap(Element ele, ClassMap classMap) {
GenericFieldMap gfm = new GenericFieldMap();
classMap.addFieldMapping(gfm);
if (StringUtils.isNotEmpty(ele.getAttribute(COPY_BY_REFERENCE_ATTRIBUTE))) {
gfm.setCopyByReference(BooleanUtils.toBoolean(ele.getAttribute(COPY_BY_REFERENCE_ATTRIBUTE)));
}
if (StringUtils.isNotEmpty(ele.getAttribute(MAPID_ATTRIBUTE))) {
gfm.setMapId(ele.getAttribute(MAPID_ATTRIBUTE));
}
if (StringUtils.isNotEmpty(ele.getAttribute(TYPE_ATTRIBUTE))) {
gfm.setType(ele.getAttribute(TYPE_ATTRIBUTE));
}
parseFieldMap(ele, gfm);
}
private void parseFieldMap(Element ele, GenericFieldMap fieldMap) {
if (StringUtils.isNotEmpty(ele.getAttribute(RELATIONSHIP_TYPE_ATTRIBUTE))) {
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -