Example usage for javax.xml.stream Location getLineNumber

List of usage examples for javax.xml.stream Location getLineNumber

Introduction

In this page you can find the example usage for javax.xml.stream Location getLineNumber.

Prototype

int getLineNumber();

Source Link

Document

Return the line number where the current event ends, returns -1 if none is available.

Usage

From source file:org.openvpms.tools.data.loader.DataLoader.java

/**
 * Creates a new load state for the specified data.
 *
 * @param data   the data/*from   w  w w  .ja  va  2  s .co m*/
 * @param parent the parent state. May be <tt>null</tt>
 * @param path   a path representing the data source, for logging purposes
 * @return a new load state
 */
private LoadState create(Data data, LoadState parent, String path) {
    String shortName = data.getShortName();
    ArchetypeDescriptor descriptor = service.getArchetypeDescriptor(shortName);
    Location location = data.getLocation();
    if (descriptor == null) {
        throw new ArchetypeDataLoaderException(InvalidArchetype, location.getLineNumber(),
                location.getColumnNumber(), shortName);
    }

    IMObject object = service.create(descriptor.getType());
    if (object == null) {
        throw new ArchetypeDataLoaderException(InvalidArchetype, location.getLineNumber(),
                location.getColumnNumber(), shortName);
    }
    cache.add(object, data.getId());
    return new LoadState(parent, object, descriptor, path, data.getLocation().getLineNumber());
}

From source file:org.plasma.sdo.xml.StreamUnmarshaller.java

private StreamObject read(XMLStreamReader streamReader) throws XMLStreamException, UnmarshallerException {
    int eventType;
    StreamObject root = null;//  w  w  w.  jav  a  2s .  c o m

    while (streamReader.hasNext()) {
        eventType = streamReader.next();
        XMLEvent event = allocateXMLEvent(streamReader);
        switch (eventType) {
        case XMLEvent.START_ELEMENT:
            QName name = event.asStartElement().getName();
            String uri = name.getNamespaceURI();
            if (uri != null && uri.trim().length() > 0)
                this.currentNamespaceUri = uri.trim();
            if (stack.size() == 0) {
                String typeName = name.getLocalPart();
                PlasmaType type = (PlasmaType) PlasmaTypeHelper.INSTANCE.getType(currentNamespaceUri, typeName);
                if (log.isDebugEnabled())
                    log.debug("unmarshaling root: " + type.getURI() + "#" + type.getName());
                root = new StreamObject(type, name, event.getLocation());
                stack.push(root);
            } else {
                StreamObject sreamObject = (StreamObject) stack.peek();
                PlasmaType type = sreamObject.getType();
                QName elemName = event.asStartElement().getName();
                PlasmaProperty property = getPropertyByLocalName(event, type, elemName.getLocalPart());
                if (property.getType().isDataType()) {
                    // still need characters event to populate this property. We expect to
                    // pop this back off the stack after its characters event is processed below
                    stack.push(new StreamProperty((PlasmaType) property.getType(), property, name,
                            event.getLocation()));
                    break; // we expect no attributes !!
                } else {
                    if (log.isDebugEnabled())
                        log.debug("unmarshaling: " + property.getType().getURI() + "#"
                                + property.getType().getName());
                    // The source is a reference property but we don't know at this point
                    // whether its a reference object.

                    // Push the new DO so we can value its contents on subsequent events
                    stack.push(new StreamObject((PlasmaType) property.getType(), property, name,
                            event.getLocation()));
                }
            }
            StreamObject streamObject = (StreamObject) stack.peek();
            readAttributes(event, streamObject);
            break;
        case XMLEvent.END_ELEMENT:
            StreamNode node = stack.pop();
            if (stack.size() == 0)
                break;

            // link stream objects creating an initial graph
            StreamObject other = (StreamObject) stack.peek();
            if (node instanceof StreamProperty) {
                StreamProperty streamProp = (StreamProperty) node;
                if (this.charbuf.length() > 0) {
                    readCharacters(streamProp, this.charbuf.toString(), event);
                    this.charbuf.setLength(0);
                }
                link((StreamProperty) node, other);
            } else {
                link((StreamObject) node, other);
            }
            break;
        case XMLEvent.CHARACTERS:
            if (stack.size() == 0)
                break;
            String data = event.asCharacters().getData();
            if (log.isDebugEnabled())
                log.debug("unmarshaling characters: " + String.valueOf(data));
            if (data == null) {
                break; // ignore null
            }
            if (data.contains(">")) {
                //Note: we even get escaped '>' char here so 
                // can't accurately determine well-formedness 
                Location loc = event.getLocation();
                String msg = "line:col[" + loc.getLineNumber() + ":" + loc.getColumnNumber() + "]";
                msg += " - document may not be well-formed";
                log.warn(msg);
            }
            StreamNode streamNode = stack.peek();
            if (streamNode instanceof StreamProperty) {
                this.charbuf.append(data);
            } else {
                if (log.isDebugEnabled()) {
                    StreamObject streamObj = (StreamObject) streamNode;
                    Location loc = event.getLocation();
                    String msg = "line:col[" + loc.getLineNumber() + ":" + loc.getColumnNumber() + "]";
                    msg += " - ignoring character(s) data '" + data + "' for complex type "
                            + streamObj.getType().getURI() + "#" + streamObj.getType().getName();
                    log.debug(msg);
                }
            }
            break;
        default:
            logEventInfo(event);
        }
    }
    return root;
}

