Android Open Source - EBrowser Weave Util






From Project

Back to project page EBrowser.

License

The source code is released under:

GNU General Public License

If you think the Android project EBrowser listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright 2010 Patrick Woodworth// w  w w  . j a v a 2 s  .  co m
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.emergent.android.weave.client;

import org.apache.http.HttpEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.protocol.HTTP;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.Date;
import java.util.Locale;

/**
 * @author Patrick Woodworth
 */
public class WeaveUtil {

  private static final String JSON_STREAM_TYPE = "application/json";
  private static final String ENTITY_CHARSET_NAME = "UTF-8";

  private WeaveUtil() {
    // no instantiation
  }

  @SuppressWarnings({})
  public static void checkNull(URI uri) {
    if (uri == null) {
      Dbg.w(new IllegalArgumentException("checkNull(URI) had null arg"));
    } else if (uri.getHost() == null || uri.getHost().length() < 1) {
      Dbg.w(new IllegalArgumentException("checkNull(URI) had empty host"));
    }
  }

  @SuppressWarnings({})
  public static void checkNull(String str) {
    if (str == null || str.trim().length() < 1) {
      Dbg.w(new IllegalArgumentException("checkNull(String) had empty arg"));
    }
  }

  public static String toModifiedTimeString(Date modified) {
    long time = modified.getTime();
    double timed = time / 1000.0;
    String retval = String.format(Locale.ENGLISH, "%.2f", timed);      
//    Dbg.debug("TIME: " + retval);
    return retval;
  }

  public static Date toModifiedTimeDate(String modified) {
    @SuppressWarnings("unused")
  long now = System.currentTimeMillis();
    try {
      double modDouble = Double.parseDouble(modified) * 1000;
      long mod = Math.round(modDouble);
//      Dbg.printf("mod: %d ; cur : %d ; delta : %d\n", mod, now, now - mod);
      return new Date(mod);
    } catch (Exception e) {
      return new Date(); // todo buggy ?
    }
  }

  public static Date toModifiedTimeDate(double modDouble) {
    try {
      long mod = Math.round(modDouble * 1000);
      return new Date(mod);
    } catch (Exception e) {
      return null;
    }
  }

  public static UriBuilder buildUpon(URI serverUri) {
    return new UriBuilder(serverUri);
  }

  public static void dump(JSONObject jsonObject) {
    try {
      String out = jsonObject.toString(2);
      System.out.println(out);
    } catch (JSONException e) {
      e.printStackTrace();
    }
  }

  public static String encodeUriSegment(String segment) {
    try {
      return URLEncoder.encode(segment, "UTF-8");
    } catch (UnsupportedEncodingException e) {
      throw new IllegalStateException(e);
    }
  }

  public static String toString(URI uri) {
    checkNull(uri);
    String retval = uri == null ? null : uri.toString();
    checkNull(retval);
    return retval;
  }

  public static byte[] toAsciiBytes(String data) {
    try {
      return data == null ? null : data.getBytes("US-ASCII");
    } catch (UnsupportedEncodingException e) {
      throw new IllegalStateException(e);
    }
  }

  public static String toAsciiString(byte[] data) {
    try {
      return data == null ? null : new String(data, "US-ASCII");
    } catch (UnsupportedEncodingException e) {
      throw new IllegalStateException(e);
    }
  }

  public static byte[] toUtf8Bytes(String data) {
    try {
      return data == null ? null : data.getBytes("UTF-8");
    } catch (UnsupportedEncodingException e) {
      throw new IllegalStateException(e);
    }
  }

  public static String toUtf8String(byte[] data) {
    try {
      return data == null ? null : new String(data, "UTF-8");
    } catch (UnsupportedEncodingException e) {
      throw new IllegalStateException(e);
    }
  }

  public static void zeroize(char[] secret) {
    if (secret != null)
      Arrays.fill(secret, '\0');
  }

  @SuppressWarnings("unused")
  private static HttpEntity toHttpEntity(JSONArray jsonArray) throws JSONException {
    try {
      StringEntity entity = new StringEntity(jsonArray.toString(0), ENTITY_CHARSET_NAME);
      entity.setContentType(JSON_STREAM_TYPE + HTTP.CHARSET_PARAM + ENTITY_CHARSET_NAME);
      return entity;
    } catch (UnsupportedEncodingException e) {
      throw new IllegalStateException(e);
    }
  }

  @SuppressWarnings("unused")
  private static HttpEntity toHttpEntity(WeaveBasicObject wbo) throws JSONException {
    try {
      StringEntity entity = new StringEntity(wbo.toJSONObjectString(), ENTITY_CHARSET_NAME);
      entity.setContentType(JSON_STREAM_TYPE + HTTP.CHARSET_PARAM + ENTITY_CHARSET_NAME);
      return entity;
    } catch (UnsupportedEncodingException e) {
      throw new IllegalStateException(e);
    }
  }

  public static class UriBuilder {

    private String m_val;

    public UriBuilder(URI uri) {
      m_val = uri.toASCIIString();
    }

