text:
package com.fsck.zywxMailk9.mail.store;

import android.util.Log;
import android.widget.Toast;

import com.fsck.zywxMailk9.Account;
import com.fsck.zywxMailk9.K9;
import com.fsck.zywxMailk9.controller.MessageRetrievalListener;
import com.fsck.zywxMailk9.helper.Utility;
import com.fsck.zywxMailk9.mail.*;
import com.fsck.zywxMailk9.mail.Folder.OpenMode;
import com.fsck.zywxMailk9.mail.internet.MimeMessage;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLException;
import javax.net.ssl.TrustManager;
import java.io.*;
import java.net.*;
import java.security.GeneralSecurityException;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

public class Pop3Store extends Store {
    public static final String STORE_TYPE = "POP3";

    public static final int CONNECTION_SECURITY_NONE = 0;
    public static final int CONNECTION_SECURITY_TLS_OPTIONAL = 1;
    public static final int CONNECTION_SECURITY_TLS_REQUIRED = 2;
    public static final int CONNECTION_SECURITY_SSL_REQUIRED = 3;
    public static final int CONNECTION_SECURITY_SSL_OPTIONAL = 4;

    private enum AuthType {
        PLAIN,
        CRAM_MD5
    }

    private static final String STLS_COMMAND = "STLS";
    private static final String USER_COMMAND = "USER";
    private static final String PASS_COMMAND = "PASS";
    private static final String CAPA_COMMAND = "CAPA";
    private static final String STAT_COMMAND = "STAT";
    private static final String LIST_COMMAND = "LIST";
    private static final String UIDL_COMMAND = "UIDL";
    private static final String TOP_COMMAND = "TOP";
    private static final String RETR_COMMAND = "RETR";
    private static final String DELE_COMMAND = "DELE";
    private static final String QUIT_COMMAND = "QUIT";

    private static final String STLS_CAPABILITY = "STLS";
    private static final String UIDL_CAPABILITY = "UIDL";
    private static final String PIPELINING_CAPABILITY = "PIPELINING";
    private static final String USER_CAPABILITY = "USER";
    private static final String TOP_CAPABILITY = "TOP";

    private static final Flag[] PERMANENT_FLAGS = { Flag.DELETED };

    /**
     * Decodes a Pop3Store URI.
     *
     *
Possible forms:

     *

     * pop3://user:password@server:port CONNECTION_SECURITY_NONE
     * pop3+tls://user:password@server:port CONNECTION_SECURITY_TLS_OPTIONAL
     * pop3+tls+://user:password@server:port CONNECTION_SECURITY_TLS_REQUIRED
     * pop3+ssl+://user:password@server:port CONNECTION_SECURITY_SSL_REQUIRED
     * pop3+ssl://user:password@server:port CONNECTION_SECURITY_SSL_OPTIONAL
     *

     */
    public static ServerSettings decodeUri(String uri) {
        String host;
        int port;
        ConnectionSecurity connectionSecurity;
        String username = null;
        String password = null;

        URI pop3Uri;
        try {
            pop3Uri = new URI(uri);
        } catch (URISyntaxException use) {
            throw new IllegalArgumentException("Invalid Pop3Store URI", use);
        }

        String scheme = pop3Uri.getScheme();
        if (scheme.equals("pop3")) {
            connectionSecurity = ConnectionSecurity.NONE;
            port = 110;
        } else if (scheme.equals("pop3+tls")) {
            connectionSecurity = ConnectionSecurity.STARTTLS_OPTIONAL;
            port = 110;
        } else if (scheme.equals("pop3+tls+")) {
            connectionSecurity = ConnectionSecurity.STARTTLS_REQUIRED;
            port = 110;
        } else if (scheme.equals("pop3+ssl+")) {
            connectionSecurity = ConnectionSecurity.SSL_TLS_REQUIRED;
            port = 995;
        } else if (scheme.equals("pop3+ssl")) {
            connectionSecurity = ConnectionSecurity.SSL_TLS_OPTIONAL;
            port = 995;
        } else {
            throw new IllegalArgumentException("Unsupported protocol (" + scheme + ")");
        }

        host = pop3Uri.getHost();

        if (pop3Uri.getPort() != -1) {
            port = pop3Uri.getPort();
        }

        String authType = AuthType.PLAIN.name();
        if (pop3Uri.getUserInfo() != null) {
            try {
                int userIndex = 0, passwordIndex = 1;
                String userinfo = pop3Uri.getUserInfo();
                String[] userInfoParts = userinfo.split(":");
                if (userInfoParts.length > 2 || userinfo.endsWith(":") ) {
                    // If 'userinfo' ends with ":" the password is empty. This can only happen
                    // after an account was imported (so authType and username are present).
                    userIndex++;
                    passwordIndex++;
                    authType = userInfoParts[0];
                }
                username = URLDecoder.decode(userInfoParts[userIndex], "UTF-8");
                if (userInfoParts.length > passwordIndex) {
                    password = URLDecoder.decode(userInfoParts[passwordIndex], "UTF-8");
                }
            } catch (UnsupportedEncodingException enc) {
                // This shouldn't happen since the encoding is hardcoded to UTF-8
                throw new IllegalArgumentException("Couldn't urldecode username or password.", enc);
            }
        }

        return new ServerSettings(STORE_TYPE, host, port, connectionSecurity, authType, username,
                password);
    }

    /**
     * Creates a Pop3Store URI with the supplied settings.
     *
     * @param server
     *         The {@link ServerSettings} object that holds the server settings.
     *
     * @return A Pop3Store URI that holds the same information as the {@code server} parameter.
     *
     * @see Account#getStoreUri()
     * @see Pop3Store#decodeUri(String)
     */
    public static String createUri(ServerSettings server) {
        String userEnc;
        String passwordEnc;
        try {
            userEnc = URLEncoder.encode(server.username, "UTF-8");
            passwordEnc = (server.password != null) ?
                    URLEncoder.encode(server.password, "UTF-8") : "";
        }
        catch (UnsupportedEncodingException e) {
            throw new IllegalArgumentException("Could not encode username or password", e);
        }

        String scheme;
        switch (server.connectionSecurity) {
            case SSL_TLS_OPTIONAL:
                scheme = "pop3+ssl";
                break;
            case SSL_TLS_REQUIRED:
                scheme = "pop3+ssl+";
                break;
            case STARTTLS_OPTIONAL:
                scheme = "pop3+tls";
                break;
            case STARTTLS_REQUIRED:
                scheme = "pop3+tls+";
                break;
            default:
            case NONE:
                scheme = "pop3";
                break;
        }

        try {
            AuthType.valueOf(server.authenticationType);
        } catch (Exception e) {
            throw new IllegalArgumentException("Invalid authentication type (" +
                    server.authenticationType + ")");
        }

        String userInfo = server.authenticationType + ":" + userEnc + ":" + passwordEnc;
        try {
            return new URI(scheme, userInfo, server.host, server.port, null, null,
                    null).toString();
        } catch (URISyntaxException e) {
            throw new IllegalArgumentException("Can't create Pop3Store URI", e);
        }
    }

    private String mHost;
    private int mPort;
    private String mUsername;
    private String mPassword;
    private AuthType mAuthType;
    private int mConnectionSecurity;
    private HashMap<String, Folder> mFolders = new HashMap<String, Folder>();
    private Pop3Capabilities mCapabilities;

    /**
     * This value is {@code true} if the server supports the CAPA command but doesn't advertise
     * support for the TOP command OR if the server doesn't support the CAPA command and we
     * already unsuccessfully tried to use the TOP command.
     */
    private boolean mTopNotSupported;

    public Pop3Store(Account account) throws MessagingException {
        super(account);

        ServerSettings settings;
        try {
            settings = decodeUri(mAccount.getStoreUri());
        } catch (IllegalArgumentException e) {
            throw new MessagingException("Error while decoding store URI", e);
        }

        mHost = settings.host;
        mPort = settings.port;

        switch (settings.connectionSecurity) {
        case NONE:
            mConnectionSecurity = CONNECTION_SECURITY_NONE;
            break;
        case STARTTLS_OPTIONAL:
            mConnectionSecurity = CONNECTION_SECURITY_TLS_OPTIONAL;
            break;
        case STARTTLS_REQUIRED:
            mConnectionSecurity = CONNECTION_SECURITY_TLS_REQUIRED;
            break;
        case SSL_TLS_OPTIONAL:
            mConnectionSecurity = CONNECTION_SECURITY_SSL_OPTIONAL;
            break;
        case SSL_TLS_REQUIRED:
            mConnectionSecurity = CONNECTION_SECURITY_SSL_REQUIRED;
            break;
        }

        mUsername = settings.username;
        mPassword = settings.password;
        mAuthType = AuthType.valueOf(settings.authenticationType);
    }

    @Override
    public Folder getFolder(String name) {
        Folder folder = mFolders.get(name);
        if (folder == null) {
            folder = new Pop3Folder(name);
            mFolders.put(folder.getName(), folder);
        }
        return folder;
    }

    @Override
    public List <? extends Folder > getPersonalNamespaces(boolean forceListAll) throws MessagingException {
        List<Folder> folders = new LinkedList<Folder>();
        folders.add(getFolder(mAccount.getInboxFolderName()));
        return folders;
    }

    @Override
    public void checkSettings() throws MessagingException {
        Pop3Folder folder = new Pop3Folder(mAccount.getInboxFolderName());
        folder.open(OpenMode.READ_WRITE);
        if (!mCapabilities.uidl) {
            /*
             * Run an additional test to see if UIDL is supported on the server. If it's not we
             * can't service this account.
             */

            /*
             * If the server doesn't support UIDL it will return a - response, which causes
             * executeSimpleCommand to throw a MessagingException, exiting this method.
             */
            folder.executeSimpleCommand(UIDL_COMMAND);

        }
        folder.close();
    }

    class Pop3Folder extends Folder {
        private Socket mSocket;
        private InputStream mIn;
        private OutputStream mOut;
        private HashMap<String, Pop3Message> mUidToMsgMap = new HashMap<String, Pop3Message>();
        private HashMap<Integer, Pop3Message> mMsgNumToMsgMap = new HashMap<Integer, Pop3Message>();
        private HashMap<String, Integer> mUidToMsgNumMap = new HashMap<String, Integer>();
        private String mName;
        private int mMessageCount;

        public Pop3Folder(String name) {
            super(Pop3Store.this.mAccount);
            this.mName = name;

            if (mName.equalsIgnoreCase(mAccount.getInboxFolderName())) {
                mName = mAccount.getInboxFolderName();
            }
        }

