Java Code Examples for org.xmlpull.v1.XmlSerializer#attribute()

The following examples show how to use org.xmlpull.v1.XmlSerializer#attribute() . 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: PlotContour.java    From microMathematics with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean onStartWriteXmlTag(XmlSerializer serializer, String key) throws Exception
{
    super.onStartWriteXmlTag(serializer, key);
    if (getBaseType().toString().equalsIgnoreCase(serializer.getName()))
    {
        serializer.attribute(FormulaList.XML_NS, XML_PROP_PLOT_STYLE,
                twoDPlotStyle.toString().toLowerCase(Locale.ENGLISH));
        functionView.getPlotParameters().writeToXml(serializer);
        functionView.getAxisParameters().writeToXml(serializer);
        functionView.getColorMapView().getColorMapParameters().writeToXml(serializer);
    }
    if (FormulaList.XML_TERM_TAG.equalsIgnoreCase(serializer.getName()) && key != null)
    {
        // contour-specific attributes
    }
    return false;
}
 
Example 2
Source File: XmlUtils.java    From JobSchedulerCompat with MIT License 6 votes vote down vote up
private static void writeMapXml(Map val, String name, XmlSerializer out)
        throws XmlPullParserException, IOException {
    if (val == null) {
        out.startTag(null, "null");
        out.endTag(null, "null");
        return;
    }

    out.startTag(null, "map");
    if (name != null) {
        out.attribute(null, "name", name);
    }

    writeMapXml(val, out);

    out.endTag(null, "map");
}
 
Example 3
Source File: XmlUtils.java    From WeexOne with MIT License 6 votes vote down vote up
/**
 * Flatten a Map into an XmlSerializer.  The map can later be read back
 * with readThisMapXml().
 *
 * @param val The map to be flattened.
 * @param name Name attribute to include with this list's tag, or null for
 *             none.
 * @param out XmlSerializer to write the map into.
 * @param callback Method to call when an Object type is not recognized.
 *
 * @see #writeMapXml(Map, OutputStream)
 * @see #writeListXml
 * @see #writeValueXml
 * @see #readMapXml
 *
 * @hide
 */
public static final void writeMapXml(Map val, String name, XmlSerializer out,
                                     WriteMapCallback callback) throws XmlPullParserException, java.io.IOException {

    if (val == null) {
        out.startTag(null, "null");
        out.endTag(null, "null");
        return;
    }

    out.startTag(null, "map");
    if (name != null) {
        out.attribute(null, "name", name);
    }

    writeMapXml(val, out, callback);

    out.endTag(null, "map");
}
 
Example 4
Source File: XmlUtils.java    From a with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Flatten a Map into an XmlSerializer.  The map can later be read back
 * with readThisMapXml().
 *
 * @param val      The map to be flattened.
 * @param name     Name attribute to include with this list's tag, or null for
 *                 none.
 * @param out      XmlSerializer to write the map into.
 * @param callback Method to call when an Object type is not recognized.
 * @hide
 * @see #writeMapXml(Map, OutputStream)
 * @see #writeListXml
 * @see #writeValueXml
 * @see #readMapXml
 */
public static void writeMapXml(Map val, String name, XmlSerializer out,
                               WriteMapCallback callback) throws XmlPullParserException, java.io.IOException {

    if (val == null) {
        out.startTag(null, "null");
        out.endTag(null, "null");
        return;
    }

    out.startTag(null, "map");
    if (name != null) {
        out.attribute(null, "name", name);
    }

    writeMapXml(val, out, callback);

    out.endTag(null, "map");
}
 
