Package org.eclipse.xsd

Examples of org.eclipse.xsd.XSDElementDeclaration


        assertNotNull(schema);
        final String targetNs = "http://cite.opengeospatial.org/gmlsf";
        final String featureName = "PrimitiveGeoFeature";
        QName name = new QName(targetNs, featureName);
        XSDElementDeclaration elementDeclaration = Schemas.getElementDeclaration(schema, name);
        assertNotNull(elementDeclaration);
       

        XSDComplexTypeDefinition type = (XSDComplexTypeDefinition) elementDeclaration.getType();
       
        assertEquals("PrimitiveGeoFeatureType", type.getName());
        assertEquals(targetNs, type.getTargetNamespace());
    }
View Full Code Here


                    children = new ListOrderedMap();

                    for (Iterator i = Schemas.getChildElementParticles(parent.getType(), true)
                                             .iterator(); i.hasNext();) {
                        XSDParticle particle = (XSDParticle) i.next();
                        XSDElementDeclaration child = (XSDElementDeclaration) particle.getContent();

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

                        QName childName = null;

                        if (child.getTargetNamespace() != null) {
                            childName = new QName(child.getTargetNamespace(), child.getName());
                        } else if (parent.getTargetNamespace() != null) {
                            childName = new QName(parent.getTargetNamespace(), child.getName());
                        } else if (parent.getType().getTargetNamespace() != null) {
                            childName = new QName(parent.getType().getTargetNamespace(),
                                    child.getName());
                        } else {
                            childName = new QName(null, child.getName());
                        }

                        children.put(childName, particle);
                    }
View Full Code Here

    public XSDElementDeclaration getChildElement(XSDElementDeclaration parent, QName childName) {
        OrderedMap children = (OrderedMap) children(parent);
        XSDParticle particle = (XSDParticle) children.get(childName);

        if (particle != null) {
            XSDElementDeclaration child = (XSDElementDeclaration) particle.getContent();

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

            return child;
        }

        if ("*".equals(childName.getNamespaceURI())) {
            //do a check just on local name
            ArrayList matches = new ArrayList();

            for (Iterator e = children.entrySet().iterator(); e.hasNext();) {
                Map.Entry entry = (Map.Entry) e.next();
                QName name = (QName) entry.getKey();

                if (name.getLocalPart().equals(childName.getLocalPart())) {
                    matches.add(entry.getValue());
                }
            }

            if (matches.size() == 1) {
                particle = (XSDParticle) matches.get(0);

                XSDElementDeclaration child = (XSDElementDeclaration) particle.getContent();

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

                return child;
            }
        }
View Full Code Here

        for (int i = 0; i < schemas.length; i++) {
            XSDSchema schema = schemas[i];

            for (Iterator e = schema.getElementDeclarations().iterator(); e.hasNext();) {
                XSDElementDeclaration element = (XSDElementDeclaration) e.next();

                QName qName = new QName(element.getTargetNamespace(), element.getName());
                elementIndex.put(qName, element);
            }
        }
    }