    public void appendEncodedPath(String s) {
      if (m_val.charAt(m_val.length() - 1) != '/')
        m_val += "/";
      m_val += s;
    }

    public URI build() {
      try {
        return URI.create(m_val);
      } catch (IllegalArgumentException e) {
        Dbg.w("BAD URI: %s", m_val);
        throw e;
      }
    }
  }
}




Java Source Code List

org.emergent.android.weave.client.Base32.java
org.emergent.android.weave.client.Base64Encoder.java
org.emergent.android.weave.client.Base64.java
org.emergent.android.weave.client.BulkKeyCouplet.java
org.emergent.android.weave.client.Dbg.java
org.emergent.android.weave.client.HexEncoder.java
org.emergent.android.weave.client.Hex.java
org.emergent.android.weave.client.QueryParams.java
org.emergent.android.weave.client.QueryResult.java
org.emergent.android.weave.client.UserWeave.java
org.emergent.android.weave.client.WeaveAccountInfo.java
org.emergent.android.weave.client.WeaveBasicObject.java
org.emergent.android.weave.client.WeaveConstants.java
org.emergent.android.weave.client.WeaveCryptoUtil.java
org.emergent.android.weave.client.WeaveException.java
org.emergent.android.weave.client.WeaveFactory.java
org.emergent.android.weave.client.WeaveHeader.java
org.emergent.android.weave.client.WeaveResponse.java
org.emergent.android.weave.client.WeaveSSLSocketFactory.java
org.emergent.android.weave.client.WeaveTransport.java
org.emergent.android.weave.client.WeaveUtil.java
org.greendroid.QuickActionGrid.java
org.greendroid.QuickActionWidget.java
org.greendroid.QuickAction.java
org.zirco.controllers.Controller.java
org.zirco.events.EventConstants.java
org.zirco.events.EventController.java
org.zirco.events.IDownloadEventsListener.java
org.zirco.model.DbAdapter.java
org.zirco.model.UrlSuggestionItemComparator.java
org.zirco.model.adapters.BookmarksCursorAdapter.java
org.zirco.model.adapters.DownloadListAdapter.java
org.zirco.model.adapters.HistoryExpandableListAdapter.java
org.zirco.model.adapters.UrlSuggestionCursorAdapter.java
org.zirco.model.adapters.WeaveBookmarksCursorAdapter.java
org.zirco.model.items.BookmarkItem.java
org.zirco.model.items.DownloadItem.java
org.zirco.model.items.HistoryItem.java
org.zirco.model.items.UrlSuggestionItem.java
org.zirco.model.items.WeaveBookmarkItem.java
org.zirco.providers.BookmarksProviderWrapper.java
org.zirco.providers.WeaveColumns.java
org.zirco.providers.WeaveContentProvider.java
org.zirco.providers.ZircoBookmarksContentProvider.java
org.zirco.sync.ISyncListener.java
org.zirco.sync.WeaveSyncTask.java
org.zirco.ui.activities.AboutActivity.java
org.zirco.ui.activities.AdBlockerWhiteListActivity.java
org.zirco.ui.activities.BookmarksHistoryActivity.java
org.zirco.ui.activities.BookmarksListActivity.java
org.zirco.ui.activities.ChangelogActivity.java
org.zirco.ui.activities.DownloadsListActivity.java
org.zirco.ui.activities.EditBookmarkActivity.java
org.zirco.ui.activities.HistoryListActivity.java
org.zirco.ui.activities.IToolbarsContainer.java
org.zirco.ui.activities.MainActivity.java
org.zirco.ui.activities.MobileViewListActivity.java
org.zirco.ui.activities.WeaveBookmarksListActivity.java
org.zirco.ui.activities.preferences.BaseSpinnerCustomPreferenceActivity.java
org.zirco.ui.activities.preferences.HomepagePreferenceActivity.java
org.zirco.ui.activities.preferences.PreferencesActivity.java
org.zirco.ui.activities.preferences.SearchUrlPreferenceActivity.java
org.zirco.ui.activities.preferences.UserAgentPreferenceActivity.java
org.zirco.ui.activities.preferences.WeavePreferencesActivity.java
org.zirco.ui.activities.preferences.WeaveServerPreferenceActivity.java
org.zirco.ui.components.CustomWebViewClient.java
org.zirco.ui.components.CustomWebView.java
org.zirco.ui.runnables.DownloadRunnable.java
org.zirco.ui.runnables.FaviconUpdaterRunnable.java
org.zirco.ui.runnables.HideToolbarsRunnable.java
org.zirco.ui.runnables.HistoryUpdater.java
org.zirco.ui.runnables.XmlHistoryBookmarksExporter.java
org.zirco.ui.runnables.XmlHistoryBookmarksImporter.java
org.zirco.utils.AnimationManager.java
org.zirco.utils.ApplicationUtils.java
org.zirco.utils.Constants.java
org.zirco.utils.DateUtils.java
org.zirco.utils.IOUtils.java
org.zirco.utils.ProxyChangeReceiver.java
org.zirco.utils.ProxySettings.java
org.zirco.utils.UrlUtils.java