Example 5
Source File: ResAttr.java    From ratel with Apache License 2.0 6 votes vote down vote up
@Override
public void serializeToResValuesXml(XmlSerializer serializer,
                                    ResResource res) throws IOException, AndrolibException {
    String type = getTypeAsString();

    serializer.startTag(null, "attr");
    serializer.attribute(null, "name", res.getResSpec().getName());
    if (type != null) {
        serializer.attribute(null, "format", type);
    }
    if (mMin != null) {
        serializer.attribute(null, "min", mMin.toString());
    }
    if (mMax != null) {
        serializer.attribute(null, "max", mMax.toString());
    }
    if (mL10n != null && mL10n) {
        serializer.attribute(null, "localization", "suggested");
    }
    serializeBody(serializer, res);
    serializer.endTag(null, "attr");
}
 
Example 6
Source File: UserRestrictionsUtils.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
public static void writeRestrictions(@NonNull XmlSerializer serializer,
        @Nullable Bundle restrictions, @NonNull String tag) throws IOException {
    if (restrictions == null) {
        return;
    }

    serializer.startTag(null, tag);
    for (String key : restrictions.keySet()) {
        if (NON_PERSIST_USER_RESTRICTIONS.contains(key)) {
            continue; // Don't persist.
        }
        if (USER_RESTRICTIONS.contains(key)) {
            if (restrictions.getBoolean(key)) {
                serializer.attribute(null, key, "true");
            }
            continue;
        }
        Log.w(TAG, "Unknown user restriction detected: " + key);
    }
    serializer.endTag(null, tag);
}
 
Example 7
Source File: SettingsState.java    From Study_Android_Demo with Apache License 2.0 6 votes vote down vote up
static void setValueAttribute(String attr, String attrBase64, int version,
        XmlSerializer serializer, String value) throws IOException {
    if (version >= SETTINGS_VERSION_NEW_ENCODING) {
        if (value == null) {
            // Null value -> No ATTR_VALUE nor ATTR_VALUE_BASE64.
        } else if (isBinary(value)) {
            serializer.attribute(null, attrBase64, base64Encode(value));
        } else {
            serializer.attribute(null, attr, value);
        }
    } else {
        // Old encoding.
        if (value == null) {
            serializer.attribute(null, attr, NULL_VALUE_OLD_STYLE);
        } else {
            serializer.attribute(null, attr, value);
        }
    }
}
 
Example 8
Source File: ImportFromSMathStudio.java    From microMathematics with GNU General Public License v3.0 6 votes vote down vote up
private void parseEquation(final Element input, boolean inRightOfPrevious, final XmlSerializer serializer) throws Exception
{
    final List<Element> elements = XmlUtils.getElements(input, SM_TAG_MATH_EXPRESSION);
    final Element last = XmlUtils.removeLast(elements);
    if (last == null || last.getTextContent() == null)
    {
        return;
    }
    ExpressionProperties p = new ExpressionProperties(last);
    if (!p.isEqual(SM_TAG_MATH_OPERATOR, 2, ":"))
    {
        return;
    }
    final String term = FormulaBase.BaseType.EQUATION.toString().toLowerCase(Locale.ENGLISH);
    serializer.startTag(FormulaList.XML_NS, term);
    serializer.attribute(FormulaList.XML_NS, FormulaList.XML_PROP_INRIGHTOFPREVIOUS,
            Boolean.toString(inRightOfPrevious));
    parseTerm("rightTerm", elements, serializer, false);
    parseTerm("leftTerm", elements, serializer, true);
    serializer.endTag(FormulaList.XML_NS, term);
}
 
Example 9
Source File: XmlUtils.java    From cwac-netsecurity with Apache License 2.0 5 votes vote down vote up
/**
 * Flatten an int[] into an XmlSerializer.  The list can later be read back
 * with readThisIntArrayXml().
 *
 * @param val The int array to be flattened.
 * @param name Name attribute to include with this array's tag, or null for
 *             none.
 * @param out XmlSerializer to write the array into.
 *
 * @see #writeMapXml
 * @see #writeValueXml
 * @see #readThisIntArrayXml
 */
