Java Code Examples for org.apache.xmlbeans.XmlCursor#insertChars()

The following examples show how to use org.apache.xmlbeans.XmlCursor#insertChars() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Iso19139GmdEncoder.java    From arctic-sea with Apache License 2.0 6 votes vote down vote up
private void encodeCiCitation(CICitationPropertyType cicpt, GmdCitation citation) {
    CICitationType cict = cicpt.addNewCICitation();
    cict.addNewTitle().setCharacterString(citation.getTitle());
    CIDateType cidt = cict.addNewDate().addNewCIDate();
    CodeListValueType clvt = cidt.addNewDateType().addNewCIDateTypeCode();
    GmdCitationDate gmdCitationDate = citation.getDate();
    GmdDateType gmdDateType = gmdCitationDate.getDateType();
    clvt.setCodeList(gmdDateType.getCodeList());
    clvt.setCodeListValue(gmdDateType.getCodeListValue());
    if (gmdDateType.getCodeSpace() != null && !gmdDateType.getCodeSpace().isEmpty()) {
        clvt.setCodeSpace(gmdDateType.getCodeSpace());
    }
    clvt.setStringValue(gmdDateType.getValue());
    XmlCursor newCursor = cidt.addNewDate().newCursor();
    newCursor.toNextToken();
    newCursor.beginElement(QN_GCO_DATE);
    newCursor.insertChars(gmdCitationDate.getDate());
    newCursor.dispose();
}
 
Example 2
Source File: Iso19139GmdEncoder.java    From arctic-sea with Apache License 2.0 6 votes vote down vote up
private void encodeGmdQuantitativeResult(DQResultPropertyType xbResult,
        GmdQuantitativeResult gmdQuantitativeResult) {
    DQQuantitativeResultType dqQuantitativeResultType = (DQQuantitativeResultType) xbResult
            .addNewAbstractDQResult().substitute(QN_GMD_QUANTITATIVE_RESULT, DQQuantitativeResultType.type);
    GmlBaseUnit unit = gmdQuantitativeResult.getUnit();
    UnitOfMeasurePropertyType valueUnit = dqQuantitativeResultType.addNewValueUnit();
    BaseUnitType xbBaseUnit =
            (BaseUnitType) valueUnit.addNewUnitDefinition().substitute(QN_GML_BASE_UNIT, BaseUnitType.type);
    CodeType xbCatalogSymbol = xbBaseUnit.addNewCatalogSymbol();
    xbCatalogSymbol.setCodeSpace(unit.getCatalogSymbol().getCodeSpace().toString());
    xbCatalogSymbol.setStringValue(unit.getCatalogSymbol().getValue());
    xbBaseUnit.setId(unit.getId());
    xbBaseUnit.addNewUnitsSystem().setHref(unit.getUnitSystem());
    xbBaseUnit.addNewIdentifier().setCodeSpace(unit.getIdentifier());
    if (gmdQuantitativeResult.isSetValueNilReason()) {
        dqQuantitativeResultType.addNewValue().setNilReason(gmdQuantitativeResult.getValueNilReason().name());
    } else {
        XmlCursor cursor = dqQuantitativeResultType.addNewValue().addNewRecord().newCursor();
        cursor.toNextToken();
        cursor.insertChars(gmdQuantitativeResult.getValue());
        cursor.dispose();
    }
}
 
Example 3
Source File: XML.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 *
 * @param qname
 * @param value
 * @return
 */
static XML createTextElement(XMLLibImpl lib, javax.xml.namespace.QName qname, String value)
{
    XScriptAnnotation anno;

    XmlObject xo = XmlObject.Factory.newInstance();
    XmlCursor cursor = xo.newCursor();
    try {
        cursor.toNextToken();

        cursor.beginElement(qname.getLocalPart(), qname.getNamespaceURI());
        //if(namespace.length() > 0)
        //    cursor.insertNamespace("", namespace);
        cursor.insertChars(value);

        cursor.toStartDoc();
        cursor.toNextToken();
        anno = new XScriptAnnotation(cursor);
        cursor.setBookmark(anno);
    } finally {
        cursor.dispose();
    }

    return new XML(lib, anno);
}
 
