KickJava   Java API By Example, From Geeks To Geeks.

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


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: SAXXMIHandler.java,v 1.4 2005/06/08 06:16:07 nickb Exp $
16  */

17 package org.eclipse.emf.ecore.xmi.impl;
18
19 import java.util.Map JavaDoc;
20
21 import org.xml.sax.Attributes JavaDoc;
22 import org.xml.sax.Locator JavaDoc;
23
24 import org.eclipse.emf.ecore.EObject;
25 import org.eclipse.emf.ecore.InternalEObject;
26 import org.eclipse.emf.ecore.xmi.XMLHelper;
27 import org.eclipse.emf.ecore.xmi.XMLResource;
28
29
30 /**
31  * This class is a SAX handler for creating MOF2 objects from an XMI 2.0 file.
32  */

33 public class SAXXMIHandler extends XMIHandler
34 {
35   protected Locator JavaDoc locator;
36   protected Attributes JavaDoc attribs;
37
38   /**
39    * Constructor.
40    */

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

85   protected boolean isNull()
86   {
87     return attribs.getValue(NIL_ATTRIB) != null;
88   }
89
90
91   /**
92    * Handle the XML namespace attributes.
93    */

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

129   protected void handleObjectAttribs(EObject obj)
130   {
131     if (attribs != null)
132     {
133       InternalEObject internalEObject = (InternalEObject)obj;
134       for (int i = 0, size = attribs.getLength(); i < size; ++i)
135       {
136         String JavaDoc name = attribs.getQName(i);
137         if (name.equals(ID_ATTRIB))
138         {
139           xmlResource.setID(internalEObject, attribs.getValue(i));
140         }
141         else if (name.equals(hrefAttribute) && (!recordUnknownFeature || types.peek() != UNKNOWN_FEATURE_TYPE))
142         {
143           handleProxy(internalEObject, attribs.getValue(i));
144         }
145         else if (!name.startsWith(XMLResource.XML_NS) && !notFeatures.contains(name))
146         {
147           setAttribValue(obj, name, attribs.getValue(i));
148         }
149       }
150     }
151   }
152 } // SAXXMIHandler
153
Popular Tags