public static final void writeIntArrayXml(int[] val, String name,
        XmlSerializer out)
        throws XmlPullParserException, IOException {

    if (val == null) {
        out.startTag(null, "null");
        out.endTag(null, "null");
        return;
    }

    out.startTag(null, "int-array");
    if (name != null) {
        out.attribute(null, "name", name);
    }

    final int N = val.length;
    out.attribute(null, "num", Integer.toString(N));

    for (int i=0; i<N; i++) {
        out.startTag(null, "item");
        out.attribute(null, "value", Integer.toString(val[i]));
        out.endTag(null, "item");
    }

    out.endTag(null, "int-array");
}
 
Example 10
Source File: XmlUtils.java    From TowerCollector with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Flatten a byte[] into an XmlSerializer.  The list can later be read back
 * with readThisByteArrayXml().
 *
 * @param val The byte array to be flattened.
 * @param name Name attribute to include with this array's tag, or null for
 *             none.
 * @param out XmlSerializer to write the array into.
 *
 * @see #writeMapXml
 * @see #writeValueXml
 */
public static final void writeByteArrayXml(byte[] val, String name,
                                           XmlSerializer out)
        throws XmlPullParserException, java.io.IOException {

    if (val == null) {
        out.startTag(null, "null");
        out.endTag(null, "null");
        return;
    }

    out.startTag(null, "byte-array");
    if (name != null) {
        out.attribute(null, "name", name);
    }

    final int N = val.length;
    out.attribute(null, "num", Integer.toString(N));

    StringBuilder sb = new StringBuilder(val.length*2);
    for (int i=0; i<N; i++) {
        int b = val[i];
        int h = b>>4;
        sb.append(h >= 10 ? ('a'+h-10) : ('0'+h));
        h = b&0xff;
        sb.append(h >= 10 ? ('a'+h-10) : ('0'+h));
    }

    out.text(sb.toString());

    out.endTag(null, "byte-array");
}
 
Example 11
Source File: XmlUtils.java    From Android-PreferencesManager with Apache License 2.0 5 votes vote down vote up
/**
 * Flatten a byte[] into an XmlSerializer. The list can later be read back
 * with readThisByteArrayXml().
 *
 * @param val  The byte array to be flattened.
 * @param name Name attribute to include with this array's tag, or null for
 *             none.
 * @param out  XmlSerializer to write the array into.
 * @see #writeMapXml
 * @see #writeValueXml
 */
public static final void writeByteArrayXml(byte[] val, String name, XmlSerializer out) throws XmlPullParserException, java.io.IOException {

    if (val == null) {
        out.startTag(null, "null");
        out.endTag(null, "null");
        return;
    }

    out.startTag(null, "byte-array");
    if (name != null) {
        out.attribute(null, "name", name);
    }

    final int N = val.length;
    out.attribute(null, "num", Integer.toString(N));

    StringBuilder sb = new StringBuilder(val.length * 2);
    for (int i = 0; i < N; i++) {
        int b = val[i];
        int h = b >> 4;
        sb.append(h >= 10 ? ('a' + h - 10) : ('0' + h));
        h = b & 0xff;
        sb.append(h >= 10 ? ('a' + h - 10) : ('0' + h));
    }

    out.text(sb.toString());

    out.endTag(null, "byte-array");
}
 
Example 12
Source File: XmlUtils.java    From android-job with Apache License 2.0 4 votes vote down vote up
public static void writeByteArrayAttribute(XmlSerializer out, String name, byte[] value)
        throws IOException {
    if (value != null) {
        out.attribute(null, name, Base64.encodeToString(value, Base64.DEFAULT));
    }
}
 
