@@ -48,7 +48,7 @@ parseChild :: Cursor -> P (Maybe Xsd.Child)
48parseChild c = do
48parseChild c = do
49 name <- getElementName c
49 name <- getElementName c
50 case name of
50 case name of
51− "element" -> Just . Xsd.ChildElement <$> parseElement c
51+ "element" -> Just . Xsd.ChildElement <$> parseElementOrRef c
52 "simpleType" -> Just <$> parseTopSimpleType c
52 "simpleType" -> Just <$> parseTopSimpleType c
53 "complexType" -> Just <$> parseTopComplexType c
53 "complexType" -> Just <$> parseTopComplexType c
54 "include" -> Just . Xsd.ChildInclude <$> parseInclude c
54 "include" -> Just . Xsd.ChildInclude <$> parseInclude c
@@ -81,6 +81,12 @@ parseImport c = do
81 , Xsd.importNamespace = namespace
81 , Xsd.importNamespace = namespace
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
84parseElement :: Cursor -> P Xsd.Element
90parseElement :: Cursor -> P Xsd.Element
85parseElement c = handleNamespaces c $ do
91parseElement c = handleNamespaces c $ do
86 name <- theAttribute "name" c
92 name <- theAttribute "name" c
@@ -154,18 +160,19 @@ parseSimpleType c = do
154 unionAxis <- makeElemAxis "union"
160 unionAxis <- makeElemAxis "union"
155 case (c $/ restrictionAxis, c $/ listAxis, c $/ unionAxis) of
161 case (c $/ restrictionAxis, c $/ listAxis, c $/ unionAxis) of
156 ([e], [], []) -> Xsd.AtomicType
162 ([e], [], []) -> Xsd.AtomicType
157− <$> parseRestriction e
163+ <$> parseSimpleRestriction e
158 <*> parseAnnotations c
164 <*> parseAnnotations c
159 ([], [l], []) -> Xsd.ListType
165 ([], [l], []) -> Xsd.ListType
160 <$> parseList l
166 <$> parseList l
161 <*> parseAnnotations c
167 <*> parseAnnotations c
162− -- TODO implement me
168+ ([], [], [u]) -> Xsd.UnionType
163− ([], [], [_]) -> parseError c "Not implemented: union"
169+ <$> parseUnion u
170+ <*> parseAnnotations c
164 _ -> parseError c "Expected exactly of of restriction, list or union"
171 _ -> parseError c "Expected exactly of of restriction, list or union"
166-- | Restriction for simple type
173-- | Restriction for simple type
167−parseRestriction :: Cursor -> P Xsd.Restriction
174+parseSimpleRestriction :: Cursor -> P Xsd.SimpleRestriction
168−parseRestriction c = do
175+parseSimpleRestriction c = do
169 -- XXX: generalaze RefOr parsing
176 -- XXX: generalaze RefOr parsing
170 tp <- case anAttribute "base" c of
177 tp <- case anAttribute "base" c of
171 Nothing -> do
178 Nothing -> do
@@ -178,9 +185,9 @@ parseRestriction c = do
179 constraints <- parseConstrains c
186 constraints <- parseConstrains c
181− return Xsd.Restriction
188+ return Xsd.SimpleRestriction
182− { Xsd.restrictionBase = tp
189+ { Xsd.simpleRestrictionBase = tp
183− , Xsd.restrictionConstraints = constraints
190+ , Xsd.simpleRestrictionConstraints = constraints
186parseConstrains :: Cursor -> P [Xsd.Constraint]
193parseConstrains :: Cursor -> P [Xsd.Constraint]
@@ -201,6 +208,15 @@ parseList c =
201 _ -> parseError c "Multiple types"
208 _ -> parseError c "Multiple types"
202 Just t -> Xsd.Ref <$> makeQName c t
209 Just t -> Xsd.Ref <$> makeQName c t
211+parseUnion :: Cursor -> P [Xsd.RefOr Xsd.SimpleType]
213+ case anAttribute "memberTypes" c of
215+ mapM (fmap Xsd.Ref . makeQName c) (Text.splitOn " " list)
217+ simpleTypeAxis <- makeElemAxis "simpleType"
218+ mapM (fmap Xsd.Inline . parseSimpleType) (c $/ simpleTypeAxis)
204parseComplexType :: Cursor -> P Xsd.ComplexType
220parseComplexType :: Cursor -> P Xsd.ComplexType
205parseComplexType c = do
221parseComplexType c = do
206 annotations <- parseAnnotations c
222 annotations <- parseAnnotations c
@@ -231,14 +247,29 @@ parsePlainContent c = do
233parseSimpleContent :: Cursor -> P Xsd.SimpleContent
249parseSimpleContent :: Cursor -> P Xsd.SimpleContent
234−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"
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
236parseComplexContent :: Cursor -> P Xsd.ComplexContent
267parseComplexContent :: Cursor -> P Xsd.ComplexContent
237parseComplexContent c = do
268parseComplexContent c = do
238 restrictionAxis <- makeElemAxis "restriction"
269 restrictionAxis <- makeElemAxis "restriction"
239 extenstionAxis <- makeElemAxis "extension"
270 extenstionAxis <- makeElemAxis "extension"
240 case (c $/ restrictionAxis, c $/ extenstionAxis) of
271 case (c $/ restrictionAxis, c $/ extenstionAxis) of
241− ([r], []) -> parseError r "not implemented"
272+ ([r], []) -> Xsd.ComplexContentRestriction <$> parseComplexRestriction r
242 ([], [e]) -> Xsd.ComplexContentExtension <$> parseComplexExtension e
273 ([], [e]) -> Xsd.ComplexContentExtension <$> parseComplexExtension e
243 _ -> parseError c "Expected one of restriction or extension"
274 _ -> parseError c "Expected one of restriction or extension"
@@ -253,6 +284,17 @@ parseComplexExtension c = handleNamespaces c $ do
253 , Xsd.complexExtensionAttributes = attributes
284 , Xsd.complexExtensionAttributes = attributes
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
256parseModelGroup :: Cursor -> P (Maybe Xsd.ModelGroup)
298parseModelGroup :: Cursor -> P (Maybe Xsd.ModelGroup)
257parseModelGroup c = do
299parseModelGroup c = do
258 sequenceAxis <- makeElemAxis "sequence"
300 sequenceAxis <- makeElemAxis "sequence"
@@ -275,10 +317,10 @@ parseAll :: Cursor -> P Xsd.ModelGroup
275parseAll c = Xsd.All <$> parseElements c
317parseAll c = Xsd.All <$> parseElements c
277-- | Get all child elements
319-- | Get all child elements
278−parseElements :: Cursor -> P [Xsd.Element]
320+parseElements :: Cursor -> P [Xsd.RefOr Xsd.Element]
279parseElements c = do
321parseElements c = do
280 elementAxis <- makeElemAxis "element"
322 elementAxis <- makeElemAxis "element"
281− mapM parseElement (c $/ elementAxis)
323+ mapM parseElementOrRef (c $/ elementAxis)
283parseAttributes :: Cursor -> P [Xsd.Attribute]
325parseAttributes :: Cursor -> P [Xsd.Attribute]
284parseAttributes c = do
326parseAttributes c = do
@@ -287,24 +329,33 @@ parseAttributes c = do
288parseAttribute :: Cursor -> P Xsd.Attribute
330parseAttribute :: Cursor -> P Xsd.Attribute
289parseAttribute c = do
331parseAttribute c = do
290− name <- theAttribute "name" c >>= makeTargetQName
291 use <- parseUseAttribute c
332 use <- parseUseAttribute c
292−
333+ case (anAttribute "name" c, anAttribute "ref" c) of
293− -- XXX: generalaze RefOr parsing
334+ (Just n, Nothing) -> do
335+ name <- makeTargetQName n
294− tp <- case anAttribute "type" c of
336+ tp <- case anAttribute "type" c of
295− Nothing -> do
337+ Nothing -> do
296− simpleTypeAxis <- makeElemAxis "simpleType"
338+ simpleTypeAxis <- makeElemAxis "simpleType"
297− case c $/ simpleTypeAxis of
339+ case c $/ simpleTypeAxis of
298− [s] -> Xsd.Inline <$> parseSimpleType s
340+ [s] -> Xsd.Inline <$> parseSimpleType s
299− [] -> parseError c "Attribute should have type"
341+ [] -> return (Xsd.Ref anySimpleType)
300− _ -> parseError c "Multiple simple types"
342+ _ -> parseError c "Multiple simple types"
301− Just t -> Xsd.Ref <$> makeQName c t
343+ Just t -> Xsd.Ref <$> makeQName c t
302−
344+ return $ Xsd.InlineAttribute $ Xsd.AttributeInline
303− return Xsd.Attribute
345+ { Xsd.attributeInlineName = name
304− { Xsd.attrName = name
346+ , Xsd.attributeInlineUse = use
305− , Xsd.attrUse = use
347+ , Xsd.attributeInlineType = tp
306− , Xsd.attrType = 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
355+ _ -> parseError c "Should be either name or ref"
358+ Xsd.QName (Just (Xsd.Namespace Xsd.schemaNamespace)) "anySimpleType"
309parseUseAttribute :: Cursor -> P Xsd.Use
360parseUseAttribute :: Cursor -> P Xsd.Use
310parseUseAttribute c = case anAttribute "use" c of
361parseUseAttribute c = case anAttribute "use" c of