2017-03-23 4 views
0

J'essaie d'obtenir l'annotation d'un élément déclaré dans un xs: complexType dans mon XSD. Un tel élément est de type SchemaPreperty. Cependant, contrairement à SchemaGlobalElement et SchemaType, il n'y a pas SchemaProperty.getAnnotation() que je peux utiliser.XMLBeans: Obtenir l'annotation d'un élément imbriqué

Ceci est le XSD. J'ai besoin d'accéder à la documentation de l'élément number.

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
    <xs:element name="test" type="my-test-type" /> 

    <xs:complexType name="my-test-type"> 
     <xs:sequence> 
      <xs:element name="number" "xs:int"> 
       <xs:annotation> 
        <xs:documentation>This is the documentation I need.</xs:documentation> 
       </xs:annotation> 
      </xs:element> 
     </xs:sequence> 
    </xs:complexType> 
</xs:schema> 

Comment faire? Cela ne semble pas ... possible. J'ai donc trouvé la réponse dans la FAQ XMLBeans.

Répondre

1

Voici le link

Je colle le code de la FAQ, car les réponses qui ne contiennent que des liens sont mal vues. Vous pouvez très bien comprendre comment l'adapter à vos propres besoins de cet exemple:

public static void someMethod() { 
    SchemaType t = XmlBeans.getContextTypeLoader().findType(new QName("http://test", "T")); 
    SchemaProperty[] schemaProperties = t.getProperties(); 
    for (int i = 0; i < schemaProperties.length; i++) 
     printPropertyInfo(schemaProperties[i]); 

    System.out.println(); 

    if (t.getContentType() == SchemaType.ELEMENT_CONTENT || 
      t.getContentType() == SchemaType.MIXED_CONTENT) 
    { 
     SchemaParticle topParticle = t.getContentModel(); 
     // topParticle is non-null if we checked the content 
     navigateParticle(topParticle); 
    } 
} 

public static void navigateParticle(SchemaParticle p) 
{ 
    switch (p.getParticleType()) 
    { 
    case SchemaParticle.ALL: 
    case SchemaParticle.CHOICE: 
    case SchemaParticle.SEQUENCE: 
     // These are "container" particles, so iterate over their children 
     SchemaParticle[] children = p.getParticleChildren(); 
     for (int i = 0; i < children.length; i++) 
      navigateParticle(children[i]); 
     break; 
    case SchemaParticle.ELEMENT: 
     printElementInfo((SchemaLocalElement) p); 
     break; 
    default: 
     // There can also be "wildcards" corresponding to <xs:any> elements in the Schema 
    } 
} 

public static void printPropertyInfo(SchemaProperty p) 
{ 
    System.out.println("Property name=\"" + p.getName() + "\", type=\"" + p.getType().getName() 
     + "\", maxOccurs=\"" + 
     (p.getMaxOccurs() != null ? p.getMaxOccurs().toString() : "unbounded") + "\""); 
} 

public static void printElementInfo(SchemaLocalElement e) 
{ 
    System.out.println("Element name=\"" + e.getName() + "\", type=\"" + e.getType().getName() 
     + "\", maxOccurs=\"" + 
     (e.getMaxOccurs() != null ? e.getMaxOccurs().toString() : "unbounded") + "\""); 
    SchemaAnnotation annotation = e.getAnnotation(); 
    if (annotation != null) 
    { 
     SchemaAnnotation.Attribute[] att = annotation.getAttributes(); 
     if (att != null && att.length > 0) 
      System.out.println(" Annotation: " + att[0].getName() + "=\"" + 
       att[0].getValue() + "\""); 
    } 
} 

En ce qui concerne les attributs, la FAQ ne les mentionne même pas, mais ils sont accessibles différemment. Cela m'a donné un mal de tête énorme, parce que j'essayais de comprendre comment accéder à l'annotation d'un attribut de la même manière que le code ci-dessus. Accéder aux annotations d'un attribut est plutôt simple et direct.

Voilà ma méthode actuelle de le faire:

public String getAttributeAnnotation(SchemaType t, String attributeLocalName) { 
    if (null != t) { 
     SchemaAttributeModel attrModel = t.getAttributeModel(); 
     if (null != attrModel) { 
      SchemaLocalAttribute[] attributes = t.getAttributeModel().getAttributes(); 
      if (attributes.length > 0) { 
       SchemaLocalAttribute attr = Arrays.stream(attributes) 
         .filter(a -> a.getName().getLocalPart().equals(attributeLocalName)) 
         .findFirst().orElse(null); 
       if (null != attr) { 
        String annotationDoc = getAnnotationDocumentation(attr.getAnnotation()); 
        return annotationDoc; 
       } 
      } 
     } 
    } 

    return null; 
} 

Voici mon getAnnotationDocumentation() (qui peut être amélioré!). Vous pouvez l'utiliser pour récupérer la documentation xs: dans une annotation xs: pour les éléments et les attributs.

public static String getAnnotationDocumentation(SchemaAnnotation an) { 
    if (null != an) { 
     StringBuilder sb = new StringBuilder(); 
     XmlObject[] userInformation = an.getUserInformation(); 
     if (null != userInformation & userInformation.length > 0) { 
      for (XmlObject obj : userInformation) { 
       Node docInfo = obj.getDomNode(); 
       NodeList list = docInfo.getChildNodes(); 
       for (int i = 0; i < list.getLength(); i++) { 
        Node c = list.item(i); 
        if (c.getNodeType() == Node.TEXT_NODE) { 
         String str = c.getNodeValue(); 
         sb.append(str.trim()); 
         break; 
        } 
       } 
      } 
     } 
     return sb.toString(); 
    } 
    return null; 
} 
+0

La documentation de XMLBeans pour l'interprétation d'un schéma est vraiment mauvaise. Ce code fonctionne, il lit les annotations déclarées en tant qu'enfants des définitions de type. Malheureusement, il ne lit pas les annotations qui sont déclarées directement aux éléments (s'ils utilisent des définitions de type ou non). – Gio