Example 13
Source File: XML_Util.java    From AndroidApp with Mozilla Public License 2.0 4 votes vote down vote up
public static String createChangesetXmlBody(Map<String, String> tags) throws Exception {
    XmlSerializer xmlSerializer = Xml.newSerializer();
    StringWriter writer = new StringWriter();

    if (tags == null) return null;
    /**
     *
     <osm>
     <changeset>
     <tag k="created_by" v="JOSM 1.61"/>
     <tag k="comment" v="Just adding some streetnames"/>
     ...
     </changeset>
     ...
     </osm>
     */
    xmlSerializer.setOutput(writer);
    // start DOCUMENT
    xmlSerializer.startDocument("UTF-8", true);
    // open tag: <osm>
    xmlSerializer.startTag("", "osm");
    // open tag: <changeset>
    xmlSerializer.startTag("", "changeset");

    //create tags
    for (Map.Entry<String, String> tag : tags.entrySet()) {
        xmlSerializer.startTag("", "tag");
        xmlSerializer.attribute("", "k", tag.getKey());
        xmlSerializer.attribute("", "v", tag.getValue());
        xmlSerializer.endTag("", "tag");
    }

    // close tag: </changeset>
    xmlSerializer.endTag("", "changeset");
    // close tag: </osm>
    xmlSerializer.endTag("", "osm");
    // end DOCUMENT
    xmlSerializer.endDocument();

    return writer.toString();
}
 
Example 14
Source File: XmlUtils.java    From MyBookshelf with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Flatten an object's value into an XmlSerializer.  The value can later
 * be read back with readThisValueXml().
 * <p>
 * Currently supported value types are: null, String, Integer, Long,
 * Float, Double Boolean, Map, List.
 *
 * @param v        The object to be flattened.
 * @param name     Name attribute to include with this value's tag, or null
 *                 for none.
 * @param out      XmlSerializer to write the object into.
 * @param callback Handler for Object types not recognized.
 * @see #writeMapXml
 * @see #writeListXml
 * @see #readValueXml
 */
private static void writeValueXml(Object v, String name, XmlSerializer out,
                                  WriteMapCallback callback) throws XmlPullParserException, java.io.IOException {
    String typeStr;
    if (v == null) {
        out.startTag(null, "null");
        if (name != null) {
            out.attribute(null, "name", name);
        }
        out.endTag(null, "null");
        return;
    } else if (v instanceof String) {
        out.startTag(null, "string");
        if (name != null) {
            out.attribute(null, "name", name);
        }
        out.text(v.toString());
        out.endTag(null, "string");
        return;
    } else if (v instanceof Integer) {
        typeStr = "int";
    } else if (v instanceof Long) {
        typeStr = "long";
    } else if (v instanceof Float) {
        typeStr = "float";
    } else if (v instanceof Double) {
        typeStr = "double";
    } else if (v instanceof Boolean) {
        typeStr = "boolean";
    } else if (v instanceof byte[]) {
        writeByteArrayXml((byte[]) v, name, out);
        return;
    } else if (v instanceof int[]) {
        writeIntArrayXml((int[]) v, name, out);
        return;
    } else if (v instanceof long[]) {
        writeLongArrayXml((long[]) v, name, out);
        return;
    } else if (v instanceof double[]) {
        writeDoubleArrayXml((double[]) v, name, out);
        return;
    } else if (v instanceof String[]) {
        writeStringArrayXml((String[]) v, name, out);
        return;
    } else if (v instanceof Map) {
        writeMapXml((Map) v, name, out);
        return;
    } else if (v instanceof List) {
        writeListXml((List) v, name, out);
        return;
    } else if (v instanceof Set) {
        writeSetXml((Set) v, name, out);
        return;
    } else if (v instanceof CharSequence) {
        // XXX This is to allow us to at least write something if
        // we encounter styled text...  but it means we will drop all
        // of the styling information. :(
        out.startTag(null, "string");
        if (name != null) {
            out.attribute(null, "name", name);
        }
        out.text(v.toString());
        out.endTag(null, "string");
        return;
    } else if (callback != null) {
        callback.writeUnknownObject(v, name, out);
        return;
    } else {
        throw new RuntimeException("writeValueXml: unable to write value " + v);
    }

    out.startTag(null, typeStr);
    if (name != null) {
        out.attribute(null, "name", name);
    }
    out.attribute(null, "value", v.toString());
    out.endTag(null, typeStr);
}
 