        @Override
        public synchronized void open(OpenMode mode) throws MessagingException {
            if (isOpen()) {
                return;
            }

            if (!mName.equalsIgnoreCase(mAccount.getInboxFolderName())) {
                throw new MessagingException("Folder does not exist");
            }

            try {

                SocketAddress socketAddress = new InetSocketAddress(mHost, mPort);
                Log.e("===address=====", socketAddress.toString());
                if (mConnectionSecurity == CONNECTION_SECURITY_SSL_REQUIRED ||
                        mConnectionSecurity == CONNECTION_SECURITY_SSL_OPTIONAL) {
                    SSLContext sslContext = SSLContext.getInstance("TLS");
                    final boolean secure = mConnectionSecurity == CONNECTION_SECURITY_SSL_REQUIRED;
                    sslContext.init(null, new TrustManager[] {
                                        TrustManagerFactory.get(mHost, secure)
                                    }, new SecureRandom());
                    mSocket = sslContext.getSocketFactory().createSocket();
                } else {
                    mSocket = new Socket();
                }

                mSocket.connect(socketAddress, SOCKET_CONNECT_TIMEOUT);
//                mSocket.isConnected()
                Log.e("===connect=====", mSocket.isConnected()+"");
                mIn = new BufferedInputStream(mSocket.getInputStream(), 1024);
                mOut = new BufferedOutputStream(mSocket.getOutputStream(), 512);

                mSocket.setSoTimeout(Store.SOCKET_READ_TIMEOUT);
                if (!isOpen()) {
                    throw new MessagingException("Unable to connect socket");
                }

                // Eat the banner
                executeSimpleCommand(null);

                if (mConnectionSecurity == CONNECTION_SECURITY_TLS_OPTIONAL
                        || mConnectionSecurity == CONNECTION_SECURITY_TLS_REQUIRED) {
                    mCapabilities = getCapabilities();

                    if (mCapabilities.stls) {
                        writeLine(STLS_COMMAND);

                        SSLContext sslContext = SSLContext.getInstance("TLS");
                        boolean secure = mConnectionSecurity == CONNECTION_SECURITY_TLS_REQUIRED;
                        sslContext.init(null, new TrustManager[] {
                                            TrustManagerFactory.get(mHost, secure)
                                        }, new SecureRandom());
                        mSocket = sslContext.getSocketFactory().createSocket(mSocket, mHost, mPort,
                                  true);
                        mSocket.setSoTimeout(Store.SOCKET_READ_TIMEOUT);
                        mIn = new BufferedInputStream(mSocket.getInputStream(), 1024);
                        mOut = new BufferedOutputStream(mSocket.getOutputStream(), 512);
                        if (!isOpen()) {
                            throw new MessagingException("Unable to connect socket");
                        }
                    } else if (mConnectionSecurity == CONNECTION_SECURITY_TLS_REQUIRED) {
                        throw new MessagingException("TLS not supported but required");
                    }
                }

                if (mAuthType == AuthType.CRAM_MD5) {
                    try {
                        String b64Nonce = executeSimpleCommand("AUTH CRAM-MD5").replace("+ ", "");

                        String b64CRAM = Authentication.computeCramMd5(mUsername, mPassword, b64Nonce);
                        executeSimpleCommand(b64CRAM);

                    } catch (MessagingException me) {
                        throw new AuthenticationFailedException(null, me);
                    }
                } else {
                    try {
                        executeSimpleCommand(USER_COMMAND + " " + mUsername);
                        executeSimpleCommand(PASS_COMMAND + " " + mPassword, true);
                    } catch (MessagingException me) {
                        throw new AuthenticationFailedException(null, me);
                    }
                }

                mCapabilities = getCapabilities();
            } catch (SSLException e) {
                throw new CertificateValidationException(e.getMessage(), e);
            } catch (GeneralSecurityException gse) {
                throw new MessagingException(
                    "Unable to open connection to POP server due to security error.", gse);
            } catch (IOException ioe) {
                throw new MessagingException("Unable to open connection to POP server.", ioe);
            }

            String response = executeSimpleCommand(STAT_COMMAND);
            String[] parts = response.split(" ");
            mMessageCount = Integer.parseInt(parts[1]);

            mUidToMsgMap.clear();
            mMsgNumToMsgMap.clear();
            mUidToMsgNumMap.clear();
        }

        @Override
        public boolean isOpen() {
            return (mIn != null && mOut != null && mSocket != null
                    && mSocket.isConnected() && !mSocket.isClosed());
        }

        @Override
        public OpenMode getMode() {
            return OpenMode.READ_WRITE;
        }

        @Override
        public void close() {
            try {
                if (isOpen()) {
                    executeSimpleCommand(QUIT_COMMAND);
                }
            } catch (Exception e) {
                /*
                 * QUIT may fail if the connection is already closed. We don't care. It's just
                 * being friendly.
                 */
            }

            closeIO();
        }

        private void closeIO() {
            try {
                mIn.close();
            } catch (Exception e) {
                /*
                 * May fail if the connection is already closed.
                 */
            }
            try {
                mOut.close();
            } catch (Exception e) {
                /*
                 * May fail if the connection is already closed.
                 */
            }
            try {
                mSocket.close();
            } catch (Exception e) {
                /*
                 * May fail if the connection is already closed.
                 */
            }
            mIn = null;
            mOut = null;
            mSocket = null;
        }

        @Override
        public String getName() {
            return mName;
        }

        @Override
        public boolean create(FolderType type) throws MessagingException {
            return false;
        }

        @Override
        public boolean exists() throws MessagingException {
            return mName.equalsIgnoreCase(mAccount.getInboxFolderName());
        }

        @Override
        public int getMessageCount() {
            return mMessageCount;
        }

        @Override
        public int getUnreadMessageCount() throws MessagingException {
            return -1;
        }
        @Override
        public int getFlaggedMessageCount() throws MessagingException {
            return -1;
        }

        @Override
        public Message getMessage(String uid) throws MessagingException {
            Pop3Message message = mUidToMsgMap.get(uid);
            if (message == null) {
                message = new Pop3Message(uid, this);
            }
            return message;
        }

        @Override
        public Message[] getMessages(int start, int end, Date earliestDate, MessageRetrievalListener listener)
        throws MessagingException {
            if (start < 1 || end < 1 || end < start) {
                throw new MessagingException(String.format("Invalid message set %d %d",
                                             start, end));
            }
            try {
                indexMsgNums(start, end);
            } catch (IOException ioe) {
                throw new MessagingException("getMessages", ioe);
            }
            ArrayList<Message> messages = new ArrayList<Message>();
            int i = 0;
            for (int msgNum = start; msgNum <= end; msgNum++) {
                Pop3Message message = mMsgNumToMsgMap.get(msgNum);
                if (message == null) {
                    /*
                     * There could be gaps in the message numbers or malformed
                     * responses which lead to "gaps" in mMsgNumToMsgMap.
                     *
                     * See issue 2252
                     */
                    continue;
                }

                if (listener != null) {
                    listener.messageStarted(message.getUid(), i++, (end - start) + 1);
                }
                messages.add(message);
                if (listener != null) {
                    listener.messageFinished(message, i++, (end - start) + 1);
                }
            }
            return messages.toArray(new Message[messages.size()]);
        }

        /**
         * Ensures that the given message set (from start to end inclusive)
         * has been queried so that uids are available in the local cache.
         * @param start
         * @param end
         * @throws MessagingException
         * @throws IOException
         */
        private void indexMsgNums(int start, int end)
        throws MessagingException, IOException {
            int unindexedMessageCount = 0;
            for (int msgNum = start; msgNum <= end; msgNum++) {
                if (mMsgNumToMsgMap.get(msgNum) == null) {
                    unindexedMessageCount++;
                }
            }
            if (unindexedMessageCount == 0) {
                return;
            }
            if (unindexedMessageCount < 50 && mMessageCount > 5000) {
                /*
                 * In extreme cases we'll do a UIDL command per message instead of a bulk
                 * download.
                 */
                for (int msgNum = start; msgNum <= end; msgNum++) {
                    Pop3Message message = mMsgNumToMsgMap.get(msgNum);
                    if (message == null) {
                        String response = executeSimpleCommand(UIDL_COMMAND + " " + msgNum);
                        int uidIndex = response.lastIndexOf(' ');
                        String msgUid = response.substring(uidIndex + 1);
                        message = new Pop3Message(msgUid, this);
                        indexMessage(msgNum, message);
                    }
                }
            } else {
                String response = executeSimpleCommand(UIDL_COMMAND);
                while ((response = readLine()) != null) {
                    if (response.equals(".")) {
                        break;
                    }

                    /*
                     * Yet another work-around for buggy server software:
                     * split the response into message number and unique identifier, no matter how many spaces it has
                     *
                     * Example for a malformed response:
                     * 1   2011071307115510400ae3e9e00bmu9
                     *
                     * Note the three spaces between message number and unique identifier.
                     * See issue 3546
                     */

                    String[] uidParts = response.split(" +");
                    if ((uidParts.length >= 3) && "+OK".equals(uidParts[0])) {
                        /*
                         * At least one server software places a "+OK" in
                         * front of every line in the unique-id listing.
                         *
                         * Fix up the array if we detected this behavior.
                         * See Issue 1237
                         */
                        uidParts[0] = uidParts[1];
                        uidParts[1] = uidParts[2];
                    }
                    if (uidParts.length >= 2) {
                        Integer msgNum = Integer.valueOf(uidParts[0]);
                        String msgUid = uidParts[1];
                        if (msgNum >= start && msgNum <= end) {
                            Pop3Message message = mMsgNumToMsgMap.get(msgNum);
                            if (message == null) {
                                message = new Pop3Message(msgUid, this);
                                indexMessage(msgNum, message);
                            }
                        }
                    }
                }
            }
        }