View Full Code Here

                //now we must parse the items up
                final XSDSimpleTypeDefinition itemType = type.getItemTypeDefinition();
                List parsed = new ArrayList();

                //create a pseudo declaration
                final XSDElementDeclaration element = XSDFactory.eINSTANCE
                    .createXSDElementDeclaration();
                element.setTypeDefinition(itemType);

                if (instance.getName() != null) {
                    element.setName(instance.getName());
                }

                if (instance.getNamespace() != null) {
                    element.setTargetNamespace(instance.getNamespace());
                }

                //create a new instance of the specified type
                InstanceComponentImpl theInstance = new InstanceComponentImpl() {
                        public XSDTypeDefinition getTypeDefinition() {
View Full Code Here

    public ElementHandler createElementHandler(QName qName, Handler parent, ParserHandler parser) {
        SchemaIndex index = parser.getSchemaIndex();

        //look up the element in the schema
        XSDElementDeclaration element = index.getElementDeclaration(qName);

        if (element != null) {
            return createElementHandler(element, parent, parser);
        }
View Full Code Here

        Handler parent = (Handler) handlers.peek();
        ElementHandler handler = (ElementHandler) parent.createChildHandler(qualifiedName);

        if (handler == null) {
            //look for a global element
            XSDElementDeclaration element = index.getElementDeclaration(qualifiedName);

            if (element != null) {
                handler = handlerFactory.createElementHandler(element, parent, this);
            }
        }

        if (handler == null) {
            //perform a lookup in the context for an element factory that create a child handler
            List handlerFactories = context.getComponentInstancesOfType(HandlerFactory.class);

            for (Iterator hf = handlerFactories.iterator(); (handler == null) && hf.hasNext();) {
                HandlerFactory handlerFactory = (HandlerFactory) hf.next();
                handler = handlerFactory.createElementHandler(qualifiedName, parent, this);
            }
        }

        if (handler == null || forceParserDelegate) {
            //look for ParserDelegate instances in the context to see if there is a delegate
            // around to handle this
            List adapters = Schemas.getComponentAdaptersOfType(context, ParserDelegate.class);
            //List delegates = Schemas.getComponentInstancesOfType(context,ParserDelegate.class);
            for ( Iterator a = adapters.iterator(); a.hasNext(); ) {
                ComponentAdapter adapter = (ComponentAdapter) a.next();
                ParserDelegate delegate = (ParserDelegate) adapter.getComponentInstance(context);
               
                //ParserDelegate delegate = (ParserDelegate) d.next();
                boolean canHandle = delegate instanceof ParserDelegate2 ?
                    ((ParserDelegate2)delegate).canHandle(qualifiedName, attributes, handler, parent)
                    : delegate.canHandle( qualifiedName );

                if (canHandle) {
                    //found one
                    handler = new DelegatingHandler( delegate, qualifiedName, parent );

                    DelegatingHandler dh = (DelegatingHandler) handler;
                    dh.startDocument();

                    //inject the current namespace context
                    Enumeration e = namespaces.getPrefixes();
                    while(e.hasMoreElements()) {
                        String pre = (String) e.nextElement();
                        dh.startPrefixMapping(pre, namespaces.getURI(pre));
                    }
                }
               
            }
        }
        if (handler == null) {
            //if the type only contains one type of element, just assume the
            // the element is of that type
            //if( context.getComponentInstance( Parser.Properties.PARSE_UNKNOWN_ELEMENTS ) != null) {
            if (!isStrict()) {
                if (logger.isLoggable(Level.FINE)) {
                    logger.fine("Could not find declaration for: " + qualifiedName
                        + ". Checking if containing type declares a single particle.");
                }

                if (parent.getComponent() instanceof ElementInstance) {
                    ElementInstance parentElement = (ElementInstance) parent.getComponent();
                    List childParticles = index.getChildElementParticles(parentElement
                            .getElementDeclaration());

                    if (childParticles.size() == 1) {
                        XSDParticle particle = (XSDParticle) childParticles.iterator().next();
                        XSDElementDeclaration child = (XSDElementDeclaration) particle.getContent();

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

                        handler = handlerFactory.createElementHandler(new QName(
                                    child.getTargetNamespace(), child.getName()), parent, this);
                    }
                }
            }
        }

        if (handler == null) {
            //check the case of where the namespace is wrong, do a lookup from
            // the parent just on local name
            if (!isStrict()) {
                String msg = "Could not find declaration for: " + qualifiedName
                    + ". Performing lookup by ignoring namespace";
                logger.fine(msg);

                // * = match any namespace
                handler = (ElementHandler) parent.createChildHandler(new QName("*",
                            qualifiedName.getLocalPart()));
            }
        }

        if (handler == null) {
            //check the parser flag, and just parse it anyways
            //if( context.getComponentInstance( Parser.Properties.PARSE_UNKNOWN_ELEMENTS ) != null) {
            if (!isStrict()) {
                String msg = "Could not find declaration for: " + qualifiedName
                    + ". Creating a mock element declaration and parsing anyways...";
                logger.fine(msg);

                //create a mock element declaration
                XSDElementDeclaration decl = XSDFactory.eINSTANCE.createXSDElementDeclaration();
                decl.setName(qualifiedName.getLocalPart());
                decl.setTargetNamespace(qualifiedName.getNamespaceURI());

                QName typeDefinition = null;
                if (root && rootElementType != null) {
                    typeDefinition = rootElementType;
                }
               
                //check for a type definition in the context, this is only used by
                // the parser in test mode
                if (typeDefinition == null) {
                    typeDefinition = (QName) context.getComponentInstance(
                        "http://geotools.org/typeDefinition");
                    if (typeDefinition != null) {
                        context.unregisterComponent("http://geotools.org/typeDefinition");
                    }
                }

                if (typeDefinition != null) {
                    XSDTypeDefinition type = index.getTypeDefinition(typeDefinition);

                    if (type == null) {
                        throw new NullPointerException();
                    }

                    decl.setTypeDefinition(type);
                } else {
                    //normal case, just set the type to be of string
                    XSDTypeDefinition type = index.getTypeDefinition(XS.ANYTYPE);
                    decl.setTypeDefinition(type);
                }

                handler = handlerFactory.createElementHandler(decl, parent, this);
            }
        }
View Full Code Here

        }

        encoded = new Stack();

        //add the first entry
        XSDElementDeclaration root = index.getElementDeclaration(name);

        if (root == null) {
            //check for context hint, this is only used when running the encoder
            // in test mode
            QName typeDefintion = rootElementType;
           
            if (typeDefintion == null) {
                typeDefintion =
                    (QName) context.getComponentInstance("http://geotools.org/typeDefinition");
            }

            if (typeDefintion != null) {
                XSDTypeDefinition type = index.getTypeDefinition(typeDefintion);

                if (type == null) {
                    throw new NullPointerException();
                }

                //create a mock element declaration
                root = XSDFactory.eINSTANCE.createXSDElementDeclaration();
                root.setName(name.getLocalPart());
                root.setTargetNamespace(name.getNamespaceURI());
                root.setTypeDefinition(type);
            }
        }

        if (root == null) {
            String msg = "Could not find element declaration for:" + name;
            throw new IllegalArgumentException(msg);
        }

        encoded.add(new EncodingEntry(object, root, null));

        while (!encoded.isEmpty()) {
            EncodingEntry entry = (EncodingEntry) encoded.peek();

            if (entry.encoding != null) {
                //element has been started, get the next child
                if (!entry.children.isEmpty()) {
                    Object[] child = (Object[]) entry.children.get(0);
                    XSDElementDeclaration element = (XSDElementDeclaration) child[0];
                    Iterator itr = (Iterator) child[1];

                    if (itr.hasNext()) {
                        Object next = itr.next();
                       
                        //here we check for instanceof EncoderDelegate
                        if ( next instanceof EncoderDelegate ) {
                            //do not add entry to the stack, just delegate to encode
                            try {
                                ((EncoderDelegate) next).encode(handler);
                            }
                            catch (Exception e) {
                                throw new RuntimeException( e );
                            }
                        }
                        else {
                            //add the next object to be encoded to the stack
                            encoded.push(new EncodingEntry(next, element,entry));                           
                        }
                    } else {
                        //iterator done, close it
                        Object source = child[2];
                        closeIterator(itr,source);
                       
                        //this child is done, remove from child list
                        entry.children.remove(0);
                    }
                } else {
                    // no more children, finish the element
                    end(entry.encoding, entry.element);
                    encoded.pop();
                   
                    //clean up the entry
                    entry.object = null;
                    entry.element = null;
                    entry.encoding = null;
                    entry.children = null;
                    entry.parent = null;
                }
            } else {
                //start the encoding of the entry

                //first make sure the element is not abstract
                if (entry.element.isAbstract()) {
                    // look for a non abstract substitute - substitution groups are subject to
                    // changes over time, so we make a copy to avoid being hit with a ConcurrentModificationException
                    List sub = safeCopy(entry.element.getSubstitutionGroup());


                    if (sub.size() > 0) {
                        //match up by type
                        List matches = new ArrayList();

                        for (Iterator s = sub.iterator(); s.hasNext();) {
                            XSDElementDeclaration e = (XSDElementDeclaration) s.next();

                            if (e == null || e.equals(entry.element)) {
                                continue;
                            }

                            if (e.getName() == null) {
                                continue;
                            }

                            //look up hte binding
                            Binding binding = bindingLoader.loadBinding(new QName(
                                        e.getTargetNamespace(), e.getName()), context);

                            if (binding == null) {
                                //try the type
                                XSDTypeDefinition type = e.getType();

                                if (type == null || type.getName() == null) {
                                    continue;
                                }

                                binding = bindingLoader.loadBinding(new QName(
                                            type.getTargetNamespace(), type.getName()), context);
                            }

                            if (binding == null) {
                                continue;
                            }

                            if (binding.getType() == null) {
                                logger.warning( "Binding: " + binding.getTarget() + " returns null type.");
                                continue;
                            }

                            //match up the type
                            if (binding.getType().isAssignableFrom(entry.object.getClass())) {
                                //we have a match, store as an (element,binding) tuple
                                matches.add(new Object[] { e, binding });
                            }
                        }

                        //if one, we are gold
                        if (matches.size() == 1) {
                            entry.element = (XSDElementDeclaration) ((Object[]) matches.get(0))[0];
                        }
                        //if multiple we have a problem
                        else if (matches.size() > 0) {
                            if (logger.isLoggable(Level.FINE)) {
                                StringBuffer msg = new StringBuffer(
                                        "Found multiple non-abstract bindings for ");
                                msg.append(entry.element.getName()).append(": ");

                                for (Iterator m = matches.iterator(); m.hasNext();) {
                                    msg.append(m.next().getClass().getName());
                                    msg.append(", ");
                                }

                                logger.fine(msg.toString());
                            }

                            //try sorting by the type of the binding
                            Collections.sort(matches,
                                new Comparator() {
                                    public int compare(Object o1, Object o2) {
                                        Object[] match1 = (Object[]) o1;
                                        Object[] match2 = (Object[]) o2;

                                        Binding b1 = (Binding) match1[1];
                                        Binding b2 = (Binding) match2[1];

                                        if ( b1.getType() != b2.getType() ) {
                                            if (b2.getType().isAssignableFrom(b1.getType())) {
                                                return -1;
                                            }
   
                                            if (b1.getType().isAssignableFrom(b2.getType())) {
                                                return 1;
                                            }
                                        }
                                       
                                        //use binding comparability
                                        if (b1 instanceof Comparable) {
                                            return ((Comparable) b1).compareTo(b2);
                                        }

                                        if (b2 instanceof Comparable) {
                                            return -1 * ((Comparable) b2).compareTo(b1);
                                        }

                                        return 0;
                                    }
                                });
                        }

                        if (matches.size() > 0) {
                            entry.element = (XSDElementDeclaration) ((Object[]) matches.get(0))[0];
                        }

                        //if zero, just use the abstract element
                    }
                }

                if (entry.element.isAbstract()) {
                    logger.fine(entry.element.getName() + " is abstract");
                }

                entry.encoding = entry.parent != null ?
                        (Element) encode(entry.object, entry.element, entry.parent.element.getType()) :
                        (Element) encode(entry.object, entry.element);

                //add any more attributes
                List attributes = index.getAttributes(entry.element);

                for (Iterator itr = attributes.iterator(); itr.hasNext();) {
                    XSDAttributeDeclaration attribute = (XSDAttributeDeclaration) itr.next();

                    //do not encode the attribute if it has already been
                    // encoded by the parent
                    String ns = attribute.getTargetNamespace();
                    String local = attribute.getName();

                    if ((entry.encoding.getAttributeNS(ns, local) != null)
                            && !"".equals(entry.encoding.getAttributeNS(ns, local))) {
                        continue;
                    }

                    //get the object(s) for this attribute
                    GetPropertyExecutor executor = new GetPropertyExecutor(entry.object, attribute);
                   
                    BindingVisitorDispatch.walk(object, bindingWalker, entry.element, executor, context);

                    if (executor.getChildObject() != null) {
                        //encode the attribute
                        Attr attr = (Attr) encode(executor.getChildObject(), attribute);

                        if (attr != null) {
                            entry.encoding.setAttributeNodeNS(attr);
                        }
                    }
                }

                //write out the leading edge of the element
                if (schemaLocations != null) {
                    //root element, add schema locations if set
                    if (!schemaLocations.isEmpty()) {
                        //declare the schema instance mapping
                        serializer.startPrefixMapping("xsi", XSDUtil.SCHEMA_INSTANCE_URI_2001);
                        serializer.endPrefixMapping("xsi");
                        namespaces.declarePrefix("xsi", XSDUtil.SCHEMA_INSTANCE_URI_2001);

                        StringBuffer schemaLocation = new StringBuffer();

                        for (Iterator e = schemaLocations.entrySet().iterator(); e.hasNext();) {
                            Map.Entry tuple = (Map.Entry) e.next();
                            String namespaceURI = (String) tuple.getKey();
                            String location = (String) tuple.getValue();

                            schemaLocation.append(namespaceURI + " " + location);

                            if (e.hasNext()) {
                                schemaLocation.append(" ");
                            }
                        }

                        entry.encoding.setAttributeNS(XSDUtil.SCHEMA_INSTANCE_URI_2001,
                            "xsi:schemaLocation", schemaLocation.toString());
                    }

                    schemaLocations = null;
                }

                start(entry.encoding, entry.element);

                //TODO: this method of getting at properties wont maintain order very well, need
                // to come up with a better system that is capable of hanlding feature types
                for (Iterator pe = propertyExtractors.iterator(); pe.hasNext();) {
                    PropertyExtractor propertyExtractor = (PropertyExtractor) pe.next();

                    if (propertyExtractor.canHandle(entry.object)) {
                        List extracted = propertyExtractor.properties(entry.object, entry.element);
O:
                        for (Iterator e = extracted.iterator(); e.hasNext();) {
                            Object[] tuple = (Object[]) e.next();
                            XSDParticle particle = (XSDParticle) tuple[0];
                            XSDElementDeclaration child = (XSDElementDeclaration) particle
                                .getContent();

                            //check for a comment
                            if ((child != null)
                                    && (COMMENT.getNamespaceURI().equals(child.getTargetNamespace()))
                                    && COMMENT.getLocalPart().equals(child.getName())) {
                                comment(child.getElement());

                                continue;
                            }

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

                            //do not encode the element if the parent has already
                            // been encoded by the parent
                            String ns = child.getTargetNamespace();
                            String local = child.getName();

                            for (int i = 0; i < entry.encoding.getChildNodes().getLength(); i++) {
                                Node node = entry.encoding.getChildNodes().item(i);

                                if (node instanceof Element) {
                                    if (ns != null) {
                                        if (ns.equals(node.getNamespaceURI())
                                                && local.equals(node.getLocalName())) {
                                            continue O;
                                        }
                                    } else {
                                        if (local.equals(node.getLocalName())) {
                                            continue O;
                                        }
                                    }
                                }
                            }

                            Object obj = tuple[1];

                            // if the value is null, can we skip it? Or do we have to go out
                            // with an non empty element with xs:nil?
                            if (obj == null) {
                                if (particle.getMinOccurs() == 0) {
                                    // just skip it
                                    continue;
                                } else if(!child.isNillable()){
                                    // log an error and skip the element, but we're encoding
                                    // something invalid
                                    logger.fine("Property " + ns + ":" + local
                                        + " not found but minoccurs > 0 ");
                                    //skip this regardless
View Full Code Here

        //find the type in the schema
        XSDTypeDefinition type = schemaIndex.getTypeDefinition(qualifiedTypeName);

        if (type == null) {
            //type not found, do a check for an element, and use its type
            XSDElementDeclaration e = schemaIndex.getElementDeclaration(qualifiedTypeName);

            if (e != null) {
                type = e.getTypeDefinition();
            }
        }

        if (type == null) {
            if (featureType instanceof SimpleFeatureType) {
                // could not find the feature type in the schema, create a mock one
                LOGGER.fine("Could find type for " + typeName
                        + " in the schema, generating type from feature.");
                type = createXmlTypeFromFeatureType((SimpleFeatureType) featureType, schemaIndex,
                        toFilter);
            } else {
                // look for an element declaration smuggled in the UserData map.
                XSDElementDeclaration e = (XSDElementDeclaration) feature.getDescriptor()
                        .getUserData().get(XSDElementDeclaration.class);
                if (e != null) {
                    type = e.getTypeDefinition();
                } else {
                    throw new RuntimeException("Could not find type for " + qualifiedTypeName
                            + " in schema");
                }
            }
        }

        List particles = Schemas.getChildElementParticles(type, true);
        List properties = new ArrayList();

    O:  for (int i = 0; i < particles.size(); i++) {
            XSDParticle particle = (XSDParticle) particles.get(i);
            XSDElementDeclaration attribute = (XSDElementDeclaration) particle.getContent();

            if (attribute.isElementDeclarationReference()) {
                attribute = attribute.getResolvedElementDeclaration();
            }
           
            if (gml.qName("boundedBy")
                    .equals(new QName(attribute.getTargetNamespace(), attribute.getName()))) {
                BoundingBox bounds = getBoundedBy(feature, configuration);
                if (bounds != null) {
                    properties.add(new Object[] { particle, bounds });
                }
            } else if (featureType instanceof SimpleFeatureType) {
                // first simple feature hack, if the schema "overrides" gml attributes like
                // name and description, ignore the gml version
                boolean skip = false;
                if (gml.getNamespaceURI().equals(attribute.getTargetNamespace())) {
                    for (int j = i+1; j < particles.size(); j++) {
                        XSDParticle particle2 = (XSDParticle) particles.get(j);
                        XSDElementDeclaration attribute2 = (XSDElementDeclaration) particle2.getContent();
                        if (attribute2.isElementDeclarationReference()) {
                            attribute2 = attribute2.getResolvedElementDeclaration();
                        }
                        if (attribute2.getName().equals(attribute.getName())) {
                            skip = true;
                            break;
                        }
                    }
                }
View Full Code Here

        boolean includeParents) {
        List particles = getChildElementParticles(type, includeParents);

        for (Iterator p = particles.iterator(); p.hasNext();) {
            XSDParticle particle = (XSDParticle) p.next();
            XSDElementDeclaration element = (XSDElementDeclaration) particle.getContent();

            if (element.isElementDeclarationReference()) {
                element.getResolvedElementDeclaration();
            }

            if (name.equals(element.getName())) {
                return particle;
            }
        }

        return null;
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.