From source file:org.plasma.sdo.xml.StreamUnmarshaller.java

private void readCharacters(StreamProperty streamProperty, String data, XMLEvent event)
        throws UnmarshallerException {
    PlasmaType type = streamProperty.getType();
    PlasmaProperty property = streamProperty.getProperty();
    if (!property.getType().isDataType()) {
        Location loc = event.getLocation();
        String msg = "line:col[" + loc.getLineNumber() + ":" + loc.getColumnNumber() + "]";
        msg += " - cannot set reference propery (" + type.getURI() + "#" + type.getName() + "."
                + property.getName() + ") - from character data";
        throw new UnmarshallerException(msg);
    }/*from  w  w w.  j a v  a2 s .c o  m*/
    if (!property.isReadOnly()) {
        if (!property.isMany()) {
            Object value = PlasmaDataHelper.INSTANCE.convert(property, data);
            streamProperty.set(value);
        } else {
            Object value = PlasmaDataHelper.INSTANCE.convert(property, data);
            streamProperty.set(value);
        }
    } else {
        Location loc = event.getLocation();
        String msg = "line:col[" + loc.getLineNumber() + ":" + loc.getColumnNumber() + "]";
        msg += " - cannot set readonly propery (" + property.getName() + ") - ignoring character data '" + data
                + "' for readonly propery, " + type.getURI() + "#" + type.getName() + "." + property.getName();
        log.warn(msg);
    }
}

From source file:org.plasma.sdo.xml.StreamUnmarshaller.java

