@@ -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,14 +81,19 @@ 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
88− -- XXX: generalaze RefOr parsing
89 tp <- case anAttribute "type" c of
94 tp <- case anAttribute "type" c of
90 Just t -> Xsd.Ref <$> makeQName c t
95 Just t -> Xsd.Ref <$> makeQName c t
91− _ -> Xsd.Inline <$> parseType c
96+ _ -> maybe (Xsd.Ref anyType) Xsd.Inline <$> parseType c
93 minOccurs <- case anAttribute "minOccurs" c of
98 minOccurs <- case anAttribute "minOccurs" c of
94 Nothing -> return 1
99 Nothing -> return 1
@@ -107,22 +112,31 @@ parseElement c = handleNamespaces c $ do
108 qname <- makeTargetQName name
113 qname <- makeTargetQName name
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"
110 return Xsd.Element
121 return Xsd.Element
111 { Xsd.elementName = qname
122 { Xsd.elementName = qname
112 , Xsd.elementType = tp
123 , Xsd.elementType = tp
113 , Xsd.elementAnnotations = annotations
124 , Xsd.elementAnnotations = annotations
114 , Xsd.elementOccurs = (minOccurs, maxOccurs)
125 , Xsd.elementOccurs = (minOccurs, maxOccurs)
126+ , Xsd.elementNillable = nillable
129+ anyType = Xsd.QName (Just (Xsd.Namespace Xsd.schemaNamespace)) "anyType"
117-- | Parse inline type, simple or complex
131-- | Parse inline type, simple or complex
118−parseType :: Cursor -> P Xsd.Type
132+parseType :: Cursor -> P (Maybe Xsd.Type)
119parseType c = do
133parseType c = do
120 simpleTypeAxis <- makeElemAxis "simpleType"
134 simpleTypeAxis <- makeElemAxis "simpleType"
121 complexTypeAxis <- makeElemAxis "complexType"
135 complexTypeAxis <- makeElemAxis "complexType"
122 case (c $/ simpleTypeAxis, c $/ complexTypeAxis) of
136 case (c $/ simpleTypeAxis, c $/ complexTypeAxis) of
123− ([], []) -> parseError c "No type"
137+ ([], []) -> return Nothing
124− ([t], []) -> Xsd.TypeSimple <$> parseSimpleType t
138+ ([t], []) -> Just . Xsd.TypeSimple <$> parseSimpleType t
125− ([], [t]) -> Xsd.TypeComplex <$> parseComplexType t
139+ ([], [t]) -> Just . Xsd.TypeComplex <$> parseComplexType t
126 _ -> parseError c "Multiple types"
140 _ -> parseError c "Multiple types"
128-- | Top level simple type
142-- | Top level simple type
@@ -146,18 +160,19 @@ parseSimpleType c = do
146 unionAxis <- makeElemAxis "union"
160 unionAxis <- makeElemAxis "union"
147 case (c $/ restrictionAxis, c $/ listAxis, c $/ unionAxis) of
161 case (c $/ restrictionAxis, c $/ listAxis, c $/ unionAxis) of
148 ([e], [], []) -> Xsd.AtomicType
162 ([e], [], []) -> Xsd.AtomicType
149− <$> parseRestriction e
163+ <$> parseSimpleRestriction e
150 <*> parseAnnotations c
164 <*> parseAnnotations c
151 ([], [l], []) -> Xsd.ListType
165 ([], [l], []) -> Xsd.ListType
152 <$> parseList l
166 <$> parseList l
153 <*> parseAnnotations c
167 <*> parseAnnotations c
154− -- TODO implement me
168+ ([], [], [u]) -> Xsd.UnionType
155− ([], [], [_]) -> parseError c "Not implemented: union"
169+ <$> parseUnion u
170+ <*> parseAnnotations c
156 _ -> parseError c "Expected exactly of of restriction, list or union"
171 _ -> parseError c "Expected exactly of of restriction, list or union"
158-- | Restriction for simple type
173-- | Restriction for simple type
159−parseRestriction :: Cursor -> P Xsd.Restriction
174+parseSimpleRestriction :: Cursor -> P Xsd.SimpleRestriction
160−parseRestriction c = do
175+parseSimpleRestriction c = do
161 -- XXX: generalaze RefOr parsing
176 -- XXX: generalaze RefOr parsing
162 tp <- case anAttribute "base" c of
177 tp <- case anAttribute "base" c of
163 Nothing -> do
178 Nothing -> do
@@ -170,9 +185,9 @@ parseRestriction c = do
171 constraints <- parseConstrains c
186 constraints <- parseConstrains c
173− return Xsd.Restriction
188+ return Xsd.SimpleRestriction
174− { Xsd.restrictionBase = tp
189+ { Xsd.simpleRestrictionBase = tp
175− , Xsd.restrictionConstraints = constraints
190+ , Xsd.simpleRestrictionConstraints = constraints
178parseConstrains :: Cursor -> P [Xsd.Constraint]
193parseConstrains :: Cursor -> P [Xsd.Constraint]
@@ -193,30 +208,105 @@ parseList c =
193 _ -> parseError c "Multiple types"
208 _ -> parseError c "Multiple types"
194 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)
196parseComplexType :: Cursor -> P Xsd.ComplexType
220parseComplexType :: Cursor -> P Xsd.ComplexType
197parseComplexType c = do
221parseComplexType c = do
198− attrAxis <- makeElemAxis "attribute"
199− attributes <- mapM parseAttribute (c $/ attrAxis)
202− sequenceAxis <- makeElemAxis "sequence"
203− choiceAxis <- makeElemAxis "choice"
204− allAxis <- makeElemAxis "all"
205− case (c $/ sequenceAxis, c $/ choiceAxis, c $/ allAxis) of
206− ([], [], []) -> return Nothing
207− ([g], [], []) -> Just <$> parseSequence g
208− ([], [g], []) -> Just <$> parseChoice g
209− ([], [], [g]) -> Just <$> parseAll g
210− _ -> parseError c "Multiple groups"
212 annotations <- parseAnnotations c
222 annotations <- parseAnnotations c
213−
223+ cont <- parseContent c
214 return Xsd.ComplexType
224 return Xsd.ComplexType
215− { Xsd.complexAttributes = attributes
216− , Xsd.complexModelGroup = group
217− , Xsd.complexAnnotations = annotations
225+ { Xsd.complexAnnotations = annotations
226+ , Xsd.complexContent = cont
229+parseContent :: Cursor -> P Xsd.Content
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
238+ "Expected one of simpleContent, complextContent or model group"
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
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"
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
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"
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
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
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"
220parseSequence :: Cursor -> P Xsd.ModelGroup
310parseSequence :: Cursor -> P Xsd.ModelGroup
221parseSequence c = Xsd.Sequence <$> parseElements c
311parseSequence c = Xsd.Sequence <$> parseElements c
@@ -227,31 +317,45 @@ parseAll :: Cursor -> P Xsd.ModelGroup
227parseAll c = Xsd.All <$> parseElements c
317parseAll c = Xsd.All <$> parseElements c
229-- | Get all child elements
319-- | Get all child elements
230−parseElements :: Cursor -> P [Xsd.Element]
320+parseElements :: Cursor -> P [Xsd.RefOr Xsd.Element]
231parseElements c = do
321parseElements c = do
232 elementAxis <- makeElemAxis "element"
322 elementAxis <- makeElemAxis "element"
233− mapM parseElement (c $/ elementAxis)
323+ mapM parseElementOrRef (c $/ elementAxis)
325+parseAttributes :: Cursor -> P [Xsd.Attribute]
326+parseAttributes c = do
327+ attrAxis <- makeElemAxis "attribute"
328+ mapM parseAttribute (c $/ attrAxis)
235parseAttribute :: Cursor -> P Xsd.Attribute
330parseAttribute :: Cursor -> P Xsd.Attribute
236parseAttribute c = do
331parseAttribute c = do
237− name <- theAttribute "name" c >>= makeTargetQName
238 use <- parseUseAttribute c
332 use <- parseUseAttribute c
239−
333+ case (anAttribute "name" c, anAttribute "ref" c) of
240− -- XXX: generalaze RefOr parsing
334+ (Just n, Nothing) -> do
335+ name <- makeTargetQName n
241− tp <- case anAttribute "type" c of
336+ tp <- case anAttribute "type" c of
242− Nothing -> do
337+ Nothing -> do
243− simpleTypeAxis <- makeElemAxis "simpleType"
338+ simpleTypeAxis <- makeElemAxis "simpleType"
244− case c $/ simpleTypeAxis of
339+ case c $/ simpleTypeAxis of
245− [s] -> Xsd.Inline <$> parseSimpleType s
340+ [s] -> Xsd.Inline <$> parseSimpleType s
246− [] -> parseError c "Attribute should have type"
341+ [] -> return (Xsd.Ref anySimpleType)
247− _ -> parseError c "Multiple simple types"
342+ _ -> parseError c "Multiple simple types"
248− Just t -> Xsd.Ref <$> makeQName c t
343+ Just t -> Xsd.Ref <$> makeQName c t
249−
344+ return $ Xsd.InlineAttribute $ Xsd.AttributeInline
250− return Xsd.Attribute
345+ { Xsd.attributeInlineName = name
251− { Xsd.attrName = name
346+ , Xsd.attributeInlineUse = use
252− , Xsd.attrUse = use
347+ , Xsd.attributeInlineType = tp
253− , 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"
256parseUseAttribute :: Cursor -> P Xsd.Use
360parseUseAttribute :: Cursor -> P Xsd.Use
257parseUseAttribute c = case anAttribute "use" c of
361parseUseAttribute c = case anAttribute "use" c of
@@ -407,8 +511,9 @@ makeElemAxis name = do
407makeQName :: Cursor -> Text -> P Xsd.QName
511makeQName :: Cursor -> Text -> P Xsd.QName
408makeQName c t = case Text.splitOn ":" t of
512makeQName c t = case Text.splitOn ":" t of
409 [n] -> do
513 [n] -> do
514+ ns <- asks (lookup Nothing . envPrefixes)
410 return Xsd.QName
515 return Xsd.QName
411− { Xsd.qnNamespace = Nothing
516+ { Xsd.qnNamespace = ns
412 , Xsd.qnName = n
517 , Xsd.qnName = n
414 [p, n] -> do
519 [p, n] -> do