Package javax.xml.stream

Examples of javax.xml.stream.XMLStreamException


    }
   
    public String nextText() throws XMLStreamException
    {
        if(getEventType() != XMLStreamConstants.START_ELEMENT) {
            throw new XMLStreamException(
                "parser must be on START_ELEMENT to read next text",
                getLocation());
        }
        int eventType = next();
        if(eventType == XMLStreamConstants.CHARACTERS) {
            String result = getText();
            eventType = next();
            if(eventType != XMLStreamConstants.END_ELEMENT) {
                throw new XMLStreamException(
                    "TEXT must be immediately followed by END_ELEMENT and not "
                        +ElementTypeNames.getEventTypeString(getEventType()),
                    getLocation());
            }
            return result;
        } else if(eventType == XMLStreamConstants.END_ELEMENT) {
            return "";
        } else {
            throw new XMLStreamException(
                "parser must be on START_ELEMENT or TEXT to read text",
                getLocation());
        }
    }
View Full Code Here


               || (eventType == XMLStreamConstants.CHARACTERS && isWhiteSpace())
               || (eventType == XMLStreamConstants.CDATA && isWhiteSpace())) {
            next();
        }
        if (eventType != XMLStreamConstants.START_ELEMENT && eventType != XMLStreamConstants.END_ELEMENT) {
            throw new XMLStreamException("expected XMLStreamConstants.START_ELEMENT or XMLStreamConstants.END_ELEMENT not "
                                         +ElementTypeNames.getEventTypeString(getEventType()),
                                         getLocation());
        }
        return eventType;
    }
View Full Code Here

    public String getElementText()
        throws XMLStreamException
    {
        StringBuffer buf = new StringBuffer();
        if(getEventType() != START_ELEMENT)
            throw new XMLStreamException(
                "Precondition for readText is getEventType() == START_ELEMENT");
        do {
            if(next() == END_DOCUMENT)
                throw new XMLStreamException("Unexpected end of Document");
            if(isStartElement())
                throw new XMLStreamException("Unexpected Element start");
            if(isCharacters() || getEventType() == XMLStreamConstants.ENTITY_REFERENCE)
                buf.append(getText());
        } while(!isEndElement());
        return buf.toString();
    }