        private void indexUids(ArrayList<String> uids)
        throws MessagingException, IOException {
            HashSet<String> unindexedUids = new HashSet<String>();
            for (String uid : uids) {
                if (mUidToMsgMap.get(uid) == null) {
                    if (K9.DEBUG && K9.DEBUG_PROTOCOL_POP3) {
                        Log.d(K9.LOG_TAG, "Need to index UID " + uid);
                    }
                    unindexedUids.add(uid);
                }
            }
            if (unindexedUids.isEmpty()) {
                return;
            }
            /*
             * If we are missing uids in the cache the only sure way to
             * get them is to do a full UIDL list. A possible optimization
             * would be trying UIDL for the latest X messages and praying.
             */
            String response = executeSimpleCommand(UIDL_COMMAND);
            while ((response = readLine()) != null) {
                if (response.equals(".")) {
                    break;
                }
                String[] uidParts = response.split(" ");

                // Ignore messages without a unique-id
                if (uidParts.length >= 2) {
                    Integer msgNum = Integer.valueOf(uidParts[0]);
                    String msgUid = uidParts[1];
                    if (unindexedUids.contains(msgUid)) {
                        if (K9.DEBUG && K9.DEBUG_PROTOCOL_POP3) {
                            Log.d(K9.LOG_TAG, "Got msgNum " + msgNum + " for UID " + msgUid);
                        }

                        Pop3Message message = mUidToMsgMap.get(msgUid);
                        if (message == null) {
                            message = new Pop3Message(msgUid, this);
                        }
                        indexMessage(msgNum, message);
                    }
                }
            }
        }

        private void indexMessage(int msgNum, Pop3Message message) {
            if (K9.DEBUG && K9.DEBUG_PROTOCOL_POP3) {
                Log.d(K9.LOG_TAG, "Adding index for UID " + message.getUid() + " to msgNum " + msgNum);
            }
            mMsgNumToMsgMap.put(msgNum, message);
            mUidToMsgMap.put(message.getUid(), message);
            mUidToMsgNumMap.put(message.getUid(), msgNum);
        }

        @Override
        public Message[] getMessages(MessageRetrievalListener listener) throws MessagingException {
            throw new UnsupportedOperationException("Pop3: No getMessages");
        }

        @Override
        public Message[] getMessages(String[] uids, MessageRetrievalListener listener)
        throws MessagingException {
            throw new UnsupportedOperationException("Pop3: No getMessages by uids");
        }

        /**
         * Fetch the items contained in the FetchProfile into the given set of
         * Messages in as efficient a manner as possible.
         * @param messages
         * @param fp
         * @throws MessagingException
         */
        @Override
        public void fetch(Message[] messages, FetchProfile fp, MessageRetrievalListener listener)
        throws MessagingException {
            if (messages == null || messages.length == 0) {
                return;
            }
            ArrayList<String> uids = new ArrayList<String>();
            for (Message message : messages) {
                uids.add(message.getUid());
            }
            try {
                indexUids(uids);
            } catch (IOException ioe) {
                throw new MessagingException("fetch", ioe);
            }
            try {
                if (fp.contains(FetchProfile.Item.ENVELOPE)) {
                    /*
                     * We pass the listener only if there are other things to do in the
                     * FetchProfile. Since fetchEnvelop works in bulk and eveything else
                     * works one at a time if we let fetchEnvelope send events the
                     * event would get sent twice.
                     */
                    fetchEnvelope(messages, fp.size() == 1 ? listener : null);
                }
            } catch (IOException ioe) {
                throw new MessagingException("fetch", ioe);
            }
            for (int i = 0, count = messages.length; i < count; i++) {
                Message message = messages[i];
                if (!(message instanceof Pop3Message)) {
                    throw new MessagingException("Pop3Store.fetch called with non-Pop3 Message");
                }
                Pop3Message pop3Message = (Pop3Message)message;
                try {
                    if (listener != null && !fp.contains(FetchProfile.Item.ENVELOPE)) {
                        listener.messageStarted(pop3Message.getUid(), i, count);
                    }
                    if (fp.contains(FetchProfile.Item.BODY)) {
                        fetchBody(pop3Message, -1);
                    } else if (fp.contains(FetchProfile.Item.BODY_SANE)) {
                        /*
                         * To convert the suggested download size we take the size
                         * divided by the maximum line size (76).
                         */
                        if (mAccount.getMaximumAutoDownloadMessageSize() > 0) {
                            fetchBody(pop3Message,
                                      (mAccount.getMaximumAutoDownloadMessageSize() / 76));
                        } else {
                            fetchBody(pop3Message, -1);
                        }
                    } else if (fp.contains(FetchProfile.Item.STRUCTURE)) {
                        /*
                         * If the user is requesting STRUCTURE we are required to set the body
                         * to null since we do not support the function.
                         */
                        pop3Message.setBody(null);
                    }
                    if (listener != null && !(fp.contains(FetchProfile.Item.ENVELOPE) && fp.size() == 1)) {
                        listener.messageFinished(message, i, count);
                    }
                } catch (IOException ioe) {
                    throw new MessagingException("Unable to fetch message", ioe);
                }
            }
        }

        private void fetchEnvelope(Message[] messages,
                                   MessageRetrievalListener listener)  throws IOException, MessagingException {
            int unsizedMessages = 0;
            for (Message message : messages) {
                if (message.getSize() == -1) {
                    unsizedMessages++;
                }
            }
            if (unsizedMessages == 0) {
                return;
            }
            if (unsizedMessages < 50 && mMessageCount > 5000) {
                /*
                 * In extreme cases we'll do a command per message instead of a bulk request
                 * to hopefully save some time and bandwidth.
                 */
                for (int i = 0, count = messages.length; i < count; i++) {
                    Message message = messages[i];
                    if (!(message instanceof Pop3Message)) {
                        throw new MessagingException("Pop3Store.fetch called with non-Pop3 Message");
                    }
                    Pop3Message pop3Message = (Pop3Message)message;
                    if (listener != null) {
                        listener.messageStarted(pop3Message.getUid(), i, count);
                    }
                    String response = executeSimpleCommand(String.format(LIST_COMMAND + " %d",
                                                           mUidToMsgNumMap.get(pop3Message.getUid())));
                    String[] listParts = response.split(" ");
                    //int msgNum = Integer.parseInt(listParts[1]);
                    int msgSize = Integer.parseInt(listParts[2]);
                    pop3Message.setSize(msgSize);
                    if (listener != null) {
                        listener.messageFinished(pop3Message, i, count);
                    }
                }
            } else {
                HashSet<String> msgUidIndex = new HashSet<String>();
                for (Message message : messages) {
                    msgUidIndex.add(message.getUid());
                }
                int i = 0, count = messages.length;
                String response = executeSimpleCommand(LIST_COMMAND);
                while ((response = readLine()) != null) {
                    if (response.equals(".")) {
                        break;
                    }
                    String[] listParts = response.split(" ");
                    int msgNum = Integer.parseInt(listParts[0]);
                    int msgSize = Integer.parseInt(listParts[1]);
                    Pop3Message pop3Message = mMsgNumToMsgMap.get(msgNum);
                    if (pop3Message != null && msgUidIndex.contains(pop3Message.getUid())) {
                        if (listener != null) {
                            listener.messageStarted(pop3Message.getUid(), i, count);
                        }
                        pop3Message.setSize(msgSize);
                        if (listener != null) {
                            listener.messageFinished(pop3Message, i, count);
                        }
                        i++;
                    }
                }
            }
        }

        /**
         * Fetches the body of the given message, limiting the downloaded data to the specified
         * number of lines if possible.
         *
         * If lines is -1 the entire message is fetched. This is implemented with RETR for
         * lines = -1 or TOP for any other value. If the server does not support TOP, RETR is used
         * instead.
         */
        private void fetchBody(Pop3Message message, int lines)
        throws IOException, MessagingException {
            String response = null;

            // Try hard to use the TOP command if we're not asked to download the whole message.
            if (lines != -1 && (!mTopNotSupported || mCapabilities.top)) {
                try {
                    if (K9.DEBUG && K9.DEBUG_PROTOCOL_POP3 && !mCapabilities.top) {
                        Log.d(K9.LOG_TAG, "This server doesn't support the CAPA command. " +
                              "Checking to see if the TOP command is supported nevertheless.");
                    }

                    response = executeSimpleCommand(String.format(TOP_COMMAND + " %d %d",
                                                    mUidToMsgNumMap.get(message.getUid()), lines));

                    // TOP command is supported. Remember this for the next time.
                    mCapabilities.top = true;
                } catch (Pop3ErrorResponse e) {
                    if (mCapabilities.top) {
                        // The TOP command should be supported but something went wrong.
                        throw e;
                    } else {
                        if (K9.DEBUG && K9.DEBUG_PROTOCOL_POP3) {
                            Log.d(K9.LOG_TAG, "The server really doesn't support the TOP " +
                                  "command. Using RETR instead.");
                        }

                        // Don't try to use the TOP command again.
                        mTopNotSupported = true;
                    }
                }
            }

            if (response == null) {
                executeSimpleCommand(String.format(RETR_COMMAND + " %d",
                                     mUidToMsgNumMap.get(message.getUid())));
            }

            try {
                message.parse(new Pop3ResponseInputStream(mIn));

                // TODO: if we've received fewer lines than requested we also have the complete message.
                if (lines == -1 || !mCapabilities.top) {
                    message.setFlag(Flag.X_DOWNLOADED_FULL, true);
                }
            } catch (MessagingException me) {
                /*
                 * If we're only downloading headers it's possible
                 * we'll get a broken MIME message which we're not
                 * real worried about. If we've downloaded the body
                 * and can't parse it we need to let the user know.
                 */
                if (lines == -1) {
                    throw me;
                }
            }
        }

        @Override
        public Flag[] getPermanentFlags() {
            return PERMANENT_FLAGS;
        }

        @Override
        public Map<String, String> appendMessages(Message[] messages) throws MessagingException {
            return null;
        }

        @Override
        public void delete(boolean recurse) throws MessagingException {
        }

        @Override
        public void delete(Message[] msgs, String trashFolderName) throws MessagingException {
            setFlags(msgs, new Flag[] { Flag.DELETED }, true);
        }

        @Override
        public String getUidFromMessageId(Message message) throws MessagingException {
            return null;
        }

        @Override
        public void setFlags(Flag[] flags, boolean value) throws MessagingException {
            throw new UnsupportedOperationException("POP3: No setFlags(Flag[],boolean)");
        }

        @Override
        public void setFlags(Message[] messages, Flag[] flags, boolean value)
        throws MessagingException {
            if (!value || !Utility.arrayContains(flags, Flag.DELETED)) {
                /*
                 * The only flagging we support is setting the Deleted flag.
                 */
                return;
            }
            ArrayList<String> uids = new ArrayList<String>();
            try {
                for (Message message : messages) {
                    uids.add(message.getUid());
                }

                indexUids(uids);
            } catch (IOException ioe) {
                throw new MessagingException("Could not get message number for uid " + uids, ioe);
            }
            for (Message message : messages) {

                Integer msgNum = mUidToMsgNumMap.get(message.getUid());
                if (msgNum == null) {
                    MessagingException me = new MessagingException("Could not delete message " + message.getUid()
                            + " because no msgNum found; permanent error");
                    me.setPermanentFailure(true);
                    throw me;
                }
                executeSimpleCommand(String.format(DELE_COMMAND + " %s", msgNum));
            }
        }

