public void testXercesIncomplete() throws Exception {
   XSModel xsModel = getXSModel("testIncomplete.xml", "test.xsd");
   XSElementDeclaration elementDeclaration = xsModel.getElementDeclaration("a", "");
   XSComplexTypeDefinition typeDefinition =
       (XSComplexTypeDefinition) elementDeclaration.getTypeDefinition();
   CMBuilder cmBuilder = new CMBuilder(new CMNodeFactory());
   XSCMValidator validator = cmBuilder.getContentModel((XSComplexTypeDecl) typeDefinition, true);
   int[] ints = validator.startContentModel();
   Vector vector = validator.whatCanGoHere(ints);
   XSElementDecl o = (XSElementDecl) vector.get(0);
   assertEquals("b", o.getName());
 }
  public void testXercesForCompletion() throws Exception {
    XSModel xsModel = getXSModel("testCompletion.xml", "test.xsd");
    PsiElement element = myFixture.getFile().findElementAt(getEditor().getCaretModel().getOffset());
    XmlTag tag = PsiTreeUtil.getParentOfType(element, XmlTag.class);

    assert tag != null;
    XSElementDeclaration elementDeclaration =
        xsModel.getElementDeclaration(tag.getLocalName(), tag.getNamespace());
    XSComplexTypeDefinition typeDefinition =
        (XSComplexTypeDefinition) elementDeclaration.getTypeDefinition();
    CMBuilder cmBuilder = new CMBuilder(new CMNodeFactory());
    XSCMValidator validator = cmBuilder.getContentModel((XSComplexTypeDecl) typeDefinition, true);
    int[] ints = validator.startContentModel();
    Vector vector = validator.whatCanGoHere(ints);
    XSElementDecl o = (XSElementDecl) vector.get(0);
    assertEquals("b", o.getName());
  }
示例#3
0
  public static void main(String args[]) {
    if (args.length != 1) {
      System.out.println("usage: java xerces.ParseXSD xsdfilepath");
      System.exit(1);
    }

    System.out.println("Going to parse schema " + args[0]);

    // get DOM implementation
    DOMImplementationAS domImpl =
        (DOMImplementationAS) ASDOMImplementationImpl.getDOMImplementation();

    // create a new parser
    DOMASBuilder parser = domImpl.createDOMASBuilder();

    ASModel asmodel = null;

    try {
      asmodel = parser.parseASURI(args[0]);
    } catch (Exception e) {
      e.printStackTrace();
      System.exit(1);
    }

    ASModelImpl model = (ASModelImpl) asmodel;
    Vector models = model.getInternalASModels();
    SchemaGrammar[] grammars = new SchemaGrammar[models.size()];
    for (int i = 0; i < models.size(); i++) {
      grammars[i] = ((ASModelImpl) models.elementAt(i)).getGrammar();
    }

    XSModel xs = new XSModelImpl(grammars);

    XSNamedMap elemmap = xs.getComponents(XSConstants.ELEMENT_DECLARATION);

    // assume the first top element is what we want
    XSElementDecl topElem = (XSElementDecl) elemmap.item(0);

    System.out.println("Top Element is: " + topElem.getName());

    echo(topElem.getAnnotationAttrs());

    XSComplexTypeDefinition ct = (XSComplexTypeDefinition) topElem.getTypeDefinition();

    // attributes
    XSObjectList attruses = ct.getAttributeUses();
    if (null != attruses) {
      for (int i = 0; i < attruses.getLength(); i++) {
        XSAttributeUse attruse = (XSAttributeUse) attruses.item(i);
        XSAttributeDeclaration attr = attruse.getAttrDeclaration();

        System.out.println("has an attribute: " + attr.getName());
        echo(attr.getAnnotationAttrs());
      }
    }

    XSParticle particle = ct.getParticle();

    XSTerm term = particle.getTerm();
    XSModelGroup topGroup = (XSModelGroup) term;

    // traverse the group
    XSObjectList objs = topGroup.getParticles();

    for (int i = 0; i < objs.getLength(); i++) {
      XSObject obj = objs.item(i);
      XSParticle p = (XSParticle) obj;
      XSTerm t = p.getTerm();

      // we got nested group
      if (t instanceof XSModelGroup) {
        System.out.println("has a group");
        XSModelGroup g = (XSModelGroup) t;

        echo(g.getAnnotationAttrs());
      }

      if (t instanceof XSElementDeclaration) {
        XSElementDeclaration e = (XSElementDeclaration) t;
        XSTypeDefinition et = e.getTypeDefinition();

        System.out.println("has an element: " + e.getName());

        if (et.getTypeCategory() == XSTypeDefinition.COMPLEX_TYPE) {
          System.out.println(e.getName() + " is complex type.");
          echo(e.getAnnotationAttrs());
        } else {
          System.out.println(e.getName() + " is simple type.");
          echo(e.getAnnotationAttrs());
        }
      }
    }
  }