View Full Code Here

                                    if( !usePC && hadCharData ) needsMerging = true;
                                }
                                //                if(tokenize) return eventType = XMLStreamConstants.CDATA;
                                if(reportCdataEvent) return eventType = XMLStreamConstants.CDATA;
                            } else {
                                throw new XMLStreamException(
                                    "unexpected character in markup "+printable(ch),
                                    getLocation());
                            }
                           
                        } else if(ch == '?') {
                            parsePI();
                            if(tokenize) return eventType = XMLStreamConstants.PROCESSING_INSTRUCTION;
                            if( !usePC && hadCharData ) needsMerging = true;
                        } else if( isNameStartChar(ch) ) {
                            if(!tokenize && hadCharData) {
                                seenStartTag = true;
                                //posEnd = pos - 2;
                                return eventType = XMLStreamConstants.CHARACTERS;
                            }
                            return eventType = parseStartTag();
                        } else {
                            throw new XMLStreamException(
                                "unexpected character in markup "+printable(ch),
                                getLocation());
                        }
                        // do content comapctation if it makes sense!!!!
                       
                    } else if(ch == '&') {
                        // work on ENTITY
                        //posEnd = pos - 1;
                       
                        if(tokenize && hadCharData) {
                            seenAmpersand = true;
                            return eventType = XMLStreamConstants.CHARACTERS;
                        }
                       
                        int oldStart = posStart;
                        int oldEnd = posEnd;
                        char[] resolvedEntity = parseEntityRef();
                        if (!getConfigurationContext().isReplacingEntities())
                            return eventType = XMLStreamConstants.ENTITY_REFERENCE;
                        eventType = XMLStreamConstants.CHARACTERS;
                        // check if replacement text can be resolved !!!
                        if(resolvedEntity == null) {
                            if(entityRefName == null) {
                                entityRefName = newString(buf, posStart, posEnd - posStart);
                            }
                            throw new XMLStreamException(
                                "could not resolve entity named '"+printable(entityRefName)+"'",
                                getLocation());
                        }
                        //int entStart = posStart;
                        //int entEnd = posEnd;
                        posStart = oldStart;
                        posEnd = oldEnd;
                        if(!usePC) {
                            if(hadCharData) {
                                joinPC(); // posEnd is already set correctly!!!
                                needsMerging = false;
                            } else {
                                usePC = true;
                                pcStart = pcEnd = 0;
                            }
                        }
                        //assert usePC == true;
                        // write into PC replacement text - do merge for replacement text!!!!
                        for (int i = 0; i < resolvedEntity.length; i++)
                        {
                            if(pcEnd >= pc.length) ensurePC(pcEnd);
                            pc[pcEnd++] = resolvedEntity[ i ];
                           
                        }
                        // fix for disappearing char if last entiry ref
                        //see http://www.extreme.indiana.edu/bugzilla/show_bug.cgi?id=192
                        hadCharData = true;
                       
                        //assert needsMerging == false;
                    } else {
                       
                        if(needsMerging) {
                            //assert usePC == false;
                            joinPC()// posEnd is already set correctly!!!
                            //posStart = pos  -  1;
                            needsMerging = false;
                        }
                        //no MARKUP not ENTITIES so work on character data ...
                       
                        // [14] CharData ::=   [^<&]* - ([^<&]* ']]>' [^<&]*)
                       
                        hadCharData = true;
                       
                        boolean normalizedCR = false;
                        // use loop locality here!!!!
                        do {
                            //if(tokenize == false) {
                            if (true) {
                                // deal with normalization issues ...
                                if(ch == '\r') {
                                    normalizedCR = true;
                                    posEnd = pos -1;
                                    // posEnd is alreadys set
                                    if(!usePC) {
                                        if(posEnd > posStart) {
                                            joinPC();
                                        } else {
                                            usePC = true;
                                            pcStart = pcEnd = 0;
                                        }
                                    }
                                    //assert usePC == true;
                                    if(pcEnd >= pc.length) ensurePC(pcEnd);
                                    pc[pcEnd++] = '\n';
                                } else if(ch == '\n') {
                                    //   if(!usePC) {  joinPC(); } else { if(pcEnd >= pc.length) ensurePC(); }
                                    if(!normalizedCR && usePC) {
                                        if(pcEnd >= pc.length) ensurePC(pcEnd);
                                        pc[pcEnd++] = '\n';
                                    }
                                    normalizedCR = false;
                                } else {
                                    if(usePC) {
                                        if(pcEnd >= pc.length) ensurePC(pcEnd);
                                        pc[pcEnd++] = ch;
                                    }
                                    normalizedCR = false;
                                }
                            }
                            ch = more();
                        } while(ch != '<' && ch != '&');
                        posEnd = pos - 1;
                        continue MAIN_LOOP;  // skip ch = more() from below - we are alreayd ahead ...
                    }
                    ch = more();
                } // endless while(true)
            } else {
                if(seenRoot) {
                    return parseEpilog();
                } else {
                    return parseProlog();
                }
            }
           
        } catch(EOFException eofe) {
            throw new XMLStreamException(EOF_MSG, getLocation(), eofe);
        }
    }
View Full Code Here

            if(eventType == XMLStreamConstants.START_DOCUMENT) {
                // bootstrap parsing with getting first character input!
                // deal with BOM
                // detect BOM and frop it (Unicode int Order Mark)
                if(ch == '\uFFFE') {
                    throw new XMLStreamException(
                        "first character in input was UNICODE noncharacter (0xFFFE)"+
                            "- input requires int swapping",
                        getLocation());
                }
                if(ch == CHAR_UTF8_BOM) {
                    // skipping UNICODE int Order Mark (so called BOM)
                    ch = more();
                }
            }
            seenMarkup = false;
            boolean gotS = false;
            posStart = pos - 1;
            while(true) {
                // deal with Misc
                // [27] Misc ::= XMLStreamConstants.COMMENT | PI | S
                // deal with docdecl --> mark it!
                // else parseStartTag seen <[^/]
                if(ch == '<') {
                    if(gotS && tokenize) {
                        posEnd = pos - 1;
                        seenMarkup = true;
                        return eventType = XMLStreamConstants.SPACE;
                    }
                    ch = more();
                    if(ch == '?') {
                        // check if it is 'xml'
                        // deal with XMLDecl
                        boolean isXMLDecl = parsePI();
                        if(tokenize) {
                            if (isXMLDecl)
                                return (eventType = XMLStreamConstants.START_DOCUMENT);
                            return (eventType = XMLStreamConstants.PROCESSING_INSTRUCTION);
                        }
                    } else if(ch == '!') {
                        ch = more();
                        if(ch == 'D') {
                            if(seenDocdecl) {
                                throw new XMLStreamException(
                                    "only one docdecl allowed in XML document",
                                    getLocation());
                            }
                            seenDocdecl = true;
                            parseDocdecl();
                            if(tokenize) return eventType = XMLStreamConstants.DTD;
                        } else if(ch == '-') {
                            parseComment();
                            if(tokenize) return eventType = XMLStreamConstants.COMMENT;
                        } else {
                            throw new XMLStreamException(
                                "unexpected markup <!"+printable(ch),
                                getLocation());
                        }
                    } else if(ch == '/') {
                        throw new XMLStreamException(
                            "expected start tag name and not "+printable(ch),
                            getLocation());
                    } else if(isNameStartChar(ch)) {
                        seenRoot = true;
                        return parseStartTag();
                    } else {
                        throw new XMLStreamException(
                            "expected start tag name and not "+printable(ch),
                            getLocation());
                    }
                } else if(isS(ch)) {
                    gotS = true;
                } else {
                    throw new XMLStreamException(
                        "only whitespace content allowed before start tag and not "+printable(ch),
                        getLocation());
                }
                ch = more();
            }
           
        } catch(EOFException eofe) {
            throw new XMLStreamException(EOF_MSG, getLocation(), eofe);
        }
    }