        private String readLine() throws IOException {
            StringBuilder sb = new StringBuilder();
            int d = mIn.read();
            if (d == -1) {
                throw new IOException("End of stream reached while trying to read line.");
            }
            do {
                if (((char)d) == '\r') {
                    continue;
                } else if (((char)d) == '\n') {
                    break;
                } else {
                    sb.append((char)d);
                }
            } while ((d = mIn.read()) != -1);
            String ret = sb.toString();
            if (K9.DEBUG && K9.DEBUG_PROTOCOL_POP3) {
                Log.d(K9.LOG_TAG, "<<< " + ret);
            }
            return ret;
        }

        private void writeLine(String s) throws IOException {
            mOut.write(s.getBytes());
            mOut.write('\r');
            mOut.write('\n');
            mOut.flush();
        }

        private Pop3Capabilities getCapabilities() throws IOException {
            Pop3Capabilities capabilities = new Pop3Capabilities();
            try {
                String response = executeSimpleCommand(CAPA_COMMAND);
                while ((response = readLine()) != null) {
                    if (response.equals(".")) {
                        break;
                    }
                    if (response.equalsIgnoreCase(STLS_CAPABILITY)) {
                        capabilities.stls = true;
                    } else if (response.equalsIgnoreCase(UIDL_CAPABILITY)) {
                        capabilities.uidl = true;
                    } else if (response.equalsIgnoreCase(PIPELINING_CAPABILITY)) {
                        capabilities.pipelining = true;
                    } else if (response.equalsIgnoreCase(USER_CAPABILITY)) {
                        capabilities.user = true;
                    } else if (response.equalsIgnoreCase(TOP_CAPABILITY)) {
                        capabilities.top = true;
                    }
                }

                if (!capabilities.top) {
                    /*
                     * If the CAPA command is supported but it doesn't advertise support for the
                     * TOP command, we won't check for it manually.
                     */
                    mTopNotSupported = true;
                }
            } catch (MessagingException me) {
                /*
                 * The server may not support the CAPA command, so we just eat this Exception
                 * and allow the empty capabilities object to be returned.
                 */
            }
            return capabilities;
        }

        private String executeSimpleCommand(String command) throws MessagingException {
            return executeSimpleCommand(command, false);
        }

        private String executeSimpleCommand(String command, boolean sensitive) throws MessagingException {
            try {
                open(OpenMode.READ_WRITE);

                if (command != null) {
                    if (K9.DEBUG && K9.DEBUG_PROTOCOL_POP3) {
                        if (sensitive && !K9.DEBUG_SENSITIVE) {
                            Log.d(K9.LOG_TAG, ">>> "
                                  + "[Command Hidden, Enable Sensitive Debug Logging To Show]");
                        } else {
                            Log.d(K9.LOG_TAG, ">>> " + command);
                        }
                    }

                    writeLine(command);
                }

                String response = readLine();
                if (response.length() > 1 && response.charAt(0) == '-') {
                    throw new Pop3ErrorResponse(response);
                }

                return response;
            } catch (MessagingException me) {
                throw me;
            } catch (Exception e) {
                closeIO();
                throw new MessagingException("Unable to execute POP3 command", e);
            }
        }

        @Override
        public boolean isFlagSupported(Flag flag) {
            return (flag == Flag.DELETED);
        }

        @Override
        public boolean supportsFetchingFlags() {
            return false;
        }

        @Override
        public boolean equals(Object o) {
            if (o instanceof Pop3Folder) {
                return ((Pop3Folder) o).mName.equals(mName);
            }
            return super.equals(o);
        }

        @Override
        public int hashCode() {
            return mName.hashCode();
        }

    }//Pop3Folder

    static class Pop3Message extends MimeMessage {
        public Pop3Message(String uid, Pop3Folder folder) {
            mUid = uid;
            mFolder = folder;
            mSize = -1;
        }

        public void setSize(int size) {
            mSize = size;
        }

        @Override
        protected void parse(InputStream in) throws IOException, MessagingException {
            super.parse(in);
        }

        @Override
        public void setFlag(Flag flag, boolean set) throws MessagingException {
            super.setFlag(flag, set);
            mFolder.setFlags(new Message[] { this }, new Flag[] { flag }, set);
        }

        @Override
        public void delete(String trashFolderName) throws MessagingException {
            //  try
            //  {
            //  Poor POP3 users, we can't copy the message to the Trash folder, but they still want a delete
            setFlag(Flag.DELETED, true);
            //   }
//         catch (MessagingException me)
//         {
//          Log.w(K9.LOG_TAG, "Could not delete non-existent message", me);
//         }
        }
    }

    static class Pop3Capabilities {
        public boolean stls;
        public boolean top;
        public boolean user;
        public boolean uidl;
        public boolean pipelining;

        @Override
        public String toString() {
            return String.format("STLS %b, TOP %b, USER %b, UIDL %b, PIPELINING %b",
                                 stls,
                                 top,
                                 user,
                                 uidl,
                                 pipelining);
        }
    }

    static class Pop3ResponseInputStream extends InputStream {
        InputStream mIn;
        boolean mStartOfLine = true;
        boolean mFinished;

        public Pop3ResponseInputStream(InputStream in) {
            mIn = in;
        }

        @Override
        public int read() throws IOException {
            if (mFinished) {
                return -1;
            }
            int d = mIn.read();
            if (mStartOfLine && d == '.') {
                d = mIn.read();
                if (d == '\r') {
                    mFinished = true;
                    mIn.read();
                    return -1;
                }
            }

            mStartOfLine = (d == '\n');

            return d;
        }
    }

    /**
     * Exception that is thrown if the server returns an error response.
     */
    static class Pop3ErrorResponse extends MessagingException {
        public Pop3ErrorResponse(String message) {
            super(message, true);
        }
    }
}


text:
layout: things-item
permalink: /:categories/:title
title: The National Wool Museum
website: http://www.geelongaustralia.com.au/nwm/
map: National+Wool+Museum/@-38.145425,144.3589833
parkweb:
wikipedia: National_Wool_Museum_(Geelong)
state: victoria
area: geelong
tags:
- things-geelong
- things-victoria
---

Back in the day, they used to say that the Australian economy rode on the sheep's back.
Basically this meant that the only worthwhile thing we provided the rest of the world was wool, and to a lesser extent, lamb and other wool by-products, such as mutton.
These are their stories.

redpajama_set_name: RedPajamaGithub


text:
A speech to text library using portaudio to record and CMU PocketSphinx to transcribe.

### Example