Example 15
Source File: XmlUtils.java    From XPrivacy with GNU General Public License v3.0 4 votes vote down vote up
public static void writeBooleanAttribute(XmlSerializer out, String name, boolean value)
        throws IOException {
    out.attribute(null, name, Boolean.toString(value));
}
 
Example 16
Source File: GpxSerializer.java    From trekarta with GNU General Public License v3.0 4 votes vote down vote up
private static int serializeTrack(XmlSerializer serializer, Track track, ProgressListener progressListener, int progress) throws IllegalArgumentException, IllegalStateException, IOException {
    serializer.startTag(GpxFile.NS, GpxFile.TAG_TRK);
    serializer.startTag(GpxFile.NS, GpxFile.TAG_NAME);
    serializer.text(track.name);
    serializer.endTag(GpxFile.NS, GpxFile.TAG_NAME);
    if (track.description != null) {
        serializer.startTag(GpxFile.NS, GpxFile.TAG_DESC);
        serializer.cdsect(track.description);
        serializer.endTag(GpxFile.NS, GpxFile.TAG_DESC);
    }
    serializer.startTag(GpxFile.NS, GpxFile.TAG_TRKSEG);

    boolean first = true;
    for (Track.TrackPoint tp : track.points) {
        if (!tp.continuous && !first) {
            serializer.endTag(GpxFile.NS, GpxFile.TAG_TRKSEG);
            serializer.startTag(GpxFile.NS, GpxFile.TAG_TRKSEG);
        }
        serializer.startTag(GpxFile.NS, GpxFile.TAG_TRKPT);
        serializer.attribute("", GpxFile.ATTRIBUTE_LAT, String.valueOf(tp.latitudeE6 / 1E6));
        serializer.attribute("", GpxFile.ATTRIBUTE_LON, String.valueOf(tp.longitudeE6 / 1E6));
        if (!Float.isNaN(tp.elevation)) {
            serializer.startTag(GpxFile.NS, GpxFile.TAG_ELE);
            serializer.text(String.valueOf(tp.elevation));
            serializer.endTag(GpxFile.NS, GpxFile.TAG_ELE);
        }
        if (tp.time > 0L) {
            serializer.startTag(GpxFile.NS, GpxFile.TAG_TIME);
            serializer.text(GpxFile.formatTime(new Date(tp.time)));
            serializer.endTag(GpxFile.NS, GpxFile.TAG_TIME);
        }
        serializer.endTag(GpxFile.NS, GpxFile.TAG_TRKPT);
        first = false;
        progress++;
        if (progressListener != null)
            progressListener.onProgressChanged(progress);
    }
    serializer.endTag(GpxFile.NS, GpxFile.TAG_TRKSEG);
    serializer.endTag(GpxFile.NS, GpxFile.TAG_TRK);

    return progress;
}
 
Example 17
Source File: XmlUtils.java    From a with GNU General Public License v3.0 4 votes vote down vote up
public static void writeLongAttribute(XmlSerializer out, String name, long value)
        throws IOException {
    out.attribute(null, name, Long.toString(value));
}
 
Example 18
Source File: AccountAuthenticatorCache.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
@Override
public void writeAsXml(AuthenticatorDescription item, XmlSerializer out)
        throws IOException {
    out.attribute(null, "type", item.type);
}
 
Example 19
Source File: XmlUtils.java    From JobSchedulerCompat with Apache License 2.0 4 votes vote down vote up
public static void writeStringAttribute(XmlSerializer out, String name, String value)
        throws IOException {
    if (value != null) {
        out.attribute(null, name, value);
    }
}
 
Example 20
Source File: XmlUtils.java    From MyBookshelf with GNU General Public License v3.0 4 votes vote down vote up
public static void writeBooleanAttribute(XmlSerializer out, String name, boolean value)
        throws IOException {
    out.attribute(null, name, Boolean.toString(value));
}