001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/metaobj/tags/sakai_2-4-1/metaobj-impl/api-impl/src/java/org/sakaiproject/metaobj/utils/xml/impl/FormatterElementType.java $
003: * $Id: FormatterElementType.java 14225 2006-09-05 17:39:44Z chmaurer@iupui.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2004, 2005, 2006 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.metaobj.utils.xml.impl;
021:
022: import java.text.Format;
023: import java.text.ParseException;
024:
025: import org.jdom.Element;
026: import org.jdom.Namespace;
027: import org.sakaiproject.metaobj.utils.xml.NormalizationException;
028: import org.sakaiproject.metaobj.utils.xml.SchemaNode;
029:
030: /**
031: * Created by IntelliJ IDEA.
032: * User: John Ellis
033: * Date: Apr 16, 2004
034: * Time: 2:48:13 PM
035: * To change this template use File | Settings | File Templates.
036: */
037: public abstract class FormatterElementType extends BaseElementType {
038:
039: public FormatterElementType(String typeName, Element schemaElement,
040: SchemaNode parentNode, Namespace xsdNamespace) {
041:
042: super (typeName, schemaElement, parentNode, xsdNamespace);
043: }
044:
045: protected abstract Format getFormatter();
046:
047: protected abstract Object checkConstraints(Object o);
048:
049: public String getSchemaNormalizedValue(Object value)
050: throws NormalizationException {
051:
052: if (value == null) {
053: return null;
054: }
055:
056: if (!getObjectType().isInstance(value)) {
057: throw new NormalizationException("Invalid object type",
058: NormalizationException.INVALID_TYPE_ERROR_CODE,
059: new Object[] { value, getObjectType() });
060: }
061:
062: value = checkConstraints(value);
063:
064: try {
065: return getFormatter().format(value);
066: } catch (Exception e) {
067: throw new NormalizationException(e);
068: }
069: }
070:
071: public String getSchemaNormalizedValue(String value)
072: throws NormalizationException {
073:
074: if (value == null) {
075: return null;
076: }
077:
078: try {
079: return getFormatter()
080: .format(
081: checkConstraints(getFormatter()
082: .parseObject(value)));
083: } catch (ParseException e) {
084: return parserException(value, e);
085: } catch (Exception e) {
086: throw new NormalizationException(e);
087: }
088: }
089:
090: protected Object getFormattedRestriction(Element restrictions,
091: String name, Namespace xsdNamespace) throws ParseException {
092: String value = processStringRestriction(restrictions, name,
093: xsdNamespace);
094:
095: if (value == null) {
096: return null;
097: }
098:
099: return getFormatter().parseObject(value);
100: }
101:
102: protected abstract String parserException(String value,
103: ParseException e);
104:
105: public Object getActualNormalizedValue(String value) {
106: try {
107: return checkConstraints(getFormatter().parseObject(value));
108: } catch (ParseException e) {
109: return parserException(value, e);
110: }
111: }
112:
113: public abstract Class getObjectType();
114:
115: }
|