001/**
002 * GeomXMLBinding -- An XML binding for working with GeomSS geometry objects.
003 *
004 * Copyright (C) 2013-2017, by Joseph A. Huwaldt. All rights reserved.
005 *
006 * This library is free software; you can redistribute it and/or modify it under the terms
007 * of the GNU Lesser General Public License as published by the Free Software Foundation;
008 * either version 2.1 of the License, or (at your option) any later version.
009 *
010 * This library is distributed in the hope that it will be useful, but WITHOUT ANY
011 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
012 * PARTICULAR PURPOSE. See the GNU Library General Public License for more details.
013 *
014 * You should have received a copy of the GNU Lesser General Public License along with
015 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place -
016 * Suite 330, Boston, MA 02111-1307, USA. Or visit: http://www.gnu.org/licenses/lgpl.html
017 */
018package geomss.geom;
019
020import geomss.geom.cst.BasicCSTCurve;
021import geomss.geom.cst.CSTClassFunction;
022import geomss.geom.cst.CSTCurveTrans;
023import geomss.geom.cst.CSTShapeFunction;
024import geomss.geom.nurbs.*;
025import jahuwaldt.js.param.Parameter;
026import java.awt.Color;
027import javolution.util.FastTable;
028import javolution.xml.XMLBinding;
029import javolution.xml.XMLFormat;
030import javolution.xml.XMLFormat.InputElement;
031import javolution.xml.stream.XMLStreamException;
032
033/**
034 * This class represents the binding between GeomSS Java classes and their XML
035 * representation (XMLFormat); the binding may be shared among multiple
036 * XMLObjectReader/XMLObjectWriter instances (is thread-safe). This binding has been
037 * customized to consistently represent GeomSS geometry objects in XML format for the
038 * purpose of defining a standard format.
039 *
040 * <p> Modified by: Joseph A. Huwaldt </p>
041 *
042 * @author Joseph A. Huwaldt, Date: June 19, 2013
043 * @version January 30, 2017
044 */
045public class GeomXMLBinding extends XMLBinding {
046
047    /**
048     * A custom XML format for java.awt.Color objects. The RGB values in the range 0-255
049     * for red, green, blue and alpha are stored in the attributes.
050     */
051    protected static final XMLFormat<Color> colorXML = new XMLFormat<Color>(null) {
052        @Override
053        public Color newInstance(Class<Color> cls, InputElement xml) throws XMLStreamException {
054            int red = xml.getAttribute("red", 0);
055            int green = xml.getAttribute("green", 0);
056            int blue = xml.getAttribute("blue", 0);
057            int alpha = xml.getAttribute("alpha", 255);
058            return new Color(red, green, blue, alpha);
059        }
060
061        @Override
062        public void read(XMLFormat.InputElement xml, Color obj) { /* Do nothing:  Color is immutable. */ }
063
064        @Override
065        public void write(Color obj, XMLFormat.OutputElement xml) throws XMLStreamException {
066            xml.setAttribute("red", obj.getRed());
067            xml.setAttribute("green", obj.getGreen());
068            xml.setAttribute("blue", obj.getBlue());
069            xml.setAttribute("alpha", obj.getAlpha());
070        }
071    };
072    private static final long serialVersionUID = 1L;
073
074    public GeomXMLBinding() {
075
076        setAlias(String.class, "String");
077        setAlias(Integer.class, "Integer");
078        setAlias(Long.class, "Long");
079        setAlias(Float.class, "Float");
080        setAlias(Double.class, "Double");
081        setAlias(org.jscience.mathematics.number.Float64.class, "Float64");
082        setAlias(Boolean.class, "Boolean");
083
084        setAlias(Parameter.class, "Parameter");
085        setAlias(GTransform.class, "GTransform");
086        setAlias(Point.class, "Point");
087        setAlias(GeomPointTrans.class, "GeomPointTrans");
088
089        setAlias(PointString.class, "PointString");
090        setAlias(PointArray.class, "PointArray");
091        setAlias(PointComponent.class, "PointComponent");
092        setAlias(PointVehicle.class, "PointVehicle");
093        setAlias(GeomList.class, "GeomList");
094
095        setAlias(Vector.class, "Vector");
096        setAlias(VectorTrans.class, "VectorTrans");
097
098        setAlias(Plane.class, "Plane");
099        setAlias(GeomPlaneTrans.class, "GeomPlaneTrans");
100
101        setAlias(LineSeg.class, "LineSeg");
102        setAlias(LineSegTrans.class, "LineSegTrans");
103        setAlias(BasicNurbsCurve.class, "BasicNurbsCurve");
104        setAlias(NurbsCurveTrans.class, "NurbsCurveTrans");
105        setAlias(ControlPoint.class, "ControlPoint");
106        setAlias(KnotVector.class, "KnotVector");
107        setAlias(CSTShapeFunction.class, "CSTShapeFunction");
108        setAlias(CSTClassFunction.class, "CSTClassFunction");
109        setAlias(BasicCSTCurve.class, "BasicCSTCurve");
110        setAlias(CSTCurveTrans.class, "CSTCurveTrans");
111
112        setAlias(LoftedSurface.class, "LoftedSurface");
113        setAlias(TFISurface.class, "TFISurface");
114        setAlias(BasicNurbsSurface.class, "BasicNurbsSurface");
115        setAlias(NurbsSurfaceTrans.class, "NurbsSurfaceTrans");
116        setAlias(ControlPointNet.class, "ControlPointNet");
117
118        setAlias(SubrangePoint.class, "SubrangePoint");
119        setAlias(SubrangeCurve.class, "SubrangeCurve");
120        setAlias(SubrangeSurface.class, "SubrangeSurface");
121
122        setAlias(Note.class, "Note");
123        setAlias(NoteTrans.class, "NoteTrans");
124
125        setAlias(Color.class, "Color");
126        setAlias(FastTable.class, "FastTable");
127
128    }
129
130    @Override
131    public <T> XMLFormat<T> getFormat(Class<T> cls) {
132        if (Color.class.equals(cls))
133            return (XMLFormat<T>)colorXML;
134        return super.getFormat(cls);
135    }
136}