Package org.eclipse.xsd

Examples of org.eclipse.xsd.XSDElementDeclaration


        List children = encoder.getSchemaIndex().getChildElementParticles(element);

O:
        for (Iterator itr = children.iterator(); itr.hasNext();) {
            XSDParticle particle = (XSDParticle) itr.next();
            XSDElementDeclaration child = (XSDElementDeclaration) particle.getContent();

            if (child.isElementDeclarationReference()) {
                child = child.getResolvedElementDeclaration();
            }

            //get the object(s) for this element
            GetPropertyExecutor executor = new GetPropertyExecutor(object, child);

            BindingVisitorDispatch.walk(object, encoder.getBindingWalker(), element, executor,
                    context);

            if (executor.getChildObject() != null) {
                properties.add(new Object[] { particle, executor.getChildObject() });
            }
        }

        //second, get the properties which cannot be infereed from the schema
        GetPropertiesExecutor executor = new GetPropertiesExecutor(object,element);

        BindingVisitorDispatch.walk(object, encoder.getBindingWalker(), element, executor, context);

        if (!executor.getProperties().isEmpty()) {
            //group into a map of name, list
            MultiHashMap map = new MultiHashMap();

            for (Iterator p = executor.getProperties().iterator(); p.hasNext();) {
                Object[] property = (Object[]) p.next();
                map.put(property[0], property[1]);
            }

            //turn each map entry into a particle
            HashMap particles = new HashMap();

            for (Iterator e = map.entrySet().iterator(); e.hasNext();) {
                Map.Entry entry = (Map.Entry) e.next();
               
                //key could be a name or a particle
                if ( entry.getKey() instanceof XSDParticle ) {
                    XSDParticle particle = (XSDParticle) entry.getKey();
                    particles.put( Schemas.getParticleName( particle), particle );
                    continue;
                }
               
                QName name = (QName) entry.getKey();
                Collection values = (Collection) entry.getValue();

                //check for comment
                if (Encoder.COMMENT.equals(name)) {
                    //create a dom element which text nodes for the comments
                    Element comment = encoder.getDocument()
                                             .createElement(Encoder.COMMENT.getLocalPart());

                    for (Iterator v = values.iterator(); v.hasNext();) {
                        comment.appendChild(encoder.getDocument().createTextNode(v.next().toString()));
                    }

                    XSDParticle particle = XSDFactory.eINSTANCE.createXSDParticle();

                    XSDElementDeclaration elementDecl = XSDFactory.eINSTANCE
                        .createXSDElementDeclaration();
                    elementDecl.setTargetNamespace(Encoder.COMMENT.getNamespaceURI());
                    elementDecl.setName(Encoder.COMMENT.getLocalPart());
                    elementDecl.setElement(comment);

                    particle.setContent(elementDecl);
                    particles.put(name, particle);

                    continue;
                }

                //find hte element
                XSDElementDeclaration elementDecl = encoder.getSchemaIndex()
                                                           .getElementDeclaration(name);

                if (elementDecl == null) {
                    //look for the element declaration as a particle of the containing type
                    XSDParticle particle =
                        Schemas.getChildElementParticle(element.getType(), name.getLocalPart(), true);
                    if (particle != null) {
                        particles.put(name, particle);
                        continue;
                    }
                }

                if (elementDecl == null) {
                    //TODO: resolving like this will return an element no
                    // matter what, modifying the underlying schema, this might
                    // be dangerous. What we shold do is force the schema to
                    // resolve all of it simports when the encoder starts
                    elementDecl = encoder.getSchema()
                                         .resolveElementDeclaration(name.getNamespaceURI(),
                            name.getLocalPart());
                }

                //look for a particle in the containing type which is either
                // a) a base type of the element
                // b) in the same subsittuion group
                // if found use the particle to dervice multiplicity
                XSDParticle reference = null;
                for ( Iterator p = Schemas.getChildElementParticles(element.getType(), true).iterator(); p.hasNext(); ) {
                    XSDParticle particle = (XSDParticle) p.next();
                    XSDElementDeclaration el = (XSDElementDeclaration) particle.getContent();
                    if ( el.isElementDeclarationReference() ) {
                        el = el.getResolvedElementDeclaration();
                    }
                   
                    if ( Schemas.isBaseType(elementDecl, el) ) {
                        reference = particle;
                        break;
                    }
                }
               
                //wrap the property in a particle
                XSDParticle particle = XSDFactory.eINSTANCE.createXSDParticle();
                XSDElementDeclaration wrapper = XSDFactory.eINSTANCE.createXSDElementDeclaration();
                wrapper.setResolvedElementDeclaration( elementDecl );
                particle.setContent(wrapper);
                //particle.setContent(elementDecl);

                //if there is a reference, derive multiplicity
                if ( reference != null ) {
                    particle.setMaxOccurs( reference.getMaxOccurs() );
                }
                else {
                    //dervice from collection
                    if ( values.size() > 1) {
                        //make a multi property
                        particle.setMaxOccurs(-1);
                    } else {
                        //single property
                        particle.setMaxOccurs(1);
                    }   
                }
               
                particles.put(name, particle);
            }

            //process the particles in order in which we got the properties
            for (Iterator p = executor.getProperties().iterator(); p.hasNext();) {
                Object[] property = (Object[]) p.next();
                Collection values = (Collection) map.get( property[0] );
               
                QName name;
                if ( property[0instanceof XSDParticle ) {
                    name = Schemas.getParticleName( (XSDParticle) property[0] );
                }
                else {
                    name = (QName) property[0];
                }

                XSDParticle particle = (XSDParticle) particles.get(name);

                if (particle == null) {
                    continue; //already processed, must be a multi property
                }
               
                if (values.size() > 1) {
                    //add as is, the encoder will unwrap
                    properties.add(new Object[] { particle, values });
                } else {
                    //unwrap it
                    properties.add(new Object[] { particle, values.iterator().next() });
                }

                //done with this particle
                particles.remove(name);
            }
        }
       
        //return properties;       
        if (properties.size()<=1){
            return properties;
        }
       
        /*
         feature properties in the "properties" list may not be in the same order as they appear in the schema,
         because in the above implementation, simple attributes and complex attributes are processed separately.
         
         to maintain the feature properties order, sort the properties to their original order as in "children" list              
        */
        if (object instanceof ComplexAttributeImpl && propertiesSortable(properties, children)) {
            List sortedProperties = new ArrayList();
           
            //sort properties according to their XSDParticle order in "children"
            for (int i = 0; i<children.size(); i++) {
                XSDParticle particle = (XSDParticle) children.get(i);
                XSDElementDeclaration child = (XSDElementDeclaration) particle.getContent();
                if (child.getResolvedElementDeclaration() != null) {
                    child = child.getResolvedElementDeclaration();
                }

                for (Iterator itr = properties.iterator(); itr.hasNext();) {
                    Object[] prop = (Object[]) itr.next();
                    XSDParticle part = (XSDParticle) prop[0];
                    XSDElementDeclaration partContent = (XSDElementDeclaration) part.getContent();
                    if (partContent.getResolvedElementDeclaration() != null) {
                        partContent = partContent.getResolvedElementDeclaration();
                    }
                    if (child.getName().equals(partContent.getName())
                            && ((child.getTargetNamespace() != null && partContent
                                    .getTargetNamespace() != null) ? child.getTargetNamespace()
                                    .equals(partContent.getTargetNamespace()) : true)) {
                        sortedProperties.add(prop);
                        properties.remove(prop);
                        i--;
                        break;
                    }
View Full Code Here


        }

        for (Iterator itr = properties.iterator(); itr.hasNext();) {
            Object[] prop = (Object[]) itr.next();
            XSDParticle part = (XSDParticle) prop[0];
            XSDElementDeclaration partContent = (XSDElementDeclaration) part.getContent();
            if (partContent.getResolvedElementDeclaration() != null) {
                partContent = partContent.getResolvedElementDeclaration();
            }
            boolean notFound = true;
            for (int i = 0; i < children.size(); i++) {
                XSDParticle particle = (XSDParticle) children.get(i);
                XSDElementDeclaration child = (XSDElementDeclaration) particle.getContent();
                if (child.getResolvedElementDeclaration() != null) {
                    child = child.getResolvedElementDeclaration();
                }
                if (child.getName().equals(partContent.getName()) &&
                        (child.getTargetNamespace()==null? partContent.getTargetNamespace()==null :
                       child.getTargetNamespace().equals(partContent.getTargetNamespace()))) {
                    notFound = false;
                    break;
                }
            }
            if (notFound){
View Full Code Here

     */
    public static SimpleFeature parseFeature(ElementInstance instance, Node node, Object value,
        FeatureTypeCache ftCache, BindingWalkerFactory bwFactory)
        throws Exception {
        //get the definition of the element
        XSDElementDeclaration decl = instance.getElementDeclaration();

        //special case, if the declaration is abstract it is probably "_Feautre"
        // which means we are parsing an elemetn which could not be found in the
        // schema, so instaed of using the element declaration to build the
        // type, just use the node given to us
        SimpleFeatureType sfType = null;
        FeatureType fType = null;
       
        if (!decl.isAbstract()) {
            //first look in cache
            fType = ftCache.get(new NameImpl(decl.getTargetNamespace(), decl.getName()));

            if (fType == null || fType instanceof SimpleFeatureType) {
                sfType = (SimpleFeatureType) fType;
            } else {
                // TODO: support parsing of non-simple GML features
View Full Code Here

        // actual xml schema type
        List children = Schemas.getChildElementParticles(element.getType(), true);

        for (Iterator itr = children.iterator(); itr.hasNext();) {
            XSDParticle particle = (XSDParticle) itr.next();
            XSDElementDeclaration property = (XSDElementDeclaration) particle.getContent();

            if (property.isElementDeclarationReference()) {
                property = property.getResolvedElementDeclaration();
            }

            final ArrayList bindings = new ArrayList();
            BindingWalker.Visitor visitor = new BindingWalker.Visitor() {
                    public void visit(Binding binding) {
                        bindings.add(binding);
                    }
                };

            bwFactory.walk(property, visitor);

            if (bindings.isEmpty()) {
                // could not find a binding, use the defaults
                LOGGER.warning( "Could not find binding for " + property.getQName() + ", using XSAnyTypeBinding." );
                bindings.add( new XSAnyTypeBinding() );
            }

            // get hte last binding in the chain to execute
            Binding last = ((Binding) bindings.get(bindings.size() - 1));
            Class theClass = last.getType();

            if (theClass == null) {
                throw new RuntimeException("binding declares null type: " + last.getTarget());
            }

            // get the attribute properties
            int min = particle.getMinOccurs();
            int max = particle.getMaxOccurs();

            //check for uninitialized values
            if (min == -1) {
                min = 0;
            }

            if (max == -1) {
                max = 1;
            }

            // if the next property is of type geometry, let's set its CRS
            if (Geometry.class.isAssignableFrom(theClass) && crs != null) {
                ftBuilder.crs(crs);
            }

            // create the type
            ftBuilder.minOccurs(min).maxOccurs(max).add(property.getName(), theClass);

            //set the default geometry explicitly. Note we're comparing the GML namespace
            //with String.startsWith to catch up on the GML 3.2 namespace too, which is hacky.
            final String propNamespace = property.getTargetNamespace();
            if (Geometry.class.isAssignableFrom(theClass)
                    && (propNamespace == null || !propNamespace.startsWith(GML.NAMESPACE))) {
                //only set if non-gml, we do this because of "gml:location",
                // we dont want that to be the default if the user has another
                // geometry attribute
                if (ftBuilder.getDefaultGeometry() == null) {
                    ftBuilder.setDefaultGeometry(property.getName());
                }
            }
        }

        return ftBuilder.buildFeatureType();
View Full Code Here

        if (type.getName() != null) {
            bindingName = new QName(type.getTargetNamespace(), type.getName());
        } else {
            //anonymous type, does it belong to a global element
            for (Iterator e = type.getSchema().getElementDeclarations().iterator(); e.hasNext();) {
                XSDElementDeclaration element = (XSDElementDeclaration) e.next();

                if (type.equals(element.getAnonymousTypeDefinition())) {
                    //TODO: this naming convention for anonymous types could conflict with
                    // other types in the schema
                    bindingName = new QName(type.getTargetNamespace(), "_" + element.getName());

                    break;
                }
            }

            if (bindingName == null) {
                //do we have a containing type?
                if (container != null) {
                    XSDNamedComponent base = container;
                   
                    //the container itself could be an anonymous type, check for
                    // a named containing element
                    if ( container.getName() == null ) {
                        if ( container.getContainer() instanceof XSDElementDeclaration ) {
                            XSDElementDeclaration e = (XSDElementDeclaration) container.getContainer();
                           
                            //only do this if the containing element is global
                            if ( e.isGlobal() ) {
                                base = e;
                            }
                        }
                    }
                   
                    //get the anonymous element, and look it up in the container type
                    if (type.getContainer() instanceof XSDElementDeclaration) {
                        XSDElementDeclaration anonymous = (XSDElementDeclaration) type.getContainer();
                        XSDParticle particle = Schemas.getChildElementParticle(container,
                                anonymous.getName(), true);

                        if (particle != null) {
                            bindingName = new QName(base.getTargetNamespace(),
                                    base.getName() + "_" + anonymous.getName());
                        }
                    }
                }
            }
View Full Code Here

        }
        if (!(notification.getNewValue() instanceof XSDElementDeclaration)) {
                return;
        }
           
        XSDElementDeclaration el = (XSDElementDeclaration) notification.getNewValue();
        XSDElementDeclaration e = target;
           
        while(e != null) {
            synchronized(e) {
                ArrayList<Integer> toremove = new ArrayList();
                for (int i = 0; i < e.getSubstitutionGroup().size(); i++) {
                    XSDElementDeclaration se = (XSDElementDeclaration) e.getSubstitutionGroup().get(i);
                    if (se == null || (Utilities.equals(el.getTargetNamespace(), se.getTargetNamespace())
                            && Utilities.equals(el.getName(), se.getName()))) {
                        toremove.add(i);
                    }
                }
               
                //iterate back in reverse order and skip the last element as to keep the latest
                // version of the element
                ArrayList<XSDElementDeclaration> removed = new ArrayList();
                for (int i = toremove.size()-2; i > -1; i--) {
                    XSDElementDeclaration se = (XSDElementDeclaration)
                        e.getSubstitutionGroup().remove(toremove.get(i).intValue());
                    removed.add(e);
                }
               
                //set the removed elements sub affiliation to a clone of the actual element
                for (XSDElementDeclaration se : removed) {
                    if (se != null && e.equals(se.getSubstitutionGroupAffiliation())) {
                        XSDElementDeclaration clone =
                            (XSDElementDeclaration) e.cloneConcreteComponent(false, false);
                        clone.setTargetNamespace(GML.NAMESPACE);
                       
                        se.setSubstitutionGroupAffiliation(clone);
                    }
                }
            }
View Full Code Here

        return encode( object, component, null );
    }
   
    protected Node encode(Object object, XSDNamedComponent component, XSDTypeDefinition container ) {
        if (component instanceof XSDElementDeclaration) {
            XSDElementDeclaration element = (XSDElementDeclaration) component;

            return encoder.encode(object, element, doc, container );
        } else if (component instanceof XSDAttributeDeclaration) {
            XSDAttributeDeclaration attribute = (XSDAttributeDeclaration) component;
View Full Code Here

            Node node = (Node) element.getChildNodes().item(i);

            if (node instanceof Element) {
                Element child = (Element) node;
                QName childName = new QName(child.getNamespaceURI(), child.getNodeName());
                XSDElementDeclaration childDecl = declaration != null ?
                    Schemas.getChildElementDeclaration(declaration, childName) : null;
                start(child, childDecl);
                end(child, childDecl);
            }
        }
View Full Code Here

    }

    private Handler getChildHandlerInternal(QName qName) {
        SchemaIndex index = parser.getSchemaIndex();

        XSDElementDeclaration element = index.getChildElement(content, qName);

        if (element != null) {
            //TODO: determine wether the element is complex or simple, and create
            ElementHandler handler = parser.getHandlerFactory()
                                           .createElementHandler(element, this, parser);

            return handler;
        }

        //could not find the element as a direct child of the parent, check
        // for a global element, and then check its substituation group
        element = index.getElementDeclaration(qName);

        if (element != null) {
            XSDElementDeclaration sub = element.getSubstitutionGroupAffiliation();

            if (sub != null) {
                QName subQName = new QName(sub.getTargetNamespace(), sub.getName());
                Handler handler = getChildHandlerInternal(subQName);

                if (handler != null) {
                    //this means that hte element is substituatable for an
                    // actual child. now we have have choice, do we return
View Full Code Here

        return factory.loadBinding(xs(name), new DefaultPicoContainer());
    }

    public ElementInstance element(String text, QName qname) {
        // create a fake element declaration and element instance
        XSDElementDeclaration declaration = XSDFactory.eINSTANCE.createXSDElementDeclaration();
        declaration.setTypeDefinition(xsdSimple(qname.getLocalPart()));

        ElementInstance element = new ElementImpl(declaration);
        element.setText(text);

        return element;
View Full Code Here

TOP

Related Classes of org.eclipse.xsd.XSDElementDeclaration

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.