View Full Code Here

   
    protected int parseEpilog()
        throws XMLStreamException
    {
        if(eventType == XMLStreamConstants.END_DOCUMENT) {
            throw new XMLStreamException(
                "already reached end document",
                getLocation());
        }
        if(reachedEnd) {
            return eventType = XMLStreamConstants.END_DOCUMENT;
        }
        boolean gotS = false;
        try {
            // epilog: Misc*
            char ch;
            if(seenMarkup) {
                ch = buf[ pos - 1 ];
            } else {
                ch = more();
            }
            seenMarkup = false;
            posStart = pos - 1;
            while(true) {
                // deal with Misc
                // [27] Misc ::= XMLStreamConstants.COMMENT | PI | S
                if(ch == '<') {
                    if(gotS && tokenize) {
                        posEnd = pos - 1;
                        seenMarkup = true;
                        return eventType = XMLStreamConstants.SPACE;
                    }
                    ch = more();
                    if(ch == '?') {
                        // check if it is 'xml'
                        // deal with XMLDecl
                        parsePI();
                        if(tokenize) return eventType = XMLStreamConstants.PROCESSING_INSTRUCTION;
                       
                    } else if(ch == '!') {
                        ch = more();
                        if(ch == 'D') {
                            parseDocdecl(); //FIXME
                            if(tokenize) return eventType = XMLStreamConstants.DTD;
                        } else if(ch == '-') {
                            parseComment();
                            if(tokenize) return eventType = XMLStreamConstants.COMMENT;
                        } else {
                            throw new XMLStreamException(
                                "unexpected markup <!"+printable(ch),
                                getLocation());
                        }
                    } else if(ch == '/') {
                        throw new XMLStreamException(
                            "end tag not allowed in epilog but got "+printable(ch),
                            getLocation());
                    } else if(isNameStartChar(ch)) {
                        throw new XMLStreamException(
                            "start tag not allowed in epilog but got "+printable(ch),
                            getLocation());
                    } else {
                        throw new XMLStreamException(
                            "in epilog expected ignorable content and not "+printable(ch),
                            getLocation());
                    }
                } else if(isS(ch)) {
                    gotS = true;
                } else {
                    throw new XMLStreamException(
                        "in epilog non whitespace content is not allowed but got "+printable(ch),
                        getLocation());
                }
                ch = more();
            }
View Full Code Here

        eventType = XMLStreamConstants.END_ELEMENT;
       
        try {
            char ch = more();
            if(!isNameStartChar(ch)) {
                throw new XMLStreamException(
                    "expected name start and not "+printable(ch),
                    getLocation());
            }
            posStart = pos - 3;
            int nameStart = pos - 1 + bufAbsoluteStart;
            do {
                ch = more();
            } while(isNameChar(ch));
           
            // now we go one level down -- do checks
            //--depth;  //FIXME
           
            // check that end tag name is the same as start tag
            //String name = new String(buf, nameStart - bufAbsoluteStart,
            //                           (pos - 1) - (nameStart - bufAbsoluteStart));
            int last = pos - 1;
            int off = nameStart - bufAbsoluteStart;
            int len = last - off;
            char[] cbuf = elRawName[depth];
            if(elRawNameEnd[depth] != len) {
                // construct strings for exception
                String startname = new String(cbuf, 0, elRawNameEnd[depth]);
                String endname = new String(buf, off, len);
                throw new XMLStreamException(
                    "end tag name '"+endname+"' must match start tag name '"+startname+"'",
                    getLocation());
            }
            for (int i = 0; i < len; i++)
            {
                if(buf[off++] != cbuf[i]) {
                    // construct strings for exception
                    String startname = new String(cbuf, 0, len);
                    String endname = new String(buf, off - i - 1, len);
                    throw new XMLStreamException(
                        "end tag name '"+endname+"' must be the same as start tag '"+startname+"'",
                        getLocation());
                }
            }
           
            while(isS(ch)) { ch = more(); } // skip additional white spaces
            if(ch != '>') throw new XMLStreamException(
                    "expected > to finsh end tag not "+printable(ch),
                    getLocation());
           
           
           
            //namespaceEnd = elNamespaceCount[ depth ]; //FIXME
           
            posEnd = pos;
            pastEndTag = true;
           
        } catch(EOFException eofe) {
            throw new XMLStreamException(EOF_MSG, getLocation(), eofe);
        }
       
        return XMLStreamConstants.END_ELEMENT;
    }
View Full Code Here

            localNamespaceEnd = 0;
            // retrieve name
            int nameStart = pos - 1 + bufAbsoluteStart;
            int colonPos = -1;
            char ch = buf[ pos - 1];
            if(ch == ':' && processNamespaces) throw new XMLStreamException(
                    "when namespaces processing enabled colon can not be at element name start",
                    getLocation());
            while(true) {
                ch = more();
                if(!isNameChar(ch)) break;
                if(ch == ':' && processNamespaces) {
                    if(colonPos != -1) throw new XMLStreamException(
                            "only one colon is allowed in name of element when namespaces are enabled",
                            getLocation());
                    colonPos = pos - 1 + bufAbsoluteStart;
                }
            }
           
            // retrieve name
            ensureElementsCapacity();
           
           
            //TODO check for efficient interning and then use elRawNameInterned!!!!
           
            int elLen = (pos - 1) - (nameStart - bufAbsoluteStart);
            if(elRawName[ depth ] == null || elRawName[ depth ].length < elLen) {
                elRawName[ depth ] = new char[ 2 * elLen ];
            }
            System.arraycopy(buf, nameStart - bufAbsoluteStart, elRawName[ depth ], 0, elLen);
            elRawNameEnd[ depth ] = elLen;
           
            String name = null;
           
            // work on prefixes and namespace URI
            String prefix = null;
            if(processNamespaces) {
                if(colonPos != -1) {
                    prefix = elPrefix[ depth ] = newString(buf, nameStart - bufAbsoluteStart,
                                                           colonPos - nameStart);
                    name = elName[ depth ] = newString(buf, colonPos + 1 - bufAbsoluteStart,
                                                       //(pos -1) - (colonPos + 1));
                                                       pos - 2 - (colonPos - bufAbsoluteStart));
                } else {
                    prefix = elPrefix[ depth ] = null;
                    name = elName[ depth ] = newString(buf, nameStart - bufAbsoluteStart, elLen);
                }
            } else {
                name = elName[ depth ] = newString(buf, nameStart - bufAbsoluteStart, elLen);
            }
           
            while(true) {
                while(isS(ch)) { ch = more(); } // skip additional white spaces
                if(ch == '>') {
                    break;
                } else if(ch == '/') {
                    if(emptyElementTag) throw new XMLStreamException(
                            "repeated / in tag declaration",
                            getLocation());
                    emptyElementTag = true;
                    ch = more();
                    if(ch != '>') throw new XMLStreamException(
                            "expected > to end empty tag not "+printable(ch), getLocation());
                    break;
                } else if(isNameStartChar(ch)) {
                    ch = parseAttribute();
                    ch = more();
                    continue;
                } else {
                    throw new XMLStreamException(
                        "start tag unexpected character "+printable(ch),
                        getLocation());
                }
                //ch = more(); // skip space
            }
           
            // now when namespaces were declared we can resolve them
            if(processNamespaces) {
                String uri = getNamespaceURI(prefix);
                if(uri == null) {
                    if(prefix == null) { // no prefix and no uri => use default namespace
                        uri = NO_NAMESPACE;
                    } else {
                        throw new XMLStreamException(
                            "could not determine namespace bound to element prefix "+prefix,
                            getLocation());
                    }
                   
                }
                elUri[ depth ] = uri;
               
               
                //String uri = getNamespaceURI(prefix);
                //if(uri == null && prefix == null) { // no prefix and no uri => use default namespace
                //  uri = "";
                //}
                // resolve attribute namespaces
                for (int i = 0; i < attributeCount; i++)
                {
                    String attrPrefix = attributePrefix[ i ];
                    if(attrPrefix != null) {
                        String attrUri = getNamespaceURI(attrPrefix);
                        if(attrUri == null) {
                            throw new XMLStreamException(
                                "could not determine namespace bound to attribute prefix "+attrPrefix,
                                getLocation());
                           
                        }
                        attributeUri[ i ] = attrUri;
                    } else {
                        attributeUri[ i ] = NO_NAMESPACE;
                    }
                }
               
                //TODO
                //[ WFC: Unique Att Spec ]
                // check namespaced attribute uniqueness contraint!!!
               
                for (int i = 1; i < attributeCount; i++)
                {
                    for (int j = 0; j < i; j++)
                    {
                        if( attributeUri[j] == attributeUri[i]
                               && (allStringsInterned && attributeName[j].equals(attributeName[i])
                                       || (!allStringsInterned
                                               && attributeNameHash[ j ] == attributeNameHash[ i ]
                                               && attributeName[j].equals(attributeName[i])) )
                              
                          ) {
                            // prepare data for nice error messgae?
                            String attr1 = attributeName[j];
                            if(attributeUri[j] != null) attr1 = attributeUri[j]+":"+attr1;
                            String attr2 = attributeName[i];
                            if(attributeUri[i] != null) attr2 = attributeUri[i]+":"+attr2;
                            throw new XMLStreamException(
                                "duplicated attributes "+attr1+" and "+attr2,
                                getLocation());
                        }
                    }
                }
               
               
            } else { // ! processNamespaces
               
                //[ WFC: Unique Att Spec ]
                // check raw attribute uniqueness contraint!!!
                for (int i = 1; i < attributeCount; i++)
                {
                    for (int j = 0; j < i; j++)
                    {
                        if((allStringsInterned && attributeName[j].equals(attributeName[i])
                                || (!allStringsInterned
                                        && attributeNameHash[ j ] == attributeNameHash[ i ]
                                        && attributeName[j].equals(attributeName[i])) )
                              
                          ) {
                            // prepare data for nice error messgae?
                            String attr1 = attributeName[j];
                            String attr2 = attributeName[i];
                            throw new XMLStreamException(
                                "duplicated attributes "+attr1+" and "+attr2,
                                getLocation());
                        }
                    }
                }
            }
           
            elNamespaceCount[ depth ] = namespaceEnd;
            posEnd = pos;
           
            if (defaultAttributes != null)
                if (prefix != null)
                    addDefaultAttributes(prefix+":"+name);
                else
                    addDefaultAttributes(name);
        } catch (EOFException eofe) {
            throw new XMLStreamException(EOF_MSG, getLocation(), eofe);
        }
       
        return XMLStreamConstants.START_ELEMENT;
    }
