KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > ecore > xmi > impl > SAXXMLHandler


1 /**
2  * <copyright>
3  *
4  * Copyright (c) 2002-2004 IBM Corporation and others.
5  * All rights reserved. This program and the accompanying materials
6  * are made available under the terms of the Eclipse Public License v1.0
7  * which accompanies this distribution, and is available at
8  * http://www.eclipse.org/legal/epl-v10.html
9  *
10  * Contributors:
11  * IBM - Initial API and implementation
12  *
13  * </copyright>
14  *
15  * $Id: SAXXMLHandler.java,v 1.6 2005/06/08 06:16:07 nickb Exp $
16  */

17 package org.eclipse.emf.ecore.xmi.impl;
18
19 import java.util.List JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import org.xml.sax.Attributes JavaDoc;
23 import org.xml.sax.Locator JavaDoc;
24
25 import org.eclipse.emf.ecore.EAttribute;
26 import org.eclipse.emf.ecore.EObject;
27 import org.eclipse.emf.ecore.EStructuralFeature;
28 import org.eclipse.emf.ecore.InternalEObject;
29 import org.eclipse.emf.ecore.xmi.XMLHelper;
30 import org.eclipse.emf.ecore.xmi.XMLResource;
31 import org.eclipse.emf.ecore.xmi.XMLResource.XMLInfo;
32
33
34 /**
35  * This class implements the XML deserializer which creates
36  * EObjects from XML files.
37  */

38 public class SAXXMLHandler extends XMLHandler
39 {
40   protected Locator JavaDoc locator;
41   protected Attributes JavaDoc attribs;
42
43   /**
44    * Constructor.
45    */

46   public SAXXMLHandler(XMLResource xmiResource, XMLHelper helper, Map JavaDoc options)
47   {
48     super(xmiResource, helper, options);
49   }
50
51   protected Object JavaDoc setAttributes(Object JavaDoc attributes)
52   {
53     Object JavaDoc oldAttribs = attribs;
54     this.attribs = (Attributes JavaDoc)attributes;
55     return oldAttribs;
56   }
57
58   public void setLocator(Object JavaDoc locator)
59   {
60     this.locator = (Locator JavaDoc) locator;
61   }
62
63   protected int getLineNumber()
64   {
65     if (locator != null)
66     {
67       return locator.getLineNumber();
68     }
69     else
70     {
71       return super.getLineNumber();
72     }
73   }
74
75   protected int getColumnNumber()
76   {
77     if (locator != null)
78     {
79       return locator.getColumnNumber();
80     }
81     else
82     {
83       return super.getColumnNumber();
84     }
85   }
86
87   /**
88    * Returns true if the xsi:nil attribute is in the list of attributes.
89    */

90   protected boolean isNull()
91   {
92     return attribs.getValue(NIL_ATTRIB) != null;
93   }
94
95
96   /**
97    * Handle the XML namespace attributes.
98    */

99   protected void handleNamespaceAttribs()
100   {
101     for (int i = 0, size = attribs.getLength(); i < size; ++i)
102     {
103       String JavaDoc attrib = attribs.getQName(i);
104       if (attrib.startsWith(XMLResource.XML_NS))
105       {
106         handleXMLNSAttribute(attrib, attribs.getValue(i));
107       }
108       else if (SCHEMA_LOCATION_ATTRIB.equals(attrib))
109       {
110         handleXSISchemaLocation(attribs.getValue(i));
111       }
112       else if (NO_NAMESPACE_SCHEMA_LOCATION_ATTRIB.equals(attrib))
113       {
114         handleXSINoNamespaceSchemaLocation(attribs.getValue(i));
115       }
116     }
117   }
118
119   protected String JavaDoc getXSIType()
120   {
121     return attribs.getValue(TYPE_ATTRIB);
122   }
123
124   /**
125    * Process the XML attributes for the newly created object.
126    */

127   protected void handleObjectAttribs(EObject obj)
128   {
129     if (attribs != null)
130     {
131       InternalEObject internalEObject = (InternalEObject)obj;
132       for (int i = 0, size = attribs.getLength(); i < size; ++i)
133       {
134         String JavaDoc name = attribs.getQName(i);
135         if (name.equals(idAttribute))
136         {
137           xmlResource.setID(internalEObject, attribs.getValue(i));
138         }
139         else if (name.equals(hrefAttribute) && (!recordUnknownFeature || types.peek() != UNKNOWN_FEATURE_TYPE))
140         {
141           handleProxy(internalEObject, attribs.getValue(i));
142         }
143         else if (!name.startsWith(XMLResource.XML_NS) && !notFeatures.contains(name))
144         {
145           setAttribValue(obj, name, attribs.getValue(i));
146         }
147       }
148     }
149   }
150
151   protected void processObject(EObject object)
152   {
153     if (object != null)
154     {
155       EStructuralFeature valueFeature = getContentFeature(object);
156
157       if (valueFeature != null)
158       {
159         text = new StringBuffer JavaDoc();
160         objects.push(object);
161         types.push(valueFeature);
162         return;
163       }
164     }
165
166     super.processObject(object);
167   }
168
169   protected EStructuralFeature getContentFeature(EObject object)
170   {
171     if (xmlMap != null)
172     {
173       List JavaDoc eAttributes = object.eClass().getEAllAttributes();
174       if (eAttributes.size() >= 1)
175       {
176         EAttribute eAttribute = (EAttribute) eAttributes.get(0);
177         XMLInfo info = xmlMap.getInfo(eAttribute);
178         if (info != null && info.getXMLRepresentation() == XMLInfo.CONTENT)
179         {
180           return eAttribute;
181         }
182       }
183     }
184
185     return null;
186   }
187 } // SAXXMLHandler
188
Popular Tags