The example below uses the CMU PocketSphinx driver provided by the included [psphinx](./engines/psphinx) package. See the [Engines](#engines) section for available speech to text engines.

```go
import (
	"github.com/kladd/stt"
	"github.com/kladd/stt/engines/psphinx"
)

func main() {
	transcriber := new(psphinx.Transcriber)

	// Records for 10 seconds before transcribing
	fmt.Println(stt.RecordAndTranscribe(transcriber, 10))
}
```

### Engines

* CMU PocketSphinx - [pshinx](./engines/pshinx)

### Caveats & Dependencies

Dependencies:

* portaudio
* cmu-sphinxbase
* cmu-pocketsphinx

Caveats:

* Not very accurate right now

### Installation

```bash
go get github.com/kladd/stt
```

### License

See [LICENSE](./LICENSE)

### Contributing

Appreciated. Fork, branch, commit, push, pull request.


text:
The typed objects library includes a number of value types, which represent immutable data. Type objects that represent value types can be called to perform their coercions, but they cannot be invoked with new.

The numeric value types are:

uint8, uint8Clamped : 8-bit unsigned integers
uint16 : 16-bit unsigned integers
uint32 : 32-bit unsigned integers
int8 : 8-bit signed integers
int16 : 16-bit signed integers
int32 : 32-bit signed integers
float32 : 32-bit IEEE754 floating-point numbers
float64 : 64-bit IEEE754 floating-point numbers
Each numeric value type's coercion is the standard coercion, except for uint8Clamped, which performs a saturating coercion.

The other value types are:

boolean : ECMAScript primitive boolean
string : ECMAScript primitive string
The coercions associated with these types are the standard ECMAScript algorithms [[ToBoolean]] and [[ToString]], respectively.


text:
ACCEPTED

#### According to
Index Fungorum

#### Published in
null

#### Original name
Xylomites deformis Unger

### Remarks
null


text:
<?hh

// Composer didn't support autoloading enums until recently (2015-03-09)
require_once('ReflectionXHPAttribute.php');
require_once('ReflectionXHPChildrenDeclaration.php');

abstract class :x:composable-element extends :xhp {
  private Map<string, mixed> $attributes = Map {};
  private Vector<XHPChild> $children = Vector {};
  private Map<string, mixed> $context = Map {};

  protected function init(): void {}

  /**
   * A new :x:composable-element is instantiated for every literal tag
   * expression in the script.
   *
   * The following code:
   * $foo = <foo attr="val">bar</foo>;
   *
   * will execute something like:
   * $foo = new xhp_foo(array('attr' => 'val'), array('bar'));
   *
   * @param $attributes    map of attributes to values
   * @param $children      list of children
   */
  final public function __construct(KeyedTraversable<string, mixed> $attributes,
                                    Traversable<XHPChild> $children) {
    parent::__construct($attributes, $children);
    foreach ($children as $child) {
      $this->appendChild($child);
    }
    $this->setAttributes($attributes);
    if (:xhp::$ENABLE_VALIDATION) {
      // There is some cost to having defaulted unused arguments on a function
      // so we leave these out and get them with func_get_args().
      $args = func_get_args();
      if (isset($args[2])) {
        $this->source = "$args[2]:$args[3]";
      } else {
        $this->source =
          'You have ENABLE_VALIDATION on, but debug information is not being ' .
          'passed to XHP objects correctly. Ensure xhp.include_debug is on ' .
          'in your PHP configuration. Without this option enabled, ' .
          'validation errors will be painful to debug at best.';
      }
    }
    $this->init();
  }

  /**
   * Adds a child to the end of this node. If you give an array to this method
   * then it will behave like a DocumentFragment.
   *
   * @param $child     single child or array of children
   */
  final public function appendChild(mixed $child): this {
    if ($child instanceof Traversable) {
      foreach ($child as $c) {
        $this->appendChild($c);
      }
    } else if ($child instanceof :x:frag) {
      $this->children->addAll($child->getChildren());
    } else if ($child !== null) {
      assert($child instanceof XHPChild);
      $this->children->add($child);
    }
    return $this;
  }

  /**
   * Adds a child to the beginning of this node. If you give an array to this
   * method then it will behave like a DocumentFragment.
   *
   * @param $child     single child or array of children
   */
  final public function prependChild(mixed $child): this {
    // There's no prepend to a Vector, so reverse, append, and reverse agains
    $this->children->reverse();
    $this->appendChild($child);
    $this->children->reverse();
    return $this;
  }

  /**
   * Replaces all children in this node. You may pass a single array or
   * multiple parameters.
   *
   * @param $children  Single child or array of children
   */
  final public function replaceChildren(...): this {
    // This function has been micro-optimized
    $args = func_get_args();
    $new_children = Vector {};
    foreach ($args as $xhp) {
      if ($xhp) {
        if ($xhp instanceof :x:frag) {
          foreach ($xhp->children as $child) {
            $new_children->add($child);
          }
        } else if (!($xhp instanceof Traversable)) {
          $new_children->add($xhp);
        } else {
          foreach ($xhp as $element) {
            if ($element instanceof :x:frag) {
              foreach ($element->children as $child) {
                $new_children->add($child);
              }
            } else if ($element !== null) {
              $new_children->add($element);
            }
          }
        }
      }
    }
    $this->children = $new_children;
    return $this;
  }

  /**
   * Fetches all direct children of this element that match a particular tag
   * name or category (or all children if none is given)
   *
   * @param $selector   tag name or category (optional)
   * @return array
   */
  final public function getChildren(
    ?string $selector = null,
  ): Vector<XHPChild> {
    if ($selector) {
      $children = Vector {};
      if ($selector[0] == '%') {
        $selector = substr($selector, 1);
        foreach ($this->children as $child) {
          if ($child instanceof :xhp && $child->categoryOf($selector)) {
            $children->add($child);
          }
        }
      } else {
        $selector = :xhp::element2class($selector);
        foreach ($this->children as $child) {
          if ($child instanceof $selector) {
            $children->add($child);
          }
        }
      }
    } else {
      $children = new Vector($this->children);
    }
    return $children;
  }

  /**
   * Fetches the first direct child of the element, or the first child that
   * matches the tag if one is given
   *
   * @param $selector   string   tag name or category (optional)
   * @return            element  the first child node (with the given selector),
   *                             false if there are no (matching) children
   */
  final public function getFirstChild(?string $selector = null): ?XHPChild {
    if (!$selector) {
      return $this->children->get(0);
    } else if ($selector[0] == '%') {
      $selector = substr($selector, 1);
      foreach ($this->children as $child) {
        if ($child instanceof :xhp && $child->categoryOf($selector)) {
          return $child;
        }
      }
    } else {
      $selector = :xhp::element2class($selector);
      foreach ($this->children as $child) {
        if ($child instanceof $selector) {
          return $child;
        }
      }
    }
    return null;
  }

  /**
   * Fetches the last direct child of the element, or the last child that
   * matches the tag or category if one is given
   *
   * @param $selector  string   tag name or category (optional)
   * @return           element  the last child node (with the given selector),
   *                            false if there are no (matching) children
   */
  final public function getLastChild(?string $selector = null): ?XHPChild {
    $temp = $this->getChildren($selector);
    if ($temp->count() > 0) {
      $count = $temp->count();
      return $temp->at($count - 1);
    }
    return null;
  }

  /**
   * Fetches an attribute from this elements attribute store. If $attr is not
   * defined in the store and is not a data- or aria- attribute an exception
   * will be thrown. An exception will also be thrown if $attr is required and
   * not set.
   *
   * @param $attr      attribute to fetch
   * @return           value
   */
  final public function getAttribute(string $attr) {
    // Return the attribute if it's there
    if ($this->attributes->containsKey($attr)) {
      return $this->attributes->get($attr);
    }

    if (!ReflectionXHPAttribute::IsSpecial($attr)) {
      // Get the declaration
      $decl = static::__xhpReflectionAttribute($attr);

      if ($decl === null) {
        throw new XHPAttributeNotSupportedException($this, $attr);
      } else if ($decl->isRequired()) {
        throw new XHPAttributeRequiredException($this, $attr);
      } else {
        return $decl->getDefaultValue();
      }
    } else {
      return null;
    }
  }

  final public static function __xhpReflectionAttribute(
    string $attr,
  ): ?ReflectionXHPAttribute {
    $map = static::__xhpReflectionAttributes();
    if ($map->containsKey($attr)) {
      return $map[$attr];
    }
    return null;
  }

  final public static function __xhpReflectionAttributes(
  ): Map<string, ReflectionXHPAttribute> {
    static $cache = Map { };
    $class = static::class;
    if (!$cache->containsKey($class)) {
      $map = Map { };
      $decl = static::__xhpAttributeDeclaration();
      foreach ($decl as $name => $attr_decl) {
        $map[$name] = new ReflectionXHPAttribute($name, $attr_decl);
      }
      $cache[$class] = $map;
    }
    return $cache[$class];
  }

  final public static function __xhpReflectionChildrenDeclaration(
  ): ReflectionXHPChildrenDeclaration {
    static $cache = Map { };
    $class = static::class;
    if (!$cache->containsKey($class)) {
      $cache[$class] = new ReflectionXHPChildrenDeclaration(
        :xhp::class2element($class),
        /* UNSAFE_EXPR: This isn't a static method for some reason - but it
         * always returns a static array, and is safe to call statically */
        static::__xhpChildrenDeclaration(),
      );
    }
    return $cache[$class];
  }

  final public static function __xhpReflectionCategoryDeclaration(
  ): Set<string> {
    return new Set(
      /* UNSAFE_EXPR: This isn't a static method for some reason - but it
       * always returns a static array, and is safe to call statically */
       array_keys(static::__xhpCategoryDeclaration())
    );
  }

  final public function getAttributes(): Map<string, mixed> {
    return $this->attributes->toMap();
  }

  /**
   * Sets an attribute in this element's attribute store. If the attribute is
   * not defined in the store and is not a data- or aria- attribute an
   * exception will be thrown. An exception will also be thrown if the
   * attribute value is invalid.
   *
   * @param $attr      attribute to set
   * @param $val       value
   */
  final public function setAttribute(string $attr, mixed $value): this {
    if (!ReflectionXHPAttribute::IsSpecial($attr)) {
      $value = $this->validateAttributeValue($attr, $value);
    } else {
      $value = (string)$value;
    }
    $this->attributes->set($attr, $value);
    return $this;
  }

  /**
   * Takes an array of key/value pairs and adds each as an attribute.
   *
   * @param $attrs    array of attributes
   */
  final public function setAttributes(
    KeyedTraversable<string, mixed> $attrs,
  ): this {
    foreach ($attrs as $key => $value) {
      $this->setAttribute($key, $value);
    }
    return $this;
  }

  /**
   * Whether the attribute has been explicitly set to a non-null value by the
   * caller (vs. using the default set by "attribute" in the class definition).
   *
   * @param $attr attribute to check
   */
  final public function isAttributeSet(string $attr): bool {
    return $this->attributes->containsKey($attr);
  }

  /**
   * Removes an attribute from this element's attribute store. An exception
   * will be thrown if $attr is not supported.
   *
   * @param $attr      attribute to remove
   * @param $val       value
   */
  final public function removeAttribute(string $attr): this {
    if (!ReflectionXHPAttribute::IsSpecial($attr)) {
      $value = $this->validateAttributeValue($attr, null);
    }
    $this->attributes->removeKey($attr);
    return $this;
  }

  /**
   * Sets an attribute in this element's attribute store. Always foregoes
   * validation.
   *
   * @param $attr      attribute to set
   * @param $val       value
   */
  final public function forceAttribute(string $attr, mixed $value): this {
    $this->attributes->set($attr, $value);
    return $this;
  }
  /**
   * Returns all contexts currently set.
   *
   * @return array  All contexts
   */
  final public function getAllContexts(): Map<string, mixed> {
    return $this->context->toMap();
  }

  /**
   * Returns a specific context value. Can include a default if not set.
   *
   * @param string $key     The context key
   * @param mixed $default  The value to return if not set (optional)
   * @return mixed          The context value or $default
   */
  final public function getContext(string $key, mixed $default = null): mixed {
    if ($this->context->containsKey($key)) {
      return $this->context->get($key);
    }
    return $default;
  }

  /**
   * Sets a value that will be automatically passed down through a render chain
   * and can be referenced by children and composed elements. For instance, if
   * a root element sets a context of "admin_mode" = true, then all elements
   * that are rendered as children of that root element will receive this
   * context WHEN RENDERED. The context will not be available before render.
   *
   * @param mixed $key      Either a key, or an array of key/value pairs
   * @param mixed $default  if $key is a string, the value to set
   * @return :xhp           $this
   */
  final public function setContext(string $key, mixed $value): this {
    $this->context->set($key, $value);
    return $this;
  }

  /**
   * Sets a value that will be automatically passed down through a render chain
   * and can be referenced by children and composed elements. For instance, if
   * a root element sets a context of "admin_mode" = true, then all elements
   * that are rendered as children of that root element will receive this
   * context WHEN RENDERED. The context will not be available before render.
   *
   * @param Map $context  A map of key/value pairs
   * @return :xhp         $this
   */
  final public function addContextMap(Map<string, mixed> $context): this {
    $this->context->setAll($context);
    return $this;
  }

  /**
   * Transfers the context but will not overwrite anything. This is done only
   * for rendering because we don't want a parent's context to replace a
   * child's context if they have the same key.
   *
   * @param array $parentContext  The context to transfer
   */
  final protected function __transferContext(
    Map<string, mixed> $parentContext,
  ): void {
    foreach ($parentContext as $key => $value) {
      if (!$this->context->containsKey($key)) {
        $this->context->set($key, $value);
      }
    }
  }

  abstract protected function __flushSubtree(): Awaitable<:x:primitive>;

  /**
   * Defined in elements by the `attribute` keyword. The declaration is simple.
   * There is a keyed array, with each key being an attribute. Each value is
   * an array with 4 elements. The first is the attribute type. The second is
   * meta-data about the attribute. The third is a default value (null for
   * none). And the fourth is whether or not this value is required.
   *
   * Attribute types are suggested by the TYPE_* constants.
   */
  protected static function &__xhpAttributeDeclaration(
  ): array<string, array<int, mixed>> {
    static $decl = array();
    return $decl;
  }

  /**
   * Defined in elements by the `category` keyword. This is just a list of all
   * categories an element belongs to. Each category is a key with value 1.
   */
  protected function &__xhpCategoryDeclaration(): array<string, int> {
    static $decl = array();
    return $decl;
  }

  /**
   * Defined in elements by the `children` keyword. This returns a pattern of
   * allowed children. The return value is potentially very complicated. The
   * two simplest are 0 and 1 which mean no children and any children,
   * respectively. Otherwise you're dealing with an array which is just the
   * biggest mess you've ever seen.
   */
  protected function &__xhpChildrenDeclaration(): mixed {
    static $decl = 1;
    return $decl;
  }

  /**
   * Throws an exception if $val is not a valid value for the attribute $attr
   * on this element.
   */
  final protected function validateAttributeValue<T>(
    string $attr,
    T $val,
  ): mixed {
    $decl = static::__xhpReflectionAttribute($attr);
    if ($decl === null) {
      throw new XHPAttributeNotSupportedException($this, $attr);
    }
    if ($val === null) {
      return null;
    }
    switch ($decl->getValueType()) {
      case XHPAttributeType::TYPE_STRING:
        if (!is_string($val)) {
          $val = XHPAttributeCoercion::CoerceToString($this, $attr, $val);
        }
        break;

      case XHPAttributeType::TYPE_BOOL:
        if (!is_bool($val)) {
          $val = XHPAttributeCoercion::CoerceToBool($this, $attr, $val);
        }
        break;

      case XHPAttributeType::TYPE_INTEGER:
        if (!is_int($val)) {
          $val = XHPAttributeCoercion::CoerceToInt($this, $attr, $val);
        }
        break;

      case XHPAttributeType::TYPE_FLOAT:
        if (!is_float($val)) {
          $val = XHPAttributeCoercion::CoerceToFloat($this, $attr, $val);
        }
        break;

      case XHPAttributeType::TYPE_ARRAY:
        if (!is_array($val)) {
          throw new XHPInvalidAttributeException($this, 'array', $attr, $val);
        }
        break;

      case XHPAttributeType::TYPE_OBJECT:
        $class = $decl->getValueClass();
        if ($val instanceof $class) {
          break;
        }
        if (enum_exists($class) && $class::isValid($val)) {
          break;
        }
        // Things that are a valid array key without any coercion
        if ($class === 'HH\arraykey') {
          if (is_int($val) || is_string($val)) {
            break;
          }
        }
        if ($class === 'HH\num') {
          if (is_int($val) || is_float($val)) {
            break;
          }
        }
        throw new XHPInvalidAttributeException(
          $this, $class, $attr, $val
        );
        break;

      case XHPAttributeType::TYPE_VAR:
        break;

      case XHPAttributeType::TYPE_ENUM:
        if (!(is_string($val) && $decl->getEnumValues()->contains($val))) {
          $enums = 'enum("' . implode('","', $decl->getEnumValues()) . '")';
          throw new XHPInvalidAttributeException($this, $enums, $attr, $val);
        }
        break;

      case XHPAttributeType::TYPE_UNSUPPORTED_LEGACY_CALLABLE:
        throw new XHPUnsupportedAttributeTypeException(
          $this,
          'callable',
          $attr,
          'not supported in XHP-Lib 2.0 or higher.',
        );
    }
    return $val;
  }

  /**
   * Validates that this element's children match its children descriptor, and
   * throws an exception if that's not the case.
   */
  final protected function validateChildren(): void {
    $decl = self::__xhpReflectionChildrenDeclaration();
    $type = $decl->getType();
    if ($type === XHPChildrenDeclarationType::ANY_CHILDREN) {
      return;
    }
    if ($type === XHPChildrenDeclarationType::NO_CHILDREN) {
      if ($this->children) {
        throw new XHPInvalidChildrenException($this, 0);
      } else {
        return;
      }
    }
    list($ret, $ii) = $this->validateChildrenExpression(
      $decl->getExpression(),
      0
    );
    if (!$ret || $ii < count($this->children)) {
      if (isset($this->children[$ii])
          && $this->children[$ii] instanceof XHPAlwaysValidChild) {
        return;
      }
      throw new XHPInvalidChildrenException($this, $ii);
    }
  }

  final private function validateChildrenExpression(
    ReflectionXHPChildrenExpression $expr,
    int $index,
  ): (bool, int) {
    switch ($expr->getType()) {
      case XHPChildrenExpressionType::SINGLE:
        // Exactly once -- :fb-thing
        return $this->validateChildrenRule($expr, $index);
      case XHPChildrenExpressionType::ANY_NUMBER:
        // Zero or more times -- :fb-thing*
        do {
          list($ret, $index) = $this->validateChildrenRule(
            $expr,
            $index,
          );
        } while ($ret);
        return tuple(true, $index);

      case XHPChildrenExpressionType::ZERO_OR_ONE:
        // Zero or one times -- :fb-thing?
        list($_, $index) = $this->validateChildrenRule(
          $expr,
          $index,
        );
        return tuple(true, $index);

      case XHPChildrenExpressionType::ONE_OR_MORE:
        // One or more times -- :fb-thing+
        list($ret, $index) = $this->validateChildrenRule(
          $expr,
          $index,
        );
        if (!$ret) {
          return tuple(false, $index);
        }
        do {
          list($ret, $index) = $this->validateChildrenRule(
            $expr,
            $index,
          );
        } while ($ret);
        return tuple(true, $index);

      case XHPChildrenExpressionType::SUB_EXPR_SEQUENCE:
        // Specific order -- :fb-thing, :fb-other-thing
        $oindex = $index;
        list($sub_expr_1, $sub_expr_2) = $expr->getSubExpressions();
        list($ret, $index) = $this->validateChildrenExpression(
          $sub_expr_1,
          $index,
        );
        if ($ret) {
          list($ret, $index) = $this->validateChildrenExpression(
            $sub_expr_2,
            $index,
          );
        }
        if ($ret) {
          return tuple(true, $index);
        }
        return tuple(false, $oindex);

      case XHPChildrenExpressionType::SUB_EXPR_DISJUNCTION:
        // Either or -- :fb-thing | :fb-other-thing
        $oindex = $index;
        list($sub_expr_1, $sub_expr_2) = $expr->getSubExpressions();
        list($ret, $index) = $this->validateChildrenExpression(
          $sub_expr_1,
          $index,
        );
        if (!$ret) {
          list($ret, $index) = $this->validateChildrenExpression(
            $sub_expr_2,
            $index,
          );
        }
        if ($ret) {
          return tuple(true, $index);
        }
        return tuple(false, $oindex);
    }
  }

  final private function validateChildrenRule(
    ReflectionXHPChildrenExpression $expr,
    int $index,
  ): (bool, int) {
    switch ($expr->getConstraintType()) {
      case XHPChildrenConstraintType::ANY:
        if ($this->children->containsKey($index)) {
          return tuple(true, $index + 1);
        }
        return tuple(false, $index);

      case XHPChildrenConstraintType::PCDATA:
        if ($this->children->containsKey($index) &&
            !($this->children->get($index) instanceof :xhp)) {
          return tuple(true, $index + 1);
        }
        return tuple(false, $index);

      case XHPChildrenConstraintType::ELEMENT:
        $class = $expr->getConstraintString();
        if ($this->children->containsKey($index) &&
            $this->children->get($index) instanceof $class) {
          return tuple(true, $index + 1);
        }
        return tuple(false, $index);

      case XHPChildrenConstraintType::CATEGORY:
        if (!$this->children->containsKey($index) ||
            !($this->children->get($index) instanceof :xhp)) {
          return tuple(false, $index);
        }
        $category = :xhp::class2element($expr->getConstraintString());
        $child = $this->children->get($index);
        assert($child instanceof :xhp);
        $categories = $child->__xhpCategoryDeclaration();
        if (empty($categories[$category])) {
          return tuple(false, $index);
        }
        return tuple(true, $index + 1);

      case XHPChildrenConstraintType::SUB_EXPR:
        return $this->validateChildrenExpression(
          $expr->getSubExpression(),
          $index,
        );
    }
  }

  /**
   * Returns the human-readable `children` declaration as seen in this class's
   * source code.
   *
   * Keeping this wrapper around reflection, as it fits well with
   * __getChildrenDescription.
   */
  public function __getChildrenDeclaration(): string {
    return (string) self::__xhpReflectionChildrenDeclaration();
  }

  /**
   * Returns a description of the current children in this element. Maybe
   * something like this:
   * <div><span>foo</span>bar</div> ->
   * :span[%inline],pcdata
   */
  final public function __getChildrenDescription(): string {
    $desc = array();
    foreach ($this->children as $child) {
      if ($child instanceof :xhp) {
        $tmp = ':' . :xhp::class2element(get_class($child));
        if ($categories = $child->__xhpCategoryDeclaration()) {
          $tmp .= '[%'. implode(',%', array_keys($categories)) . ']';
        }
        $desc[] = $tmp;
      } else {
        $desc[] = 'pcdata';
      }
    }
    return implode(',', $desc);
  }

  final public function categoryOf(string $c): bool {
    $categories = $this->__xhpCategoryDeclaration();
    if (isset($categories[$c])) {
      return true;
    }
    // XHP parses the category string
    $c = str_replace(array(':', '-'), array('__', '_'), $c);
    return isset($categories[$c]);
  }
}


text:
package com.webcontext.apps.restwebapp.services.business;

import java.util.Collection;
import java.util.List;

import javax.ejb.Stateless;
import javax.inject.Inject;

import com.webcontext.apps.restwebapp.exceptions.EntityAlreadyExistsException;
import com.webcontext.apps.restwebapp.managers.UserManager;
import com.webcontext.apps.restwebapp.model.User;

/**
 * Service to manage users. Can add update, delete and retrieve any users.
 *
 * @author Frédéric Delorme<frederic.delorme@web-context.com>
 *
 */
@Stateless
public class UserService {

	@Inject
	private UserManager userMgr;

	/**
	 * Add a new User to the system.
	 *
	 * @param user
	 *            Add a new user.
	 * @return
	 */
	public User add(User user) throws EntityAlreadyExistsException {
		if (userMgr.findByUsername(user.getUsername()) == null) {
			userMgr.save(user);
		} else {
			throw new EntityAlreadyExistsException("User " + user.getUsername()
					+ " already exixts");
		}
		return user;
	}

	/**
	 * Add a collection of User entities to the service.
	 *
	 * @param users
	 *            List of users to add to the service.
	 * @throws EntityAlreadyExistsException
	 */
	public void add(Collection<User> newUsers)
			throws EntityAlreadyExistsException {
		for (User user : newUsers) {
			add(user);
		}
	}

	/**
	 * Update some data on the user object.
	 *
	 * @param user
	 *            update the user and persist.
	 * @return
	 */
	public User update(User user) {
		userMgr.save(user);
		return user;
	}

	/**
	 * Delete the occurrence of the user object.
	 *
	 * @param user
	 *            the user to be deleted.
	 * @return
	 */
	public void delete(User user) {
		userMgr.delete(user);
	}

	/**
	 * Find a User object for the username.
	 *
	 * @param username
	 *            this is the username to search for.
	 * @return
	 */
	public User findByUsername(String username) {
		return userMgr.findByUsername(username);
	}

	/**
	 * Retrieve all User entities managed by service
	 *
	 * @return
	 */
	public List<User> findAll() {
		return userMgr.findAll();
	}

	/**
	 * Retrieve a User on its internal userId.
	 *
	 * @param userId
	 *            the internal user id.
	 * @return
	 */
	public User findById(Long userId) {
		return userMgr.findById(userId);
	}

	/**
	 * return the size of the managed User list.
	 *
	 * @return
	 */
	public long count() {
		return userMgr.count();
	}

}


text:
<?xml version='1.0' encoding='UTF-8'?>
<Document>
  <Header>
    <SchemaName>Location</SchemaName>
    <Identifier>location_geonames_8062857</Identifier>
    <DocumentState>public</DocumentState>
    <TimeStamp>1390753463855</TimeStamp>
    <SummaryFields>
      <Title>NL > North Brabant > Gemeente Bladel en Netersel > wxh</Title>
    </SummaryFields>
  </Header>
  <Body>
    <Location>
      <GeopoliticalHierarchy>NL,North Brabant,Gemeente Bladel en Netersel,wxh</GeopoliticalHierarchy>
      <LocationName>
        <Appelation>wxh</Appelation>
        <LocationNameType>agricultural facility</LocationNameType>
        <Comments></Comments>
      </LocationName>
      <Coordinates>
        <Longitude>5.29387</Longitude>
        <Latitude>51.396029999999996</Latitude>
      </Coordinates>
      <GeonamesURI>http://www.geonames.org/8062857</GeonamesURI>
      <WikipediaLink/>
      <DbpediaLink/>
    </Location>
  </Body>
</Document>


text:
A small pluggable bot for Discord using [discord.py](https://github.com/Rapptz/discord.py).

If you have no clue what Discord is you can go here: https://discordapp.com/

## Features

Something something useful text

## Extending functionality

Some text to come here soon

## Credentials

Credentials are set through environment variables.

Name | Value | Type | Plugin
--- | --- | --- | ---
**DISCOBOT_EMAIL** | Discord email | Required |
**DISCOBOT_PASS** | Discord password | Required |
**IMGFLIP_USER** | [ImgFlip](https://imgflip.com/) username | Optional | meme.py
**IMGFLIP_PASS** | [ImgFlip](https://imgflip.com/) password | Optional | meme.py

## Installation

Linux is currently the only tested environment.

The bot needs *Python >= v3.5.1* to be able to run, due to the use of `asyncio`.

It's recommended you also use a virtual environment when installing dependencies:
```
$ virtualenv -p python3.5 venv --always-copy
$ source ./venv/bin/activate
```

You also need to install all the requirements

```
$ pip install -r requirements.txt
```

Then run the bot:
```
$ python main.py
```

## Keeping it online

If you want to run your bot online 24/7 you might consider setting up a [supervisor](http://supervisord.org/) config.
If you do end up using supervisor, it's worth noting you **must set the credentials inside
the config**.

A sample supervisor config file can be found togheter with this repo.

## License

The whole project is licensed under an MIT license.


text:
/* vim: set expandtab ts=4 sw=4: */
/*
 * You may redistribute this program and/or modify it under the terms of
 * the GNU General Public License as published by the Free Software Foundation,
 * either version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include "crypto/random/Random.h"
#include "memory/MallocAllocator.h"
#include "util/Bits.h"
#include "util/Base32.h"
#include "util/Assert.h"

#include <stdio.h>

int main()
{
    struct Allocator* alloc = MallocAllocator_new(20000);
    struct Random* rand = Random_new(alloc, NULL, NULL);

    uint8_t bytes[32];
    Random_bytes(rand, bytes, 32);

    uint8_t base32[64];
    Bits_memset(base32, 0, 64);

    Assert_true(Base32_encode(base32, 64, bytes, 32) == 52);

    //printf("base32 encoded: %s\n", base32);

    uint8_t bytes2[32];
    Assert_true(Base32_decode(bytes2, 32, base32, 52) == 32);

    Assert_true(Bits_memcmp(bytes, bytes2, 32) == 0);

    Allocator_free(alloc);
    return 0;
}


text:
    // Copyright: 2015 AlignAlytics
    // License: "https://github.com/PMSI-AlignAlytics/dimple/blob/master/MIT-LICENSE.txt"
    // Source: /src/objects/aggregateMethod/sum.js
    dimple.aggregateMethod.sum = function (lhs, rhs) {
        lhs.value = (lhs.value === null || lhs.value === undefined ? 0 : parseFloat(lhs.value));
        rhs.value = (rhs.value === null || rhs.value === undefined ? 0 : parseFloat(rhs.value));
        return lhs.value + rhs.value;
    };


text:
#ifndef LMS_XML_PARSER_H
#define LMS_XML_PARSER_H

#include <vector>
#include <string>
#include <exception>
#include <map>
#include <stack>

#include "pugixml.hpp"
#include "lms/config.h"
#include "lms/logging/level.h"
#include "lms/time.h"

namespace lms {
namespace internal {

struct ModuleInfo {
    std::string name;
    std::string lib;
    std::string clazz;
    bool mainThread = false;
    std::map<std::string, std::pair<std::string, int>> channelMapping;
    std::map<std::string, lms::Config> configs;
    lms::logging::Level log;

    std::pair<std::string, int> mapChannel(const std::string &name) const {
        auto it = channelMapping.find(name);
        if (it != channelMapping.end()) {
            return it->second;
        } else {
            return std::make_pair(name, 0);
        }
    }
};

struct ServiceInfo {
    std::string name;
    std::string lib;
    std::string clazz;
    std::map<std::string, lms::Config> configs;
    lms::logging::Level log;
};

struct ClockInfo {
    lms::Time cycle;
    bool sleep = false;
    bool sleepCompensate = false;
    lms::Time watchDog;
    bool watchDogEnabled = false;
    bool slowWarnings = false;
};

struct LibraryInfo {
    std::string lib;
};

struct RuntimeInfo {
    ClockInfo clock;
    std::vector<ModuleInfo> modules;
    std::vector<ServiceInfo> services;
    std::vector<LibraryInfo> libraries;
};

/**
 * @brief Traverse an XML node and evaluate <if> tags with the given condition.
 *
 * The <if> tags get evaluated before an XML documented is parsed.
 *
 * Example:
 * The framework started with --flags=logging,norender
 *
 * <framework>
 * <modulesToEnable>
 *   <if set="logging">
 *     <module>image_logger</module>
 *   </if>
 *   <if notSet="norender">
 *     <module>image_renderer</module>
 *   </if>
 * </modulesToEnable>
 * </framework>
 *
 * This gets preprocessed to:
 *
 * <framework>
 * <modulesToEnable>
 *   <module>image_logger</module>
 * </modulesToEnable>
 * </framework>
 *
 * <if> attributes:
 * - set: a single flag. The flag must be set to evaluate the condition to true.
 * - notSet: a single flag. The flag must not be set to evaluate the condition
 *          to true.
 * - anyOf: comma-separated list of flags. One of the flags must be set to
 *          evaluate the condition to true.
 * - allOf: comma-separated list of flags. All of the flags must be set to
 *          evaluate the condition to true.
 * - nothingOf: comma-separated list of flags. None of the flags must be set
 *          to evaluate the condition to true.
 */
void preprocessXML(pugi::xml_node node, const std::vector<std::string> &flags);

/**
 * @brief Parse <config> tag inside of a <module> tag.
 *
 * The <config> tag gets parsed recursively and the subnode's names get
 * concatenated by '.'.
 *
 * Example:
 * <config>
 *   <device>/dev/ttyACM0</device>
 *   <image>
 *     <width>320</width>
 *     <height>240</height>
 *   </image>
 * </config>
 *
 * This config is equal to
 *
 * device = /dev/ttyACM0
 * image.width = 320
 * image.height = 240
 *
 * @param node The node to parse.
 * @param key Initial config parameter key
 * @param config ModuleConfig object to put the parsed key-value pairs into.
 */
void parseModuleConfig(pugi::xml_node node, Config &config,
                       const std::string &key = "");

class XmlParser {
public:
    XmlParser(RuntimeInfo &info, const std::vector<std::string> &flags = {});

    std::vector<std::string> const &errors() const;
    std::vector<std::string> const &files() const;

    /**
     * @brief Parse the given XML node as <execution>
     * @param rootNode
     * @param clock
     */
    bool parseClock(pugi::xml_node node, ClockInfo &info);

    bool parseInclude(pugi::xml_node node);

    bool parseModule(pugi::xml_node node, ModuleInfo &info);

    bool parseService(pugi::xml_node node, ServiceInfo &info);

    bool parseLibrary(pugi::xml_node node, LibraryInfo &info);

    bool parseFile(std::istream &is, const std::string &file);
    bool parseFile(const std::string &file);

private:
    std::vector<std::string> m_errors;
    std::vector<std::string> m_files;
    std::stack<std::string> m_filestack;
    std::vector<std::string> m_flags;

    RuntimeInfo &runtime;

    /**
     * @brief Add parse error: An xml node misses a required attribute.
     * @param node
     * @param attr
     */
    bool errorMissingAttr(pugi::xml_node node, pugi::xml_attribute attr);

    /**
     * @brief Add parse error: An xml node has an invalid attribute value.
     * @param node
     * @param attrName
     * @param attrValue
     * @param expectedValue
     */
    bool errorInvalidAttr(pugi::xml_node node, pugi::xml_attribute attr,
                          const std::string &expectedValue);

    void errorInvalidNodeContent(pugi::xml_node, const std::string &expected);

    bool errorFile(const std::string &file);

    bool errorPugiParseResult(const std::string &file,
                              const pugi::xml_parse_result &result);

    bool errorUnknownNode(pugi::xml_node node);
};

} // namespace internal
} // namespace lms

#endif /* LMS_XML_PARSER_H */


text:
class FlowUITextViewSpan < Android::Text::Style::ClickableSpan
  def initialize(view, link)
    @view, @link = view, link
  end

  def onClick(view)
    @view.trigger :link, @link
  end
end

module UI
  class Text < UI::View
    include UI::SharedText
    def text_proxy; proxy; @text_proxy; end
    include Eventable

    def editable?
      @editable
    end

    def editable=(flag)
      if @editable != flag
        @editable = flag
        @proxy = nil
      end
    end

    attr_accessor :link_color

    def links=(links)
      ss = Android::Text::SpannableString.new(self.text)

      links.each do |range, link|
        span = FlowUITextViewSpan.new(self, link)
        ss.setSpan(span, range.begin, range.end, Android::Text::Spanned::SPAN_EXCLUSIVE_EXCLUSIVE)

        if @link_color
          fspan = Android::Text::Style::ForegroundColorSpan.new(UI.Color(@link_color).proxy)
          ss.setSpan(fspan, range.begin, range.end, Android::Text::Spanned::SPAN_INCLUSIVE_INCLUSIVE)
        end
      end

      @text_proxy.text = ss
      @text_proxy.movementMethod = Android::Text::Method::LinkMovementMethod.getInstance
    end

    def proxy
      @proxy ||= begin
        scroll = Android::Widget::ScrollView.new(UI.context)
        @text_proxy = @editable ? Android::Widget::EditText.new(UI.context) : Android::Widget::TextView.new(UI.context)
        scroll.addView(@text_proxy)
        scroll
      end
    end
  end
end


text:
% Implements multi-level mediation models on ROI data
% Reproduces statistics used to create Figure 4 in Fleming, van der Putten & Daw
% Stored in stats_forward{r}, stats_reverse{r}, where r indexes ROI
%
% Requires Tor Wager's Mediation Toolbox and functions from CanLabCore available here:
% https://canlabweb.colorado.edu/wiki/doku.php/help/mediation/m3_mediation_fmri_toolbox
%
% Steve Fleming stephen.fleming@ucl.ac.uk 2017

clear all
close all

cwd = pwd;

addpath(genpath('~/Dropbox/Utils/fmri/MetaLabCore/'));
dir_roiData_upsampled = '~/Dropbox/Published/FlemingNN2018/Code&analysis/FlemingVdPuttenDaw/fmri/roidata_upsample';
dir_datafiles = '~/Dropbox/Published/FlemingNN2018/Code&analysis/FlemingVdPuttenDaw/data';
dir_stats = '~/Dropbox/Published/FlemingNN2018/Code&analysis/FlemingVdPuttenDaw/stats';

name_subj = {'sub12','sub13','sub14','sub15','sub16','sub17','sub18','sub19','sub23',...
    'sub24','sub25','sub26','sub27','sub28','sub30','sub31','sub32','sub33','sub34','sub35','sub36','sub37'};
rois = {'pMFC','union_FPL','union_46','union_FPm'};

permutationtest = 0;    % If 0, load previously computed permutation test stats from .mat file; if 1, compute again
npermute = 1000;

xcenters = [1 2 3];
allBetas = [];
allSE = [];
for r = 1:length(rois)

    % Load up regression outputs from R hierarchical model of confidence/QSR, sems stored as
    % second part of vector
    cd(dir_stats)
    betaFile = ['regression_conf_split_' rois{r} '.csv'];
    betas = csvread(betaFile,1,1);
    segmented_ffx = betas([2 3])';
    segmented_se = betas([6 7])';

    % Load up regression outputs from R hierarchical segmented regression model, sems stored as
    % second part of vector
    h = figure;
    set(gcf, 'Position', [600 300 220 250])
    bar(1, segmented_ffx(1), 'FaceColor', [0.6 0.6 1])
    hold on
    bar(2, segmented_ffx(2), 'FaceColor', [0.8 0.8 0.8])
    errorbar(1, segmented_ffx(1), segmented_se(1), 'o ', 'Color', 'k', 'LineWidth', 2)
    errorbar(2, segmented_ffx(2), segmented_se(2), 'o ', 'Color', 'k', 'LineWidth', 2)
    set(gca, 'XTickLabel', {'',''}, 'YLim', [-0.7 0.5], 'FontSize', 16)
    ylabel('Regression coefficient')
    box off
    title(rois{r})

    % Create upsampled regressions
    for s = 1:length(name_subj)

        % Get behavioural data
        sub = name_subj{s}(4:5);
        datafile = ['fMRI_pilotData_sub_' num2str(sub) '_fMRI_2.mat'];
        cd(dir_datafiles);
        load(datafile)
        cd(cwd);

        % extract locDATA variables
        rt =  locDATA.reaction_time_button;
        conf_rt =  locDATA.reaction_time_mouse;
        precoh = locDATA.dots_coherence;
        postcoh = locDATA.post_coherence;
        accuracy = locDATA.accuracy;
        accuracy(accuracy == 0) = -1;
        conf = locDATA.mouse_response;
        qsr = max(1-(1 - conf).^2, 1-(0 - conf).^2);
        err = isnan(rt) | isnan(accuracy) | isnan(conf);

        % Extract out ordinal variables for coherence
        index = unique(sort(locDATA.dots_coherence));
        for i = 1:3
            precoh(precoh == index(i)) = i;
            postcoh(postcoh == index(i)) = i;
        end

        % Get upsampled ROI data
        cd(dir_roiData_upsampled)
        datafile = ['ROI_' rois{r} '_' name_subj{s}];
        load(datafile)
        cd(cwd)

        subjectXtime(s,:) = ([1:length(ROI.time_series(1,:))].*ROI.spec.samp_reso) - ROI.spec.samp_reso;  % same for all subjects, subtract one bin to start at 0

        % Regression by timepoint on confidence / conf + qsr
        for t = 1:length(ROI.time_series(1,:))

            % Extract out signal over trials for this timepoint
            bold_t = ROI.time_series(:,t);

            % include qsr, store for permutation test
            regress(t).X{s} = [conf' qsr' log(rt)'];
            regress(t).X{s} = scale(regress(t).X{s});
            regress(t).Y{s} = bold_t;    % zscore per timepoint to get standardised coefficients
            b = glmfit(regress(t).X{s},regress(t).Y{s});
            confValBeta1(s,t) = b(2);
            confValBeta2(s,t) = b(3);

        end
        % End subject loop
    end

    %% Group-level analyses
    % Create null distribution of betas for group-level test by permuting
    % Y trial ordering within-subject, don't break neighbourhood of Y over
    % time
    if permutationtest
        for n = 1:npermute
            permute_confValBeta1 = [];
            permute_confValBeta2 = [];
            for s = 1:length(name_subj)
                permuteY = randperm(length(ROI.time_series(:,1)));
                for t = 1:length(ROI.time_series(1,:))
                    X = regress(t).X{s};
                    Y = regress(t).Y{s};
                    bold_permute = Y(permuteY);
                    b = glmfit(X,bold_permute);
                    permute_confValBeta1(s,t) = b(2);
                    permute_confValBeta2(s,t) = b(3);
                end
            end
            % Store t-values against zero (at group level) for this iteration
            [h p ci stats] = ttest(permute_confValBeta1);
            permute_tBeta1(n,:) = stats.tstat;
            [h p ci stats] = ttest(permute_confValBeta2);
            permute_tBeta2(n,:) = stats.tstat;
        end
        % compute significance bounds
        upperCI_beta1 = quantile(permute_tBeta1, 0.975);
        lowerCI_beta1 = quantile(permute_tBeta1, 0.025);
        upperCI_beta2 = quantile(permute_tBeta2, 0.975);
        lowerCI_beta2 = quantile(permute_tBeta2, 0.025);

    else
        cd(dir_stats)
        load([rois{r} '_confval_permutationTest.mat']);
        cd(cwd)
    end
    [h p ci stats] = ttest(confValBeta1);
    actual_tBeta1 = stats.tstat;
    [h p ci stats] = ttest(confValBeta2);
    actual_tBeta2 = stats.tstat;
    sigBeta1 = (actual_tBeta1 > upperCI_beta1) | (actual_tBeta1 < lowerCI_beta1);
    sigBeta2 = (actual_tBeta2 > upperCI_beta2) | (actual_tBeta2 < lowerCI_beta2);

    %% PLOT FIGURES
    xMean = mean(subjectXtime);

    %% Confidence / value beta
    h = figure;
    set(gcf, 'Position', [600 300 300 250])
    betaMean = mean(confValBeta2);
    betaSE = std(confValBeta2)./sqrt(length(name_subj));
    errorarea(xMean, betaMean, betaMean-betaSE, betaMean+betaSE, 'y', [0.8 0.8 0.4]);
    minmaxBeta1 = [min(betaMean) max(betaMean)];
    hold on
    betaMean = mean(confValBeta1);  % plot confidence second on top
    betaSE = (std(confValBeta1)./sqrt(length(name_subj)));
    errorarea(xMean, betaMean, betaMean-betaSE, betaMean+betaSE, [0.91 0.30 0.06], [0.91 0.44 0.20]);
    minmaxBeta2 = [min(betaMean) max(betaMean)];
    minmaxAll = [min([minmaxBeta1(1) minmaxBeta2(1)]) max([minmaxBeta1(2) minmaxBeta2(2)])];
    ymax = minmaxAll(2) + 0.15;
    ymin = minmaxAll(1) - 0.15;

    set(gca, 'FontSize', 16, 'YLim', [ymin ymax], 'XLim', [0 max(xMean)])
    line([0 max(xMean)], [0 0], 'LineStyle', '--', 'Color', 'k', 'LineWidth', 1.5)
    line([1.9 1.9], [ymin ymin+0.05], 'LineStyle', '-', 'Color', 'k', 'LineWidth', 1.5) % add line at onset of PDE

    % Add significance below plot
    hold on
    minBeta = min(betaMean);
    totSig1 = sum(sigBeta1);
    plot(xMean(sigBeta1), ones(1,totSig1).*(ymin+0.03), 'o ', 'Color', [0.91 0.44 0.20])
    totSig2 = sum(sigBeta2);
    plot(xMean(sigBeta2), ones(1,totSig2).*(ymin+0.015), 'yo ')
    ylabel('Regression coefficient')
    xlabel('Time (s)')
    box off
    title(rois{r})
end
cd(cwd)


text:
package org.elasticsearch.xpack.core.security.authc.support.mapper;

public final class NativeRoleMappingStoreField {

    public static final String DOC_TYPE_FIELD = "doc_type";
    public static final String DOC_TYPE_ROLE_MAPPING = "role-mapping";
    public static final String ID_PREFIX = DOC_TYPE_ROLE_MAPPING + "_";

    private NativeRoleMappingStoreField() {}
}