View Full Code Here

            // [WFC: No < in Attribute Values]
            int prevPosStart = posStart + bufAbsoluteStart;
            int nameStart = pos - 1 + bufAbsoluteStart;
            int colonPos = -1;
            char ch = buf[ pos - 1 ];
            if(ch == ':' && processNamespaces) throw new XMLStreamException(
                    "when namespaces processing enabled colon can not be at attribute name start",
                    getLocation());
           
           
            boolean startsWithXmlns = processNamespaces && ch == 'x';
            int xmlnsPos = 0;
           
            ch = more();
            while(isNameChar(ch)) {
                if(processNamespaces) {
                    if(startsWithXmlns && xmlnsPos < 5) {
                        ++xmlnsPos;
                        if(xmlnsPos == 1) { if(ch != 'm') startsWithXmlns = false; }
                        else if(xmlnsPos == 2) { if(ch != 'l') startsWithXmlns = false; }
                        else if(xmlnsPos == 3) { if(ch != 'n') startsWithXmlns = false; }
                        else if(xmlnsPos == 4) { if(ch != 's') startsWithXmlns = false; }
                        else if(xmlnsPos == 5) {
                            if(ch != ':') throw new XMLStreamException(
                                    "after xmlns in attribute name must be colon"
                                        +"when namespaces are enabled",
                                    getLocation());
                            //colonPos = pos - 1 + bufAbsoluteStart;
                        }
                    }
                    if(ch == ':') {
                        if(colonPos != -1) throw new XMLStreamException(
                                "only one colon is allowed in attribute name"
                                    +" when namespaces are enabled",
                                getLocation());
                        colonPos = pos - 1 + bufAbsoluteStart;
                    }
                }
                ch = more();
            }
           
            ensureAttributesCapacity(attributeCount);
           
            // --- start processing attributes
            String name = null;
            String prefix = null;
            // work on prefixes and namespace URI
            if(processNamespaces) {
                if(xmlnsPos < 4) startsWithXmlns = false;
                if(startsWithXmlns) {
                    if(colonPos != -1) {
                        //prefix = attributePrefix[ attributeCount ] = null;
                        name = //attributeName[ attributeCount ] =
                            newString(buf, colonPos - bufAbsoluteStart + 1,
                                      //pos - 1 - (colonPos + 1 - bufAbsoluteStart)
                                      pos - 2 - (colonPos - bufAbsoluteStart)
                                      );
                        /* 07-Mar-2006, TSa: Illegal to try to (re)declare
                         *   prefix "xmlns" (even to its correct URI)...
                         *   (similar check for "xml" has to wait until we
                         *   see the URI -- that can be re-declared to the
                         *   same URI)
                         */
                        if (name.equals("xmlns")) {
                            throw new XMLStreamException("trying to bind reserved NS prefix 'xmlns'",
                                                         getLocation());
                        }
                    }
                } else {
                    if(colonPos != -1) {
                        prefix = attributePrefix[ attributeCount ] =
                            newString(buf, nameStart - bufAbsoluteStart,
                                      //colonPos - (nameStart - bufAbsoluteStart));
                                      colonPos - nameStart);
                        name = attributeName[ attributeCount ] =
                            newString(buf, colonPos - bufAbsoluteStart + 1,
                                      //pos - 1 - (colonPos + 1 - bufAbsoluteStart));
                                      pos - 2 - (colonPos - bufAbsoluteStart));
                        //name.substring(0, colonPos-nameStart);
                    } else {
                        prefix = attributePrefix[ attributeCount = null;
                        name = attributeName[ attributeCount ] =
                            newString(buf, nameStart - bufAbsoluteStart,
                                      pos - 1 - (nameStart - bufAbsoluteStart));
                    }
                    if(!allStringsInterned) {
                        attributeNameHash[ attributeCount ] = name.hashCode();
                    }
                }
               
            } else {
                // retrieve name
                name = attributeName[ attributeCount ] =
                    newString(buf, nameStart - bufAbsoluteStart,
                              pos - 1 - (nameStart - bufAbsoluteStart));
                ////assert name != null;
                if(!allStringsInterned) {
                    attributeNameHash[ attributeCount ] = name.hashCode();
                }
            }
           
            // [25] Eq ::=  S? '=' S?
            while(isS(ch)) { ch = more(); } // skip additional spaces
            if(ch != '=') throw new XMLStreamException(
                    "expected = after attribute name",
                    getLocation());
            ch = more();
            while(isS(ch)) { ch = more(); } // skip additional spaces
           
            // [10] AttValue ::=   '"' ([^<&"] | Reference)* '"'
            //                  |  "'" ([^<&'] | Reference)* "'"
            char delimit = ch;
            if(delimit != '"' && delimit != '\'') throw new XMLStreamException(
                    "attribute value must start with quotation or apostrophe not "
                        +printable(delimit),
                    getLocation());
            // parse until delimit or < and resolve Reference
            //[67] Reference ::= EntityRef | CharRef
            //int valueStart = pos + bufAbsoluteStart;
           
           
            boolean normalizedCR = false;
            usePC = false;
            pcStart = pcEnd;
            posStart = pos;
           
            while(true) {
                ch = more();
                if(ch == delimit) {
                    break;
                } if(ch == '<') {
                    throw new XMLStreamException(
                        "markup not allowed inside attribute value - illegal < ", getLocation());
                } if(ch == '&') {
                    // extractEntityRef
                    posEnd = pos - 1;
                    if(!usePC) {
                        boolean hadCharData = posEnd > posStart;
                        if(hadCharData) {
                            // posEnd is already set correctly!!!
                            joinPC();
                        } else {
                            usePC = true;
                            pcStart = pcEnd = 0;
                        }
                    }
                    //assert usePC == true;
                   
                    char[] resolvedEntity = parseEntityRef();
                    // check if replacement text can be resolved !!!
                    if(resolvedEntity == null) {
                        if(entityRefName == null) {
                            entityRefName = newString(buf, posStart, posEnd - posStart);
                        }
                        throw new XMLStreamException(
                            "could not resolve entity named '"+printable(entityRefName)+"'",
                            getLocation());
                    }
                    // write into PC replacement text - do merge for replacement text!!!!
                    for (int i = 0; i < resolvedEntity.length; i++)
                    {
                        if(pcEnd >= pc.length) ensurePC(pcEnd);
                        pc[pcEnd++] = resolvedEntity[ i ];
                       
                    }
                } else if(ch == '\t' || ch == '\n' || ch == '\r') {
                    // do attribute value normalization
                    // as described in http://www.w3.org/TR/REC-xml#AVNormalize
                    // TODO add test for it form spec ...
                    // handle EOL normalization ...
                    if(!usePC) {
                        posEnd = pos - 1;
                        if(posEnd > posStart) {
                            joinPC();
                        } else {
                            usePC = true;
                            pcEnd = pcStart = 0;
                        }
                    }
                    //assert usePC == true;
                    if(pcEnd >= pc.length) ensurePC(pcEnd);
                    if(ch != '\n' || !normalizedCR) {
                        pc[pcEnd++] = ' '; //'\n';
                    }
                   
                } else {
                    if(usePC) {
                        if(pcEnd >= pc.length) ensurePC(pcEnd);
                        pc[pcEnd++] = ch;
                    }
                }
                normalizedCR = ch == '\r';
            }
           
           
            if(processNamespaces && startsWithXmlns) {
               
                String ns = null;
                if(!usePC) {
                    ns = newStringIntern(buf, posStart, pos - 1 - posStart);
                } else {
                    ns = newStringIntern(pc, pcStart, pcEnd - pcStart);
                }
                ensureNamespacesCapacity(namespaceEnd);
                int prefixHash = -1;

                /* 07-Mar-2006, TSa: It is illegal (as per XML Namespaces
                 *    specs) to bind anything to 'xmlns' URI; and anything
                 *    other than 'xml' to 'xml' URI...
                 */
                if (ns.equals(XMLConstants.XML_NS_URI)) {
                    if (!"xml".equals(name)) {
                        throw new XMLStreamException("trying to bind reserved NS URI  '"+XMLConstants.XML_NS_URI+"' to prefix other than 'xml'");
                    }
                } else if (ns.equals(XMLConstants.XMLNS_ATTRIBUTE_NS_URI)) {
                    // Can bind nothing to this URI:
                    throw new XMLStreamException("trying to bind reserved NS URI  '"+XMLConstants.XMLNS_ATTRIBUTE_NS_URI+"'");
                }

                if(colonPos != -1) {
                    if(ns.length() == 0) {
                        throw new XMLStreamException(
                            "non-default namespace can not be declared to be empty string (in xml 1.0)",
                            getLocation());
                    }

                    /* 07-Mar-2006, TSa: Can only declare 'xml' to bind to
                     *   its standard URI -- otherwise that's an error.
                     */
                    if (name.equals("xml")) {
                        if (!ns.equals(XMLConstants.XML_NS_URI)) {
                            throw new XMLStreamException("trying to bind reserved NS prefix 'xml' to URI other than its standard value ("+XMLConstants.XML_NS_URI+")",
                                                         getLocation());
                        }
                    }

                    // declare new namespace
                    namespacePrefix[ namespaceEnd ] = name;
                    if(!allStringsInterned) {
                        prefixHash = namespacePrefixHash[ namespaceEnd ] = name.hashCode();
                    }
                } else {
                    // declare  new default namespace ...
                    namespacePrefix[ namespaceEnd ] = null;
                    if(!allStringsInterned) {
                        prefixHash = namespacePrefixHash[ namespaceEnd ] = -1;
                    }
                }
                namespaceUri[ namespaceEnd ] = ns;
               
                // detect duplicate namespace declarations!!!
                int startNs = elNamespaceCount[ depth - 1 ];
                for (int i = namespaceEnd - 1; i >= startNs; --i)
                {
                    if(((allStringsInterned || name == null) && namespacePrefix[ i ] == name)
                           || (!allStringsInterned && name != null &&
                                   namespacePrefixHash[ i ] == prefixHash
                                   && name.equals(namespacePrefix[ i ])
                              ))
                    {
                        String s = name == null ? "default" : "'"+name+"'";
                        throw new XMLStreamException(
                            "duplicated namespace declaration for "+s+" prefix", getLocation());
                       
                    }
                }
                //            ++localNamespaceEnd;
                ++namespaceEnd;
               
            } else {
                if(!usePC) {
                    attributeValue[ attributeCount ] =
                        new String(buf, posStart, pos - 1 - posStart);
                } else {
                    attributeValue[ attributeCount ] =
                        new String(pc, pcStart, pcEnd - pcStart);
                }
                ++attributeCount;
            }
            posStart = prevPosStart - bufAbsoluteStart;
            return ch;
           
        } catch (EOFException eofe) {
            throw new XMLStreamException(EOF_MSG, getLocation(), eofe);
        }
    }
