@@ -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
@@ -168,11 +183,19 @@ parseRestriction c = do
168183 _ -> parseError c "Multiple types"
169184 Just t -> Xsd.Ref <$> makeQName c t
170185
171− return Xsd.Restriction
172− { Xsd.restrictionBase = tp
173− , Xsd.restrictionConstraints = [] -- TODO: implement me
186+ constraints <- parseConstrains c
187+
188+ return Xsd.SimpleRestriction
189+ { Xsd.simpleRestrictionBase = tp
190+ , Xsd.simpleRestrictionConstraints = constraints
174191 }
175192
193+parseConstrains :: Cursor -> P [Xsd.Constraint]
194+parseConstrains c = do
195+ enumerationAxis <- makeElemAxis "enumeration"
196+ forM (c $/ enumerationAxis) $ \e -> do
197+ Xsd.Enumeration <$> theAttribute "value" e
198+
176199parseList :: Cursor -> P (Xsd.RefOr Xsd.SimpleType)
177200parseList c =
178201 -- XXX: generalaze RefOr parsing
@@ -185,30 +208,105 @@ parseList c =
185208 _ -> parseError c "Multiple types"
186209 Just t -> Xsd.Ref <$> makeQName c t
187210
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+
188220parseComplexType :: Cursor -> P Xsd.ComplexType
189221parseComplexType c = do
190− attrAxis <- makeElemAxis "attribute"
191− attributes <- mapM parseAttribute (c $/ attrAxis)
192−
193− group <- do
194− sequenceAxis <- makeElemAxis "sequence"
195− choiceAxis <- makeElemAxis "choice"
196− allAxis <- makeElemAxis "all"
197− case (c $/ sequenceAxis, c $/ choiceAxis, c $/ allAxis) of
198− ([], [], []) -> return Nothing
199− ([g], [], []) -> Just <$> parseSequence g
200− ([], [g], []) -> Just <$> parseChoice g
201− ([], [], [g]) -> Just <$> parseAll g
202− _ -> parseError c "Multiple groups"
203−
204222 annotations <- parseAnnotations c
205−
223+ cont <- parseContent c
206224 return Xsd.ComplexType
207− { Xsd.complexAttributes = attributes
208− , Xsd.complexModelGroup = group
209− , Xsd.complexAnnotations = annotations
225+ { Xsd.complexAnnotations = annotations
226+ , Xsd.complexContent = cont
227+ }
228+
229+parseContent :: Cursor -> P Xsd.Content
230+parseContent c = do
231+ simpleContentAxis <- makeElemAxis "simpleContent"
232+ complexContentAxis <- makeElemAxis "complexContent"
233+ case (c $/ simpleContentAxis, c $/ complexContentAxis) of
234+ ([], []) -> Xsd.ContentPlain <$> parsePlainContent c
235+ ([s], []) -> Xsd.ContentSimple <$> parseSimpleContent s
236+ ([], [s]) -> Xsd.ContentComplex <$> parseComplexContent s
237+ _ -> parseError c
238+ "Expected one of simpleContent, complextContent or model group"
239+
240+parsePlainContent :: Cursor -> P Xsd.PlainContent
241+parsePlainContent c = do
242+ attributes <- parseAttributes c
243+ model <- parseModelGroup c
244+ return Xsd.PlainContent
245+ { Xsd.plainContentModel = model
246+ , Xsd.plainContentAttributes = attributes
247+ }
248+
249+parseSimpleContent :: Cursor -> P Xsd.SimpleContent
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+ }
266+
267+parseComplexContent :: Cursor -> P Xsd.ComplexContent
268+parseComplexContent c = do
269+ restrictionAxis <- makeElemAxis "restriction"
270+ extenstionAxis <- makeElemAxis "extension"
271+ case (c $/ restrictionAxis, c $/ extenstionAxis) of
272+ ([r], []) -> Xsd.ComplexContentRestriction <$> parseComplexRestriction r
273+ ([], [e]) -> Xsd.ComplexContentExtension <$> parseComplexExtension e
274+ _ -> parseError c "Expected one of restriction or extension"
275+
276+parseComplexExtension :: Cursor -> P Xsd.ComplexExtension
277+parseComplexExtension c = handleNamespaces c $ do
278+ base <- theAttribute "base" c >>= makeQName c
279+ attributes <- parseAttributes c
280+ model <- parseModelGroup c
281+ return Xsd.ComplexExtension
282+ { Xsd.complexExtensionBase = base
283+ , Xsd.complexExtensionModel = model
284+ , Xsd.complexExtensionAttributes = attributes
210285 }
211286
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+
298+parseModelGroup :: Cursor -> P (Maybe Xsd.ModelGroup)
299+parseModelGroup c = do
300+ sequenceAxis <- makeElemAxis "sequence"
301+ choiceAxis <- makeElemAxis "choice"
302+ allAxis <- makeElemAxis "all"
303+ case (c $/ sequenceAxis, c $/ choiceAxis, c $/ allAxis) of
304+ ([], [], []) -> return Nothing
305+ ([g], [], []) -> Just <$> parseSequence g
306+ ([], [g], []) -> Just <$> parseChoice g
307+ ([], [], [g]) -> Just <$> parseAll g
308+ _ -> parseError c "Multiple model groups"
309+
212310parseSequence :: Cursor -> P Xsd.ModelGroup
213311parseSequence c = Xsd.Sequence <$> parseElements c
214312
@@ -219,31 +317,45 @@ parseAll :: Cursor -> P Xsd.ModelGroup
219317parseAll c = Xsd.All <$> parseElements c
220318
221319-- | Get all child elements
222−parseElements :: Cursor -> P [Xsd.Element]
320+parseElements :: Cursor -> P [Xsd.RefOr Xsd.Element]
223321parseElements c = do
224322 elementAxis <- makeElemAxis "element"
225− mapM parseElement (c $/ elementAxis)
323+ mapM parseElementOrRef (c $/ elementAxis)
324+
325+parseAttributes :: Cursor -> P [Xsd.Attribute]
326+parseAttributes c = do
327+ attrAxis <- makeElemAxis "attribute"
328+ mapM parseAttribute (c $/ attrAxis)
226329
227330parseAttribute :: Cursor -> P Xsd.Attribute
228331parseAttribute c = do
229− name <- theAttribute "name" c >>= makeTargetQName
230332 use <- parseUseAttribute c
231−
232− -- XXX: generalaze RefOr parsing
233− tp <- case anAttribute "type" c of
234− Nothing -> do
235− simpleTypeAxis <- makeElemAxis "simpleType"
236− case c $/ simpleTypeAxis of
237− [s] -> Xsd.Inline <$> parseSimpleType s
238− [] -> parseError c "Attribute should have type"
239− _ -> parseError c "Multiple simple types"
240− Just t -> Xsd.Ref <$> makeQName c t
241−
242− return Xsd.Attribute
243− { Xsd.attrName = name
244− , Xsd.attrUse = use
245− , Xsd.attrType = tp
246− }
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"
247359
248360parseUseAttribute :: Cursor -> P Xsd.Use
249361parseUseAttribute c = case anAttribute "use" c of
@@ -399,8 +511,9 @@ makeElemAxis name = do
399511makeQName :: Cursor -> Text -> P Xsd.QName
400512makeQName c t = case Text.splitOn ":" t of
401513 [n] -> do
514+ ns <- asks (lookup Nothing . envPrefixes)
402515 return Xsd.QName
403− { Xsd.qnNamespace = Nothing
516+ { Xsd.qnNamespace = ns
404517 , Xsd.qnName = n
405518 }
406519 [p, n] -> do