@@ -48,7 +48,7 @@ parseChild :: Cursor -> P (Maybe Xsd.Child)
4848parseChild c = do
4949 name <- getElementName c
5050 case name of
51− "element" -> Just . Xsd.ChildElement <$> parseElement c
51+ "element" -> Just . Xsd.ChildElement <$> parseElementOrRef c
5252 "simpleType" -> Just <$> parseTopSimpleType c
5353 "complexType" -> Just <$> parseTopComplexType c
5454 "include" -> Just . Xsd.ChildInclude <$> parseInclude c
@@ -81,14 +81,19 @@ parseImport c = do
8181 , Xsd.importNamespace = namespace
8282 }
8383
84+parseElementOrRef :: Cursor -> P (Xsd.RefOr Xsd.Element)
85+parseElementOrRef c = do
86+ case anAttribute "ref" c of
87+ Nothing -> Xsd.Inline <$> parseElement c
88+ Just ref -> Xsd.Ref <$> makeQName c ref
89+
8490parseElement :: Cursor -> P Xsd.Element
8591parseElement c = handleNamespaces c $ do
8692 name <- theAttribute "name" c
8793
88− -- XXX: generalaze RefOr parsing
8994 tp <- case anAttribute "type" c of
9095 Just t -> Xsd.Ref <$> makeQName c t
91− _ -> Xsd.Inline <$> parseType c
96+ _ -> maybe (Xsd.Ref anyType) Xsd.Inline <$> parseType c
9297
9398 minOccurs <- case anAttribute "minOccurs" c of
9499 Nothing -> return 1
@@ -107,22 +112,31 @@ parseElement c = handleNamespaces c $ do
107112
108113 qname <- makeTargetQName name
109114
115+ nillable <- case anAttribute "nillable" c of
116+ Nothing -> return False
117+ Just "true" -> return True
118+ Just "false" -> return False
119+ _ -> parseError c "Unexpected value of nillable attribute"
120+
110121 return Xsd.Element
111122 { Xsd.elementName = qname
112123 , Xsd.elementType = tp
113124 , Xsd.elementAnnotations = annotations
114125 , Xsd.elementOccurs = (minOccurs, maxOccurs)
126+ , Xsd.elementNillable = nillable
115127 }
128+ where
129+ anyType = Xsd.QName (Just (Xsd.Namespace Xsd.schemaNamespace)) "anyType"
116130
117131-- | Parse inline type, simple or complex
118−parseType :: Cursor -> P Xsd.Type
132+parseType :: Cursor -> P (Maybe Xsd.Type)
119133parseType c = do
120134 simpleTypeAxis <- makeElemAxis "simpleType"
121135 complexTypeAxis <- makeElemAxis "complexType"
122136 case (c $/ simpleTypeAxis, c $/ complexTypeAxis) of
123− ([], []) -> parseError c "No type"
124− ([t], []) -> Xsd.TypeSimple <$> parseSimpleType t
125− ([], [t]) -> Xsd.TypeComplex <$> parseComplexType t
137+ ([], []) -> return Nothing
138+ ([t], []) -> Just . Xsd.TypeSimple <$> parseSimpleType t
139+ ([], [t]) -> Just . Xsd.TypeComplex <$> parseComplexType t
126140 _ -> parseError c "Multiple types"
127141
128142-- | Top level simple type
@@ -146,18 +160,19 @@ parseSimpleType c = do
146160 unionAxis <- makeElemAxis "union"
147161 case (c $/ restrictionAxis, c $/ listAxis, c $/ unionAxis) of
148162 ([e], [], []) -> Xsd.AtomicType
149− <$> parseRestriction e
163+ <$> parseSimpleRestriction e
150164 <*> parseAnnotations c
151165 ([], [l], []) -> Xsd.ListType
152166 <$> parseList l
153167 <*> parseAnnotations c
154− -- TODO implement me
155− ([], [], [_]) -> parseError c "Not implemented: union"
168+ ([], [], [u]) -> Xsd.UnionType
169+ <$> parseUnion u
170+ <*> parseAnnotations c
156171 _ -> parseError c "Expected exactly of of restriction, list or union"
157172
158173-- | Restriction for simple type
159−parseRestriction :: Cursor -> P Xsd.Restriction
160−parseRestriction c = do
174+parseSimpleRestriction :: Cursor -> P Xsd.SimpleRestriction
175+parseSimpleRestriction c = do
161176 -- XXX: generalaze RefOr parsing
162177 tp <- case anAttribute "base" c of
163178 Nothing -> do
@@ -170,9 +185,9 @@ parseRestriction c = do
170185
171186 constraints <- parseConstrains c
172187
173− return Xsd.Restriction
174− { Xsd.restrictionBase = tp
175− , Xsd.restrictionConstraints = constraints
188+ return Xsd.SimpleRestriction
189+ { Xsd.simpleRestrictionBase = tp
190+ , Xsd.simpleRestrictionConstraints = constraints
176191 }
177192
178193parseConstrains :: Cursor -> P [Xsd.Constraint]
@@ -193,6 +208,15 @@ parseList c =
193208 _ -> parseError c "Multiple types"
194209 Just t -> Xsd.Ref <$> makeQName c t
195210
211+parseUnion :: Cursor -> P [Xsd.RefOr Xsd.SimpleType]
212+parseUnion c =
213+ case anAttribute "memberTypes" c of
214+ Just list ->
215+ mapM (fmap Xsd.Ref . makeQName c) (Text.splitOn " " list)
216+ Nothing -> do
217+ simpleTypeAxis <- makeElemAxis "simpleType"
218+ mapM (fmap Xsd.Inline . parseSimpleType) (c $/ simpleTypeAxis)
219+
196220parseComplexType :: Cursor -> P Xsd.ComplexType
197221parseComplexType c = do
198222 annotations <- parseAnnotations c
@@ -223,19 +247,34 @@ parsePlainContent c = do
223247 }
224248
225249parseSimpleContent :: Cursor -> P Xsd.SimpleContent
226−parseSimpleContent c = parseError c "not implemented"
250+parseSimpleContent c = do
251+ restrictionAxis <- makeElemAxis "restriction"
252+ extenstionAxis <- makeElemAxis "extension"
253+ case (c $/ restrictionAxis, c $/ extenstionAxis) of
254+ ([r], []) -> Xsd.SimpleContentRestriction <$> parseSimpleRestriction r
255+ ([], [e]) -> Xsd.SimpleContentExtension <$> parseSimpleExtension e
256+ _ -> parseError c "Expected one of restriction or extension"
257+
258+parseSimpleExtension :: Cursor -> P Xsd.SimpleExtension
259+parseSimpleExtension c = handleNamespaces c $ do
260+ base <- theAttribute "base" c >>= makeQName c
261+ attributes <- parseAttributes c
262+ return Xsd.SimpleExtension
263+ { Xsd.simpleExtensionBase = base
264+ , Xsd.simpleExtensionAttributes = attributes
265+ }
227266
228267parseComplexContent :: Cursor -> P Xsd.ComplexContent
229268parseComplexContent c = do
230269 restrictionAxis <- makeElemAxis "restriction"
231270 extenstionAxis <- makeElemAxis "extension"
232271 case (c $/ restrictionAxis, c $/ extenstionAxis) of
233− ([r], []) -> parseError r "not implemented"
272+ ([r], []) -> Xsd.ComplexContentRestriction <$> parseComplexRestriction r
234273 ([], [e]) -> Xsd.ComplexContentExtension <$> parseComplexExtension e
235274 _ -> parseError c "Expected one of restriction or extension"
236275
237276parseComplexExtension :: Cursor -> P Xsd.ComplexExtension
238−parseComplexExtension c = do
277+parseComplexExtension c = handleNamespaces c $ do
239278 base <- theAttribute "base" c >>= makeQName c
240279 attributes <- parseAttributes c
241280 model <- parseModelGroup c
@@ -245,6 +284,17 @@ parseComplexExtension c = do
245284 , Xsd.complexExtensionAttributes = attributes
246285 }
247286
287+parseComplexRestriction :: Cursor -> P Xsd.ComplexRestriction
288+parseComplexRestriction c = handleNamespaces c $ do
289+ base <- theAttribute "base" c >>= makeQName c
290+ attributes <- parseAttributes c
291+ model <- parseModelGroup c
292+ return Xsd.ComplexRestriction
293+ { Xsd.complexRestrictionBase = base
294+ , Xsd.complexRestrictionModel = model
295+ , Xsd.complexRestrictionAttributes = attributes
296+ }
297+
248298parseModelGroup :: Cursor -> P (Maybe Xsd.ModelGroup)
249299parseModelGroup c = do
250300 sequenceAxis <- makeElemAxis "sequence"
@@ -267,10 +317,10 @@ parseAll :: Cursor -> P Xsd.ModelGroup
267317parseAll c = Xsd.All <$> parseElements c
268318
269319-- | Get all child elements
270−parseElements :: Cursor -> P [Xsd.Element]
320+parseElements :: Cursor -> P [Xsd.RefOr Xsd.Element]
271321parseElements c = do
272322 elementAxis <- makeElemAxis "element"
273− mapM parseElement (c $/ elementAxis)
323+ mapM parseElementOrRef (c $/ elementAxis)
274324
275325parseAttributes :: Cursor -> P [Xsd.Attribute]
276326parseAttributes c = do
@@ -279,24 +329,33 @@ parseAttributes c = do
279329
280330parseAttribute :: Cursor -> P Xsd.Attribute
281331parseAttribute c = do
282− name <- theAttribute "name" c >>= makeTargetQName
283332 use <- parseUseAttribute c
284−
285− -- XXX: generalaze RefOr parsing
286− tp <- case anAttribute "type" c of
287− Nothing -> do
288− simpleTypeAxis <- makeElemAxis "simpleType"
289− case c $/ simpleTypeAxis of
290− [s] -> Xsd.Inline <$> parseSimpleType s
291− [] -> parseError c "Attribute should have type"
292− _ -> parseError c "Multiple simple types"
293− Just t -> Xsd.Ref <$> makeQName c t
294−
295− return Xsd.Attribute
296− { Xsd.attrName = name
297− , Xsd.attrUse = use
298− , Xsd.attrType = tp
299− }
333+ case (anAttribute "name" c, anAttribute "ref" c) of
334+ (Just n, Nothing) -> do
335+ name <- makeTargetQName n
336+ tp <- case anAttribute "type" c of
337+ Nothing -> do
338+ simpleTypeAxis <- makeElemAxis "simpleType"
339+ case c $/ simpleTypeAxis of
340+ [s] -> Xsd.Inline <$> parseSimpleType s
341+ [] -> return (Xsd.Ref anySimpleType)
342+ _ -> parseError c "Multiple simple types"
343+ Just t -> Xsd.Ref <$> makeQName c t
344+ return $ Xsd.InlineAttribute $ Xsd.AttributeInline
345+ { Xsd.attributeInlineName = name
346+ , Xsd.attributeInlineUse = use
347+ , Xsd.attributeInlineType = tp
348+ }
349+ (Nothing, Just r) -> do
350+ ref <- makeQName c r
351+ return $ Xsd.RefAttribute $ Xsd.AttributeRef
352+ { Xsd.attributeRefRef = ref
353+ , Xsd.attributeRefUse = use
354+ }
355+ _ -> parseError c "Should be either name or ref"
356+ where
357+ anySimpleType =
358+ Xsd.QName (Just (Xsd.Namespace Xsd.schemaNamespace)) "anySimpleType"
300359
301360parseUseAttribute :: Cursor -> P Xsd.Use
302361parseUseAttribute c = case anAttribute "use" c of
@@ -452,8 +511,9 @@ makeElemAxis name = do
452511makeQName :: Cursor -> Text -> P Xsd.QName
453512makeQName c t = case Text.splitOn ":" t of
454513 [n] -> do
514+ ns <- asks (lookup Nothing . envPrefixes)
455515 return Xsd.QName
456− { Xsd.qnNamespace = Nothing
516+ { Xsd.qnNamespace = ns
457517 , Xsd.qnName = n
458518 }
459519 [p, n] -> do