View Full Code Here

                        } else if(ch >= 'a' && ch <= 'f') {
                            charRef += (ch - ('a' - 10));
                        } else if(ch >= 'A' && ch <= 'F') {
                            charRef += (ch - ('A' - 10));
                        } else {
                            throw new XMLStreamException(
                                "character reference (with hex value) may not contain "
                                    +printable(ch), getLocation());
                        }
                    } while (charRef <= MAX_UNICODE_CHAR);
                } else {
                    // encoded in decimal
                    do {
                        if(ch >= '0' && ch <= '9') {
                            charRef = (charRef * 10) + (ch - '0');
                        } else if (ch == ';') {
                            break;
                        } else {
                            throw new XMLStreamException(
                                "character reference (with decimal value) may not contain "
                                    +printable(ch), getLocation());
                        }
                        ch = more();
                    } while (charRef <= MAX_UNICODE_CHAR);
                }
                posEnd = pos - 1;

                /* 07-Mar-2006, TSa: Character entities still have to expand
                 *    to valid XML characters...
                 */
                checkCharValidity(charRef, false);

                if (charRef > 0xFFFF) { // surrogate pair
                    if (charRefTwoCharBuf == null) {
                        charRefTwoCharBuf = new char[2];
                    }
                    charRef -= 0x10000;
                    charRefTwoCharBuf[0] = (char) ((charRef >> 10+ 0xD800);
                    charRefTwoCharBuf[1] = (char) ((charRef & 0x3FF+ 0xDC00);
                    return (entityValue = charRefTwoCharBuf);
                }
                // normal char:
                charRefOneCharBuf[0] = (char) charRef;
                return (entityValue = charRefOneCharBuf);
            } else {
                // name reference
               
                // scan until ;
                do{ ch = more(); } while(ch != ';');
                posEnd = pos - 1;
                // determine what name maps to
                int len = posEnd - posStart;
                if(len == 2 && buf[posStart] == 'l' && buf[posStart+1] == 't') {
                    if(!replace)
                        text = "<";
                    charRefOneCharBuf[0] = '<';
                   
                    return (entityValue = charRefOneCharBuf);
                    //if(paramPC || isParserTokenizing) {
                    //    if(pcEnd >= pc.length) ensurePC();
                    //   pc[pcEnd++] = '<';
                    //}
                } else if(len == 3 && buf[posStart] == 'a'
                              && buf[posStart+1] == 'm' && buf[posStart+2] == 'p') {
                    if(!replace)
                        text = "&";
                    charRefOneCharBuf[0] = '&';
                   
                    return (entityValue = charRefOneCharBuf);
                } else if(len == 2 && buf[posStart] == 'g' && buf[posStart+1] == 't') {
                    if(!replace)
                        text = ">";
                    charRefOneCharBuf[0] = '>';
                   
                    return (entityValue = charRefOneCharBuf);
                } else if(len == 4 && buf[posStart] == 'a' && buf[posStart+1] == 'p'
                              && buf[posStart+2] == 'o' && buf[posStart+3] == 's')
                {
                    if(!replace)
                        text = "'";
                    charRefOneCharBuf[0] = '\'';
                   
                    return (entityValue = charRefOneCharBuf);
                } else if(len == 4 && buf[posStart] == 'q' && buf[posStart+1] == 'u'
                              && buf[posStart+2] == 'o' && buf[posStart+3] == 't')
                {
                    if(!replace)
                        text = "\"";
                    charRefOneCharBuf[0] = '"';
                   
                    return (entityValue = charRefOneCharBuf);
                }

                // No, should be a general entity:
                return (entityValue = lookupEntityReplacement(len));
            }
           
        } catch (EOFException eofe) {
            throw new XMLStreamException(EOF_MSG, getLocation(), eofe);
        }
    }
View Full Code Here

TOP

Related Classes of javax.xml.stream.XMLStreamException

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.