Example 4
Source File: XMLLibImpl.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Escapes the reserved characters in a value of a text node
 *
 * @param value Unescaped text
 * @return The escaped text
 */
public String escapeTextValue(Object value)
{
    if (value instanceof XMLObjectImpl) {
        return ((XMLObjectImpl)value).toXMLString(0);
    }

    String text = ScriptRuntime.toString(value);

    if (text.length() == 0) return text;

    XmlObject xo = XmlObject.Factory.newInstance();

    XmlCursor cursor = xo.newCursor();
    cursor.toNextToken();
    cursor.beginElement("a");
    cursor.insertChars(text);
    cursor.dispose();

    String elementText = xo.toString();
    int begin = elementText.indexOf('>') + 1;
    int end = elementText.lastIndexOf('<');
    return (begin < end) ? elementText.substring(begin, end) : "";
}
 
Example 5
Source File: XML.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 *
 * @param curs
 * @param xmlToInsert
 */
private void insertChild(XmlCursor curs, Object xmlToInsert)
{
    if (xmlToInsert == null || xmlToInsert instanceof Undefined)
    {
        // Do nothing
    }
    else if (xmlToInsert instanceof XmlCursor)
    {
        moveSrcToDest((XmlCursor)xmlToInsert, curs, true);
    }
    else if (xmlToInsert instanceof XML)
    {
        XML xmlValue = (XML) xmlToInsert;

        // If it's an attribute, then change to text node
        if (xmlValue.tokenType() == XmlCursor.TokenType.ATTR)
        {
            insertChild(curs, xmlValue.toString());
        }
        else
        {
            XmlCursor cursToInsert = ((XML) xmlToInsert).newCursor();

            moveSrcToDest(cursToInsert, curs, true);

            cursToInsert.dispose();
        }
    }
    else if (xmlToInsert instanceof XMLList)
    {
        XMLList list = (XMLList) xmlToInsert;

        for (int i = 0; i < list.length(); i++)
        {
            insertChild(curs, list.item(i));
        }
    }
    else
    {
        // Convert to string and make XML out of it
        String  xmlStr = ScriptRuntime.toString(xmlToInsert);
        XmlObject xo = XmlObject.Factory.newInstance();         // Create an empty document.

        XmlCursor sourceCurs = xo.newCursor();
        sourceCurs.toNextToken();

        // To hold the text.
        sourceCurs.insertChars(xmlStr);

        sourceCurs.toPrevToken();

        // Call us again with the cursor.
        moveSrcToDest(sourceCurs, curs, true);
    }
}
 
Example 6
Source File: XML.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 *
 */
void normalize()
{
    XmlCursor curs = newCursor();
    TokenType tt = curs.currentTokenType();

    // Walk through the tokens removing empty text nodes and merging adjacent text nodes.
    if (tt.isStartdoc())
    {
        tt = curs.toFirstContentToken();
    }

    if (tt.isContainer())
    {
        int nestLevel = 1;
        String previousText = null;

        while (nestLevel > 0)
        {
            tt = curs.toNextToken();

            if (tt == XmlCursor.TokenType.TEXT)
            {
                String currentText = curs.getChars().trim();

                if (currentText.trim().length() == 0)
                {
                    // Empty text node, remove.
                    removeToken(curs);
                    curs.toPrevToken();
                }
                else if (previousText == null)
                {
                    // No previous text node, reset to trimmed version
                    previousText = currentText;
                }
                else
                {
                    // It appears that this case never happens with XBeans.
                    // Previous text node exists, concatenate
                    String newText = previousText + currentText;

                    curs.toPrevToken();
                    removeToken(curs);
                    removeToken(curs);
                    curs.insertChars(newText);
                }
            }
            else
            {
                previousText = null;
            }

            if (tt.isStart())
            {
                nestLevel++;
            }
            else if (tt.isEnd())
            {
                nestLevel--;
            }
            else if (tt.isEnddoc())
            {
                // Shouldn't get here, but just in case.
                break;
            }
        }
    }


    curs.dispose();
}