@SuppressWarnings("unchecked")
private void readAttributes(XMLEvent event, StreamObject prototype) throws UnmarshallerException {
    boolean serialIdAttributeFound = false;
    PlasmaType type = prototype.getType();
    Iterator<Attribute> iter = event.asStartElement().getAttributes();
    while (iter.hasNext()) {
        Attribute attrib = (Attribute) iter.next();
        QName attribName = attrib.getName();
        String localPart = attribName.getLocalPart();
        if (XMLConstants.ATTRIB_TARGET_NAMESPACE.equals(localPart)) {
            continue;
        } else if (XMLConstants.XMLSCHEMA_INSTANCE_NAMESPACE_URI.equals(attribName.getNamespaceURI())) {
            // its an XSI attribute we care about
            if ("type".equals(localPart)) {
                String xsiTypeLocalName = attrib.getValue();
                int delim = xsiTypeLocalName.indexOf(":");
                if (delim >= 0)
                    xsiTypeLocalName = xsiTypeLocalName.substring(delim + 1);
                // In order to validate XML graphs with both containment
                // and non containment references with an XML schema,
                // the Schema types must be generated with XSI 'xsi:type'
                // attribute to indicate a subclass. The subclass name will be
                // either 1.) matching the local for the type, wherein we
                // assume a containment reference or 2.) matching a generated
                // synthetic name for the non-containment reference for the
                // type, e.g. 'MyTypeRef'. So for containment references
                // we should find 'xsi:type="prefix:MyType" and for 
                // non-containment references we should find 'xsi:type="prefix:MyTypeRef"
                // or some other generated suffix with no name collisions. 
                if (!xsiTypeLocalName.equals(type.getLocalName())) {
                    if (log.isDebugEnabled())
                        log.debug("type as non-containment reference, " + type.getURI() + "#" + type.getName());
                    prototype.setNonContainmentReference(true);
                }/*from   w  ww .  ja  va 2  s.c  om*/
            }
            continue;
        }

        PlasmaProperty property = findPropertyByLocalName(type, localPart);
        if (property == null) {
            if (!SchemaUtil.getSerializationAttributeName().equals(localPart)) {
                Location loc = event.getLocation();
                String msg = "line:col[" + loc.getLineNumber() + ":" + loc.getColumnNumber() + "]";
                msg += " - no property '" + localPart + "' found defined for type " + type.getURI() + "#"
                        + type.getName();
                throw new UnmarshallerException(msg);
            } else {
                prototype.setSerialId(attrib.getValue());
                serialIdAttributeFound = true;
                continue;
            }
        }

        if (property.getType().isDataType()) {
            if (property.isMany()) {
                Location loc = event.getLocation();
                String msg = "line:col[" + loc.getLineNumber() + ":" + loc.getColumnNumber() + "]";
                msg += " - unexpected 'many' propery (" + type.getURI() + "#" + type.getName() + "."
                        + property.getName() + ") can only set singular properties from attribute '" + localPart
                        + "'";
                throw new UnmarshallerException(msg);
            }
            Object value = PlasmaDataHelper.INSTANCE.convert(property, attrib.getValue());
            if (!property.isReadOnly()) {
                prototype.set(property, value);
            } else {
                DataFlavor dataFlavor = property.getDataFlavor();
                switch (dataFlavor) {
                case integral:
                    Location loc = event.getLocation();
                    String msg = "line:col[" + loc.getLineNumber() + ":" + loc.getColumnNumber() + "]";
                    msg += " - cannot set integral readonly propery (" + property.getName()
                            + ") - ignoring attribute data '" + attrib.getValue() + "' for readonly propery, "
                            + type.getURI() + "#" + type.getName() + "." + property.getName();
                    log.warn(msg);
                    break;
                default:
                    prototype.set(property, value);
                }
            }
        } else {
            Location loc = event.getLocation();
            String msg = "line:col[" + loc.getLineNumber() + ":" + loc.getColumnNumber() + "]";
            msg += " - expected datatype property - attribute '" + localPart
                    + "' is a reference property as defined in type " + type.getURI() + "#" + type.getName();
            throw new UnmarshallerException(msg);
        }
    }

    if (!serialIdAttributeFound) {
        Location loc = event.getLocation();
        String msg = "line:col[" + loc.getLineNumber() + ":" + loc.getColumnNumber() + "]";
        msg += " - expected serialization attribute '" + SchemaUtil.getSerializationAttributeName()
                + "' for type " + type.getURI() + "#" + type.getName();
        throw new UnmarshallerException(msg);
    }
}

From source file:org.plasma.sdo.xml.StreamUnmarshaller.java

private PlasmaProperty getPropertyByLocalName(XMLEvent event, PlasmaType type, String localName)
        throws UnmarshallerException {
    PlasmaProperty property = findPropertyByLocalName(type, localName);
    if (property == null) {
        Location loc = event.getLocation();
        String msg = "line:col[" + loc.getLineNumber() + ":" + loc.getColumnNumber() + "]";
        msg += " - no property with local name (" + localName + ") found defined for type " + type.getURI()
                + "#" + type.getName();
        ;/* w w w  .  j  a va2 s.  c  om*/
        throw new UnmarshallerException(msg);
    }
    return property;
}